|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface CommConnection
This is the primary control class for a Serial port. An instance of this class may be operated on by more than one thread. Settings will be those of the last thread to successfully change each particular setting.
Code written to use a javax.comm.SerialPort object in a shared mode should make use of synchronization blocks where exclusive transactions are wanted. In most instances, both the OutputStream and the InputStream should be synchronized on, normally with the OutputStream being synchronized first.
Example Code: try { String uri = new CommURI.Builder("/dev/tty.PL2303-00001004") .withBaudRate(19200) .withDataBits(8) .withStopBits(1) .withParity(0) .withTimeout(2000) .build().toString(); CommConnection connOne = (CommConnection) CommTest.connectionFactory.createConnection(uri, 1, false); assertNotNull(connOne); uri = new CommURI.Builder("/dev/tty.PL2303-00002006") .withBaudRate(19200) .withDataBits(8) .withStopBits(1) .withParity(0) .withTimeout(2000) .build().toString(); CommConnection connTwo = (CommConnection) CommTest.connectionFactory.createConnection(uri, 1, false); assertNotNull(connTwo); InputStream isOne = connOne.openInputStream(); OutputStream osOne = connOne.openOutputStream(); InputStream isTwo = connTwo.openInputStream(); OutputStream osTwo = connTwo.openOutputStream(); assertNotNull(isOne); assertNotNull(osOne); assertNotNull(isTwo); assertNotNull(osTwo); //write from one to two byte[] array = "this is a message from one to two\n".getBytes(); osOne.write(array); StringBuffer sb = new StringBuffer(); int c; while((c = isTwo.read()) != 0xa) { //System.out.println("port 2: read byte: 0x" + Integer.toHexString(c) + " -> " + (char)c); sb.append((char)c); } System.out.println("Port 2: Read from serial port two: " + sb.toString()); array = "this is a message from two to one\n".getBytes(); osTwo.write(array); sb = new StringBuffer(); while((c = isOne.read()) != 0xa) { //System.out.println("port 1: read byte: 0x" + Integer.toHexString(c) + " -> " + (char)c); sb.append((char)c); } System.out.println("Port 1: Read from serial port: " + sb.toString()); isOne.close(); osOne.close(); isOne = null; osOne = null; isTwo.close(); osTwo.close(); isTwo = null; osTwo = null; connOne.close(); connOne = null; connTwo.close(); connTwo = null; } catch (Exception e) { e.printStackTrace(); }Note: avoid blocking read (InputStream.read()) if the InputStream can be closed on a different thread, in this case, the read will never exit and the thread will be blocked forever. It is preferable to test InputStream.available before InputStream.read():
if (isOne.available() != 0) { c = isOne.read(); } else { try { Thread.sleep(100); continue; } catch (InterruptedException e) { return; }
Method Summary | |
---|---|
void |
close()
|
byte[] |
flushSerialBuffer()
Reads all bytes that are waiting in the serial port buffer and returns them in an array. |
CommURI |
getURI()
Returns the URI for this connection. |
byte[] |
sendCommand(byte[] command,
int timeout)
Sends and array of bytes to a CommConnection and returns an array of bytes that represents the 'response' to the command. |
byte[] |
sendCommand(byte[] command,
int timeout,
int demark)
|
void |
sendMessage(byte[] message)
Sends and array of bytes to a CommConnection |
Methods inherited from interface javax.microedition.io.InputConnection |
---|
openDataInputStream, openInputStream |
Methods inherited from interface javax.microedition.io.OutputConnection |
---|
openDataOutputStream, openOutputStream |
Method Detail |
---|
CommURI getURI()
void sendMessage(byte[] message) throws EsfException, IOException
message
- the array of bytes to send to the CommConnection
EsfException
IOException
byte[] sendCommand(byte[] command, int timeout) throws EsfException, IOException
command
- the array of bytes to send to the CommConnectiontimeout
- the maximum length of time to wait before returning a null
response in the event no response is ever returned.
EsfException
IOException
byte[] sendCommand(byte[] command, int timeout, int demark) throws EsfException, IOException
EsfException
IOException
byte[] flushSerialBuffer() throws EsfException, IOException
EsfException
IOException
void close() throws IOException
close
in interface javax.microedition.io.Connection
IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |