Where can I find an example of reading from and writing to the TINI Board's serial ports?
Created May 4, 2012
Tim Rohaly
The following program demonstrates how to use the Java Communications API on a TINI board to read and write characters to a serial port. Since the TINI uses the CommAPI for its serial ports, you can also look at the sample programs included with the CommAPI.
import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.util.TooManyListenersException; import javax.comm.*; /** * Simple program to read characters from the command-line, send * them to the specified serial port, and echo any characters returned. * Modeled after the SimpleRead and SimpleWrite sample programs included * with the Java Communications API distribution. */ public class SimpleReadWrite implements Runnable, SerialPortEventListener { static String DEFAULT_PORT = "serial0"; InputStream in; OutputStream out; /** * Main method. */ public static void main(String[] args) { String portname = DEFAULT_PORT; if (args.length > 1) { System.err.println("Usage: java SimpleReadWrite [port_name]"); System.exit(1); } else if (args.length == 1) { portname = args[0]; } try { CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portname); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { SimpleReadWrite srw = new SimpleReadWrite(portId); } else { System.err.println(portname + " is not a serial port"); System.exit(1); } } catch (NoSuchPortException e) { System.err.println("No such port: " + portname); System.exit(1); } catch (PortInUseException e) { System.err.println("Port in use: " + portname); System.exit(1); } } /** * Constructor. */ public SimpleReadWrite(CommPortIdentifier portId) throws PortInUseException { try { SerialPort serialPort = (SerialPort) portId.open("SimpleReadWrite", 5000); in = serialPort.getInputStream(); out = serialPort.getOutputStream(); serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); Thread readerThread = new Thread(this); readerThread.start(); } catch (IOException e) { System.err.println("Problem opening streams"); System.exit(1); } catch (TooManyListenersException e) { System.err.println("Too Many Listeners"); System.exit(1); } catch (UnsupportedCommOperationException e) { System.err.println("Problem setting port parameters"); System.exit(1); } } /** * This method loops forever, reading data from the * command line and writing it directly to the serial port. * No buffering is performed. */ public void run() { byte character; try { while (true) { character = (byte) System.in.read(); out.write(character); } } catch (IOException e) { // Terminate thread } } /** * Process serial port events. This method is required * when implementing SerialPortEventListener * * @param event SerialPortEvent object to process */ public void serialEvent(SerialPortEvent event) { switch (event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: // If there is data available on serial port, // read it in in chunks <=20 bytes // byte[] readBuffer = new byte[20]; try { while (in.available() > 0) { int numBytes = in.read(readBuffer); } System.out.print(new String(readBuffer)); } catch (IOException e) { // Terminate handling this event on read error } break; default: System.out.print("Unknown SerialPortEvent type = " + event.getEventType()); break; } } }