You are viewing outdated content for BUG. If you have a BUG Y.T. edition or 2.0 series device, please visit our updated wiki: http://wiki.buglabs.net
Serial Port Programming
From BUG Wiki
Contents |
Overview
This page describes how to use the Serial Port API for the BUGvonHippel. The Serial Port API was added in BUG Release 1.4. IVonHippelSerialPort is based on the JSR 118 implementation for javax.microedition.CommConnection. The linux device nodes corresponding to the Von Hippel's serial output are /dev/ttymxc/0, /dev/ttymxc/1, /dev/ttymxc/2, /dev/ttymxc/3, where the numbers correspond to the slots into which the module is inserted.
Hardware Overview
The module itself has pins for TX, RX, CTS, RTS, and GND.
They are at RS232 voltage levels, not TTL levels, so to communicate with device like an arduino, you will likely need some kind of a level shifter. For more information about serial port wiring conventions, have a look at allpinouts.org.
IVonHippelSerialPort Overview
IVonHippelSerialPort is a convenience wrapper to the javax.microedition.CommConnection subsystem. It allows a BUGapp (OSGi bundles) to take control of, read from, and write to the RS232 output on the VonHippel module.
IVonHippelSerialPort API
The IVonHippelSerialPort service, like all BUG Module services, is made available upon module recognition. There are various getters and setters for the properties of the serial port, consistent with the javax.microedition.CommConnection connection string fields. These fields may only be set while there is not an existing open reference to the InputStream or OutputStream objects used to read or write to the serial port. Fields:
| Parameter | Default | Description |
|---|---|---|
| baudrate | 9600 | The speed of the serial port. |
| bitsperchar | 8 | The number bits per character (either 7 or 8). |
| stopbits | "1" | The number of stop bits per char (either 1 or 2). |
| parity | "none" | "odd", "even", or "none". |
| blocking | false | The number bits per character (either 7 or 8). |
| autocts | false | If true, wait for the CTS line to be on before writing. |
| autorts | false | If true, turn on the RTS line when input buffer is not full. If false, the RTS line is always on. |
getSerialOutputStreamor
getSerialOutputStreamwill initialize the serial device with the specified parameters.
Examples
Changes to Home Directory
Here is a Service Tracker that listens for the availability of the IVonHippelSerialPort service, then in the doStart method, writes out periodically.
public void doStart() { System.out.println("VonHippelSerialExampleServiceTracker: start"); vhsp = (IVonHippelSerialPort)getService(IVonHippelSerialPort.class); try { vhsp.setBaudrate("19200"); vhsp.setAutoCTS(true); vhsp.setAutoRTS(true); System.out.println(vhsp.getBaudrate()); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(vhsp.getSerialOutputStream())); String message = "This is some data to write to serial port"; for (int i = 0; i < 100; i++) { Thread.sleep(1000); System.out.println("message"); bw.write(message); bw.flush(); } bw.flush(); bw.close(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
This application is available on BUGnet.
Multi-threaded Sender/Receiver Example
This example spawns two anonymous inner threads. One for listening, the other for sending over the serial port.
/** * If canStart returns true this method is called to start the application. * Place your fun logic here. */ public void doStart() { System.out.println("VonHippelSerialExampleServiceTracker: start"); vhsp = (IVonHippelSerialPort) getService(IVonHippelSerialPort.class); try { vhsp.setBaudrate("19200"); vhsp.setAutoCTS(true); vhsp.setAutoRTS(true); } catch (IOException ioe) { ioe.printStackTrace(); } //writer thread new Thread() { public void run() { try { BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(vhsp.getSerialOutputStream())); String message = "This is some data to write to serial port"; for (int i = 0; i < 100; i++) { Thread.sleep(1000); System.out.println("message"); bw.write(message); bw.flush(); } bw.flush(); bw.close(); } catch (Exception e) { e.printStackTrace(); } } }.start(); //reader thread new Thread() { public void run() { try { BufferedReader bw = new BufferedReader( new InputStreamReader(vhsp.getSerialInputStream())); String message = ""; while (message!=null){ message = bw.readLine(); System.out.println(message); } } catch (Exception e) { e.printStackTrace(); } } }.start(); }
Caveats
- The default parameters for CommConnection provided by IVonHippelSerialPort are not applicable to every device. Depending on your device, you'll probably want to at least set the baud rate, set setAutoCTS(true), and setAutoRTS(true).
Notes
- We're hoping to switch the implementation for serial access from the javax.microedition.CommConnection implementation to rxtx in the near future. While it would be convenient to make the implementation transparent (i.e. not break the interface), the rxtx implementation is far more robust and feature-rich than the MID implementation. This means that the IVonHippelSerialPort interface may be deprecated in the future.
Categories: Java | API | Software

