com.eurotech.framework.comm
Interface CommConnection

All Superinterfaces:
javax.microedition.io.Connection, javax.microedition.io.InputConnection, javax.microedition.io.OutputConnection, javax.microedition.io.StreamConnection
All Known Implementing Classes:
CommConnectionImpl

public interface CommConnection
extends javax.microedition.io.StreamConnection

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

getURI

CommURI getURI()
Returns the URI for this connection.

Returns:
this connection URI

sendMessage

void sendMessage(byte[] message)
                 throws EsfException,
                        IOException
Sends and array of bytes to a CommConnection

Parameters:
message - the array of bytes to send to the CommConnection
Throws:
EsfException
IOException

sendCommand

byte[] sendCommand(byte[] command,
                   int timeout)
                   throws EsfException,
                          IOException
Sends and array of bytes to a CommConnection and returns an array of bytes that represents the 'response' to the command. If the timeout is exceeded before any bytes are read on the InputStream null is returned. This is meant to be used in common command/response type situations when communicating with serial devices

Parameters:
command - the array of bytes to send to the CommConnection
timeout - the maximum length of time to wait before returning a null response in the event no response is ever returned.
Returns:
an array of bytes representing the response
Throws:
EsfException
IOException

sendCommand

byte[] sendCommand(byte[] command,
                   int timeout,
                   int demark)
                   throws EsfException,
                          IOException
Throws:
EsfException
IOException

flushSerialBuffer

byte[] flushSerialBuffer()
                         throws EsfException,
                                IOException
Reads all bytes that are waiting in the serial port buffer and returns them in an array. This can be used to read unsolicited messages from an attached serial device.

Returns:
the array of bytes buffered on the InputStream if any
Throws:
EsfException
IOException

close

void close()
           throws IOException
Specified by:
close in interface javax.microedition.io.Connection
Throws:
IOException


Copyright © 2013. All Rights Reserved.