Com

interface Com

The {@link Com} interface provide methods that enables the use of the serial communication port.
Implementation of the {@link Com} interface is provided in the class {@link SerialCommunication}.
To start using the serial communication an instance of the class {@link SerialCommunication} needs to be created.
Below examples show how to connect to the serial communication port,
setup callback to get notification when data is available for reading and method to write data.

Create instance of the SerialCommunication class and do connect.

         ...         {@code           SerialCommunication = _serialCommunication = new SerialCommunication();           if (_serialCommunication.connect(_baudRate)) {             setDataAvailableCallback();             Toast.makeText(SerialComActivity.this, "Successful connect!", Toast.LENGTH_SHORT).show();           } else {             Toast.makeText(SerialComActivity.this, "Unsuccessful connect!", Toast.LENGTH_SHORT).show();           }         }         ...     
Setup data available callback and read data when it is available.
         {@code           private void setDataAvailableCallback() {             _serialCommunication.setDataAvailableCallback(new DataAvailable()  {               Override               public void onDataAvailable(SerialCommunication.EventTypes event) {                 if (event == SerialCommunication.EventTypes.DATA_AVAILABLE) {                   byte [] buffer = new byteMAX_BUFFER_SIZE;                   _serialCommunication.read(buffer, buffer.length);                 }               }             });           }         }     
Write to the serial port.
         ...         {@code           byte[] buffer = text.getBytes();           _serialCommunication.write(buffer, buffer.length);         }         ...     

Types

Link copied to clipboard

Baud rates

Link copied to clipboard

Number of data bits.

Link copied to clipboard

Event types.

Link copied to clipboard

Parity.

Link copied to clipboard

Stop bits.

Functions

Link copied to clipboard
abstract fun connect(baudRate: Com.BaudRates?): Boolean
abstract fun connect(baudRate: Com.BaudRates?, stopBits: Com.StopBits?, dataBits: Com.DataBits?, parity: Com.Parity?): Boolean

Open and connect to the serial port.

Link copied to clipboard
abstract fun disconnect()

Disconnect the connected port.

Link copied to clipboard
abstract fun isOpened(): Boolean

Check if serial communication port is open.

Link copied to clipboard
abstract fun read(data: ByteArray?, length: Int): Int

Read data from the serial communication port. Timeout default 3 seconds.

abstract fun read(data: ByteArray?, length: Int, timeout: Int): Int

Read data from the serial communication port.

Link copied to clipboard
abstract fun setDataAvailableCallback(dataAvailable: DataAvailable?)

Set data available callback for the application layer.

Link copied to clipboard
abstract fun write(data: ByteArray?, length: Int): Int

Write data to the serial communication port. Timeout default 3 seconds.

abstract fun write(data: ByteArray?, length: Int, timeout: Int): Int

Write data to the serial communication port.

Inheritors

Link copied to clipboard