processing.serial
Class Serial

java.lang.Object
  extended by processing.serial.Serial
All Implemented Interfaces:
gnu.io.SerialPortEventListener, java.util.EventListener

public class Serial
extends java.lang.Object
implements gnu.io.SerialPortEventListener


Field Summary
 int databits
           
 java.io.InputStream input
           
 java.io.OutputStream output
           
 int parity
           
 gnu.io.SerialPort port
           
 int rate
           
 int stopbits
           
 
Constructor Summary
Serial(PApplet parent)
           
Serial(PApplet parent, int irate)
           
Serial(PApplet parent, java.lang.String iname)
           
Serial(PApplet parent, java.lang.String iname, int irate)
           
Serial(PApplet parent, java.lang.String iname, int irate, char iparity, int idatabits, float istopbits)
           
 
Method Summary
 int available()
          Returns the number of bytes that have been read from serial and are waiting to be dealt with by the user.
 void buffer(int count)
          Set number of bytes to buffer before calling serialEvent() in the host applet.
 void bufferUntil(int what)
          Set a specific byte to buffer until before calling serialEvent() in the host applet.
 void clear()
          Ignore all the bytes read so far and empty the buffer.
 void dispose()
          Used by PApplet to shut things down.
static void errorMessage(java.lang.String where, java.lang.Throwable e)
          General error reporting, all corraled here just in case I think of something slightly more intelligent to do.
 int last()
          Same as read() but returns the very last value received and clears the buffer.
 char lastChar()
          Just like last() and readChar().
static java.lang.String[] list()
          If this just hangs and never completes on Windows, it may be because the DLL doesn't have its exec bit set.
 int read()
          Returns a number between 0 and 255 for the next byte that's waiting in the buffer.
 byte[] readBytes()
          Return a byte array of anything that's in the serial buffer.
 int readBytes(byte[] outgoing)
          Grab whatever is in the serial buffer, and stuff it into a byte buffer passed in by the user.
 byte[] readBytesUntil(int interesting)
          Reads from the serial port into a buffer of bytes up to and including a particular character.
 int readBytesUntil(int interesting, byte[] outgoing)
          Reads from the serial port into a buffer of bytes until a particular character.
 char readChar()
          Returns the next byte in the buffer as a char.
 java.lang.String readString()
          Return whatever has been read from the serial port so far as a String.
 java.lang.String readStringUntil(int interesting)
          Combination of readBytesUntil and readString.
 void serialEvent(gnu.io.SerialPortEvent serialEvent)
           
 void setDTR(boolean state)
          Set the DTR line.
 void setProperties(java.util.Properties props)
           
 void stop()
          Stop talking to serial and shut things down.
 void write(byte[] bytes)
           
 void write(int what)
          This will handle both ints, bytes and chars transparently.
 void write(java.lang.String what)
          Write a String to the output.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

port

public gnu.io.SerialPort port

rate

public int rate

parity

public int parity

databits

public int databits

stopbits

public int stopbits

input

public java.io.InputStream input

output

public java.io.OutputStream output
Constructor Detail

Serial

public Serial(PApplet parent)

Serial

public Serial(PApplet parent,
              int irate)

Serial

public Serial(PApplet parent,
              java.lang.String iname,
              int irate)

Serial

public Serial(PApplet parent,
              java.lang.String iname)

Serial

public Serial(PApplet parent,
              java.lang.String iname,
              int irate,
              char iparity,
              int idatabits,
              float istopbits)
Method Detail

setProperties

public void setProperties(java.util.Properties props)

stop

public void stop()
Stop talking to serial and shut things down.

Basically just a user-accessible version of dispose(). For now, it just calls dispose(), but dispose shouldn't be called from applets, because in some libraries, dispose() blows shit up if it's called by a user who doesn't know what they're doing.


dispose

public void dispose()
Used by PApplet to shut things down.


setDTR

public void setDTR(boolean state)
Set the DTR line. Addition from Tom Hulbert.


serialEvent

public void serialEvent(gnu.io.SerialPortEvent serialEvent)
Specified by:
serialEvent in interface gnu.io.SerialPortEventListener

buffer

public void buffer(int count)
Set number of bytes to buffer before calling serialEvent() in the host applet.


bufferUntil

public void bufferUntil(int what)
Set a specific byte to buffer until before calling serialEvent() in the host applet.


available

public int available()
Returns the number of bytes that have been read from serial and are waiting to be dealt with by the user.


clear

public void clear()
Ignore all the bytes read so far and empty the buffer.


read

public int read()
Returns a number between 0 and 255 for the next byte that's waiting in the buffer. Returns -1 if there was no byte (although the user should first check available() to see if things are ready to avoid this)


last

public int last()
Same as read() but returns the very last value received and clears the buffer. Useful when you just want the most recent value sent over the port.


readChar

public char readChar()
Returns the next byte in the buffer as a char. Returns -1, or 0xffff, if nothing is there.


lastChar

public char lastChar()
Just like last() and readChar().


readBytes

public byte[] readBytes()
Return a byte array of anything that's in the serial buffer. Not particularly memory/speed efficient, because it creates a byte array on each read, but it's easier to use than readBytes(byte b[]) (see below).


readBytes

public int readBytes(byte[] outgoing)
Grab whatever is in the serial buffer, and stuff it into a byte buffer passed in by the user. This is more memory/time efficient than readBytes() returning a byte[] array. Returns an int for how many bytes were read. If more bytes are available than can fit into the byte array, only those that will fit are read.


readBytesUntil

public byte[] readBytesUntil(int interesting)
Reads from the serial port into a buffer of bytes up to and including a particular character. If the character isn't in the serial buffer, then 'null' is returned.


readBytesUntil

public int readBytesUntil(int interesting,
                          byte[] outgoing)
Reads from the serial port into a buffer of bytes until a particular character. If the character isn't in the serial buffer, then 'null' is returned. If outgoing[] is not big enough, then -1 is returned, and an error message is printed on the console. If nothing is in the buffer, zero is returned. If 'interesting' byte is not in the buffer, then 0 is returned.


readString

public java.lang.String readString()
Return whatever has been read from the serial port so far as a String. It assumes that the incoming characters are ASCII. If you want to move Unicode data, you can first convert the String to a byte stream in the representation of your choice (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.


readStringUntil

public java.lang.String readStringUntil(int interesting)
Combination of readBytesUntil and readString. See caveats in each function. Returns null if it still hasn't found what you're looking for. If you want to move Unicode data, you can first convert the String to a byte stream in the representation of your choice (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.


write

public void write(int what)
This will handle both ints, bytes and chars transparently.


write

public void write(byte[] bytes)

write

public void write(java.lang.String what)
Write a String to the output. Note that this doesn't account for Unicode (two bytes per char), nor will it send UTF8 characters.. It assumes that you mean to send a byte buffer (most often the case for networking and serial i/o) and will only use the bottom 8 bits of each char in the string. (Meaning that internally it uses String.getBytes) If you want to move Unicode data, you can first convert the String to a byte stream in the representation of your choice (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.


list

public static java.lang.String[] list()
If this just hangs and never completes on Windows, it may be because the DLL doesn't have its exec bit set. Why the hell that'd be the case, who knows.


errorMessage

public static void errorMessage(java.lang.String where,
                                java.lang.Throwable e)
General error reporting, all corraled here just in case I think of something slightly more intelligent to do.