Author Topic: Voltage to Bytes?  (Read 1530 times)

0 Members and 1 Guest are viewing this topic.

Offline vidamTopic starter

  • Supreme Robot
  • *****
  • Posts: 423
  • Helpful? 1
  • Robotronics.org
Voltage to Bytes?
« on: January 14, 2008, 08:40:37 AM »
First, I'm using the gumstix product called RoboStix to read in values from DC brushless motor encoders.
Below, I have copied Java code from a book called "Java Robots" in order to communicate to the micro-controller via a USB port.

Not knowing anything about the encoders or this code, and all I am sort of confused on how to convert the voltage levels to bytes or rather how to interpret what the byte values will mean in the code.

package com.scottpreston.javarobot.chapter2;

public interface JSerialPort{

   public byte[] read();
   public String readString();
   public void write(byte[] b) throws Exception;
   public void close();
   public void setDTR(boolean dtr);
   public void setTimeout(int tOut);
   public static final String READ_COMMAND = "r";
   public static final String WRITE_COMMAND = "w";
   public static final String WRITE_READ_COMMAND = "wr";
   public byte[] read(byte[] b) throws Exception;
   public String readString(byte[] b) throws Exception;
}
package com.scottpreston.javarobot.chapter2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;

public class StandardSerialPort implements SerialPortEventListener,
       JSerialPort {

   private Enumeration portList;
   private CommPortIdentifier portId;
   private SerialPort serialPort;
   private OutputStream outputStream;
   private InputStream inputStream;
   private byte[] readBuffer;
   private boolean dataIn = false;
   private byte[] currentWrite;
   private int i = 0;

   public StandardSerialPort(int id) throws Exception {
       init(id, 9600);
   }

   public StandardSerialPort(int id, int baud) throws Exception {
       init(id, baud);
   }

   private void init(int comID, int baud) {
       String comIdAsString = new Integer(comID).toString();
       try {
           portList = CommPortIdentifier.getPortIdentifiers();
           while (portList.hasMoreElements()) {
               portId = (CommPortIdentifier) portList.nextElement();
               if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                   if (portId.getName().endsWith(comIdAsString)) {
                       // create serial port
                       serialPort = (SerialPort) portId.open(
                               "StandardSerialPort", 3000);

                       // set config parms
                       serialPort.setSerialPortParams(baud,
                               SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                               SerialPort.PARITY_NONE);
                       serialPort
                               .setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
                       Utils.pause(50);
                       // config output stream
                       outputStream = serialPort.getOutputStream();
                       // config input stream
                       inputStream = serialPort.getInputStream();
                       // add events listener
                       serialPort.addEventListener(this);
                       serialPort.notifyOnDataAvailable(true);
                       Thread.sleep(50); // waits till ports change state.
                   }
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       }

   }

   public byte[] read() {
       while (!dataIn) {
           try {
               Thread.sleep(1);
           } catch (Exception e) {
           }
       }
       dataIn = false;
       return readBuffer;
   }

   public String readString() {
       byte[] b = read();
       StringBuffer s = new StringBuffer();
       for (int i = 0; i < b.length; i++) {
           if (b != 0) {
               int in = (int) b;
               if (in < 0) {
                   in = in + 256;
               }
               s.append(in);
               s.append("~");
           }
       }

       s.deleteCharAt(s.length() - 1);
       return s.toString();
   }

   public void write(byte[] b) throws Exception {
       currentWrite = b;
       outputStream.write(b);
   }

   public void close() {
       serialPort.close();
   }

   public byte[] read(byte[] b) throws Exception {
       // not used
       return null;
   }

   public String readString(byte[] b) throws Exception {
       //      not used
       return null;

   }

   public void serialEvent(SerialPortEvent event) {

       if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
           readBuffer = new byte[32];
           i = 0;
           try {
               while (inputStream.available() > 0) {
                   int numBytes = inputStream.read(readBuffer);
               }
               int byteCount = 0;
               for (int i = 0; i < currentWrite.length; i++) {
                   if (currentWrite == readBuffer) {
                       byteCount++;
                   }
               }
               if (byteCount != currentWrite.length) {
                   dataIn = true;
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }

   }

   public void setTimeout(int timeout) {
       // not used
   }

   public void setDTR(boolean dtr) {
       serialPort.setDTR(dtr);
   }

   public String getName() {
       return serialPort.getName();
   }

}

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: Voltage to Bytes?
« Reply #1 on: January 14, 2008, 08:48:42 AM »
You should take baby steps.

First, get your digital I/O initialized.

Then have it read in input, and store that value as a variable (either a 0 or 1). Use an LED to verify it's working.

Then use a rprintf to send that variable over UART using hyperterminal (search this site for tutorials on each of these).

Don't try to do all of it at once . . . thats a debug nightmare.

(and why are you programming in java for a microcontroller? ;))

 


Get Your Ad Here