public interface Com
Com
interface provide methods that enables the use of the serial communication port.Com
interface is provided in the class SerialCommunication
.SerialCommunication
needs to be created.
...
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.
private void setDataAvailableCallback() {
_serialCommunication.setDataAvailableCallback(new DataAvailable() {
Override
public void onDataAvailable(SerialCommunication.EventTypes event) {
if (event == SerialCommunication.EventTypes.DATA_AVAILABLE) {
byte [] buffer = new byte[MAX_BUFFER_SIZE];
_serialCommunication.read(buffer, buffer.length);
}
}
});
}
Write to the serial port.
...
byte[] buffer = text.getBytes();
_serialCommunication.write(buffer, buffer.length);
...
Modifier and Type | Interface and Description |
---|---|
static class |
Com.BaudRates
Baud rates
|
static class |
Com.DataBits
Number of data bits.
|
static class |
Com.EventTypes
Event types.
|
static class |
Com.Parity
Parity.
|
static class |
Com.StopBits
Stop bits.
|
Modifier and Type | Method and Description |
---|---|
boolean |
connect(Com.BaudRates baudRate)
Open and connect to the serial port.
|
boolean |
connect(Com.BaudRates baudRate,
Com.StopBits stopBits,
Com.DataBits dataBits,
Com.Parity parity)
Open and connect to the serial port.
|
void |
disconnect()
Disconnect the connected port.
|
boolean |
isOpened()
Check if serial communication port is open.
|
int |
read(byte[] data,
int length)
Read data from the serial communication port.
|
int |
read(byte[] data,
int length,
int timeout)
Read data from the serial communication port.
|
void |
setDataAvailableCallback(DataAvailable dataAvailable)
Set data available callback for the application layer.
|
int |
write(byte[] data,
int length)
Write data to the serial communication port.
|
int |
write(byte[] data,
int length,
int timeout)
Write data to the serial communication port.
|
boolean connect(Com.BaudRates baudRate)
baudRate
- Baud rate to use.true
success, false
otherwise.boolean connect(Com.BaudRates baudRate, Com.StopBits stopBits, Com.DataBits dataBits, Com.Parity parity)
baudRate
- Baud rate to use.stopBits
- Number of stop bits.dataBits
- Number of data bits per byte.parity
- Enable parity generation and detection.true
success, false
otherwise.void disconnect()
int read(byte[] data, int length)
data
- Read buffer.length
- Length to read.int read(byte[] data, int length, int timeout)
data
- Read buffer.length
- Length to read.timeout
- Timeout for the read operation.int write(byte[] data, int length)
data
- Write buffer.length
- Length to read.int write(byte[] data, int length, int timeout)
data
- Write buffer.length
- Length to read.timeout
- Timeout for the write operation. Default 3 seconds.boolean isOpened()
true
if the serial port is open,
false
otherwise.void setDataAvailableCallback(DataAvailable dataAvailable)
dataAvailable
- Callback object that is called when data is available.