This topic discusses the various factors that need to be considered while developing Serial communication applications.
The com.adventnet.cli.serial package contains the implementation of CLITransportProvider and CLIProtocolOptions for Serial communication.
SerialCommProviderImpl
The SerialCommProviderImpl class is for communicating with the CLI device through serial port (RS232 interface). To communicate with devices using serial port, the cliTransport.conf file in <CLI Home>/conf has to be modified as:
|
com.adventnet.cli.transport.SerialCommProviderImpl |
SerialCommOptionsImpl
The SerialCommOptionsImpl class provides the Serial port options implementation of the CLIProtocolOptions Interface. It contains parameters such as the baud rate, data bits, parity, stop bits, and flow control mode. In order to establish a CLI session over serial port with a particular device, the appropriate parameters have to be set on an instance of this class and passed to the CLISession's constructor.
A subsequent call to the open method in CLISession will establishes the serial connection based on the parameters set here.
|
SerialCommOptionsImpl sp = new SerialCommOptionsImpl(); sp.setSerialCommParameters(9600,8,1,0); sp.setPortId("COM1"); CLISession clisession = new CLISession(sp); clisession.setTransportProviderClassName (com.adventnet.cli.transport.SerialCommProviderImpl); clisession.open(); |
SerialCommSession
You can use SerialCommSession class to establish Serial connections with the desired device. A Serial connection is established after you connect to the device with appropriate options.
SerialCommSession API provides the following methods to set various connection options:
| Method | Purpose |
|---|---|
|
This method can be used to open a serial port connection through the port identifier already set. | |
|
This method can be used to open a serial port connection through the specified port identifier. | |
|
This method can be used to set the serial port ID. | |
|
setSerialCommParameters(int baudRate, int dataBits, int stopBits,int parity) |
This method can be used to set the communication parameters such as baud rate, data bits, stop bits, and parity. |
|
This method can be used to set the flow control mode. |
The SerialCommProviderImpl implementation class is used to establish a CLI session for communicating with the device through serial port (RS232 interface).
Establish the session by passing the port identifier to the open method. The port can be either COM1, COM2 etc on windows machines or /dev/ttyS0, /dev/ttyS1 on UNIX based systems.
Provide Serial communication parameters such as baud rate, data bits, stop bits, and parity using the setSerialCommParameters method.
Send commands and receive response using the read and write methods.
Close the serial port connection using the close method.
The following code snippet explains how to establish a Serial session with a device.
|
SerialCommSession serial = new SerialCommSession(); serial.open("COM1"); serial.setSerialCommParameters(9600,8,1,0); serial.setFlowControlMode( SerialCommOptionsImpl.FLOWCONTROL_NONE ); |
|
Note:
|
Please refer to Java documentation for complete details.