Class vga_1_4

java.lang.Object
  |
  +--vga_1_4
All Implemented Interfaces:
java.util.EventListener, javax.comm.SerialPortEventListener

public class vga_1_4
extends java.lang.Object
implements javax.comm.SerialPortEventListener

Example class that uses javax.comm and serial events This class implements SerialPortEventListener in order to receive the DataAvailable event.


Field Summary
protected  java.io.InputStream inStream
          reference to the serial input stream
(package private)  Motor mElbow
           
private  byte msgIndex
           
private  byte msgSize
          Number of bytes total in a message from the LCD.
private  byte msgValue
          This is the value of the message if it is a byte, such as LCD doing a get byte or sending an RPC 03, in this case the value is 0x03.
(package private)  Motor mWrist
           
protected  java.io.OutputStream outStream
          reference to the serial output stream
(package private) static byte[] readBuffer
          The incoming data buffer, will hold request from the LCD
 
Constructor Summary
vga_1_4()
           
 
Method Summary
private  byte ascHex(byte[] barray, byte offset)
           
 void blockThread()
          method to block a thread
 void echo(java.io.InputStream inStream, java.io.OutputStream outStream)
          Method that reads the input stream and echo's the data out the uart.
If no data is available it will block waiting for incomming data What we want here is to look for a variable length packet from the LCD, build up an array until the packet is complete, then process it and start all over.
 void initializeSerialPort()
          Method to initialize the serial port (9600N81)and set up the input and output streams.
static void main(java.lang.String[] args)
          Main entry point for the program.
Initializes the serial port, sends a message, then blocks.
 void processMsg()
          Process the message, which exists in readBuffer
 void sendMessage(java.lang.String msgString)
          Sends a string out the serial port.
 void serialEvent(javax.comm.SerialPortEvent ev)
          Javax.comm will call this method whenever a Data Available event occurs
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

inStream

protected java.io.InputStream inStream
reference to the serial input stream

outStream

protected java.io.OutputStream outStream
reference to the serial output stream

msgSize

private byte msgSize
Number of bytes total in a message from the LCD. For example, a get byte has the message type and two ascii chars of the message ID, total of 3 bytes. Use a value of 0 to mean we are not currently building up a packet. As soon as we receive the start byte we set msgSize to a nonzero value

msgValue

private byte msgValue
This is the value of the message if it is a byte, such as LCD doing a get byte or sending an RPC 03, in this case the value is 0x03.

msgIndex

private byte msgIndex

readBuffer

static byte[] readBuffer
The incoming data buffer, will hold request from the LCD

mElbow

final Motor mElbow

mWrist

final Motor mWrist
Constructor Detail

vga_1_4

public vga_1_4()
Method Detail

main

public static void main(java.lang.String[] args)
Main entry point for the program.
Initializes the serial port, sends a message, then blocks. Serial data will be echoed via the SerialThread calling SerialEvent() method with DataAvailable messages.

blockThread

public void blockThread()
method to block a thread

sendMessage

public void sendMessage(java.lang.String msgString)
Sends a string out the serial port.
Parameters:
msgString - is the string to be send out the serial port

echo

public void echo(java.io.InputStream inStream,
                 java.io.OutputStream outStream)
Method that reads the input stream and echo's the data out the uart.
If no data is available it will block waiting for incomming data What we want here is to look for a variable length packet from the LCD, build up an array until the packet is complete, then process it and start all over. Packets come at most every 100 msec or so. The Client Start of Message values 0x11-0x15 cannot occur in the message body and indicate an error if they do. Communication is half duplex. The LCD is considered the master. LCD Message type bytes are 0x11-0x15. These are followed by two or more ASCII bytes which should only have the values 0x20-0x7F. So we can start a packet receipt by looking for 0x11-0x15. 0x10 is the acknowledge message and has no other data following. The reply starts with the message type but with the 0x11 changed to 0x21, then followed by appropriate data. 0x14 is the RPC message followed by the RPC Hi and Lo nibbles. Response to the RPC should be 0x24 followed by the RPC Hi and Lo nibbles. An action button such as "lower arm" could use this RPC.

processMsg

public void processMsg()
Process the message, which exists in readBuffer

ascHex

private byte ascHex(byte[] barray,
                    byte offset)

serialEvent

public void serialEvent(javax.comm.SerialPortEvent ev)
Javax.comm will call this method whenever a Data Available event occurs
Specified by:
serialEvent in interface javax.comm.SerialPortEventListener

initializeSerialPort

public void initializeSerialPort()
Method to initialize the serial port (9600N81)and set up the input and output streams. If it is unable to aquire the serial port, the program will exit. It demostrates how to get a hold of the serial input and output streams using javax.comm. It also registers this class as a listener for the data available events with Javax.comm.