Class to handle serial type connections. More...
#include <di_serial_io.h>
Public Member Functions | |
const int16_t | bytes_in_receive () |
Returns the number of bytes in the input buffer. | |
const u_int16_t | connect (const char *const dev_file, const u_int8_t device) |
Connect to device and set up serial port settings. | |
const bool | di_read (u_int8_t *data, u_int16_t &amount, const u_int8_t packet_len=2) |
Read data from serial connection, validate if requested. | |
const bool | di_send (const u_int8_t *const data, u_int16_t &amount, const u_int8_t echo=2, const u_int8_t retry=4) |
Send data to device. | |
di_serial_io () | |
Simple initialization. | |
const u_int16_t | disconnect () |
Restore original serial port settings and disconnect from device. | |
void | flush_receive () |
Clears input buffers. | |
const bool | is_comm_open () |
Determines whether the serial connection is active. | |
~di_serial_io () | |
Close connection. | |
Private Member Functions | |
const bool | di_echo (const u_int8_t *const expected, const u_int8_t amount) |
Returns true if the 'expected' data is echoed (except NULL). | |
const bool | di_synchronize (u_int8_t *data, const u_int8_t data_len, const u_int8_t packet_len) |
Attempts to synchronize the input buffer to the beginning of a packet. | |
const bool | di_valid (const u_int8_t *const data, const u_int8_t packet_len) |
True when packet matches DI signature. | |
Private Attributes | |
unsigned int | m_baudrate |
Connection baudrate. | |
int | m_comm_fd |
Serial port file descriptor. | |
struct termios | m_old_termios |
Original serial port settings. | |
int | m_old_tiocm |
Other original settings. | |
u_int8_t | m_timeout |
Timeout in seconds. |
This class handles setting up the port settings, connecting to the device, providing validated reads, send out data, open status, available data points, and some device controlled errors.
Definition at line 46 of file di_serial_io.h.
di_serial_io::di_serial_io | ( | ) |
Definition at line 33 of file di_serial_io.cpp.
di_serial_io::~di_serial_io | ( | ) |
const int16_t di_serial_io::bytes_in_receive | ( | ) |
Definition at line 430 of file di_serial_io.cpp.
const u_int16_t di_serial_io::connect | ( | const char *const | dev_file, |
const u_int8_t | device | ||
) |
Returns error codes of internal functions it calls.
dev_file | Device file path and name. |
device | Device code for specific settings. See file comment. |
Definition at line 64 of file di_serial_io.cpp.
const bool di_serial_io::di_echo | ( | const u_int8_t *const | expected, |
const u_int8_t | amount | ||
) | [private] |
Checks if 'amount' bytes of 'expected' are echoed by the device. The function automatically excludes the beginning NULL.
expected | Pointer to array of expected data. |
amount | Number of bytes to check. |
Definition at line 527 of file di_serial_io.cpp.
const bool di_serial_io::di_read | ( | u_int8_t * | data, |
u_int16_t & | amount, | ||
const u_int8_t | packet_len = 2 |
||
) |
Return error codes of internal functions. Function will block until either enough data was read or it timed out, according to m_timeout.
data | The data read. |
amount | The number of bytes to try and read. |
packet_len | Length of normal packet/scan (bytes). If this value is zero, validation is not performed. |
Error codes set:
ENODATA = Not enough data available.
Errors returned by read().
ETIMEDOUT = Timed out before getting requested amount.
Errors set by di_serial_io::di_synchronize().
0 = No error.
Definition at line 285 of file di_serial_io.cpp.
const bool di_serial_io::di_send | ( | const u_int8_t *const | data, |
u_int16_t & | amount, | ||
const u_int8_t | echo = 2 , |
||
const u_int8_t | retry = 4 |
||
) |
Flushes output buffer before every command. Blocks slightly if device signals its receive buffer is full.
data | Pointer to array of data to send. |
amount | Number of bytes to send. |
echo | Number of bytes to check for echo (except NULL). |
retry | Number of times to try checking echo. Value of zero will prevent the entire send from occurring. |
Error codes set:
Errors set by write().
EBADRSVP = Device not responding properly. Bad echo.
0 = No error.
Definition at line 367 of file di_serial_io.cpp.
const bool di_serial_io::di_synchronize | ( | u_int8_t * | data, |
const u_int8_t | data_len, | ||
const u_int8_t | packet_len | ||
) | [private] |
Attempts to synchronize the input buffer to the beginning of a packet, it stores what it thinks is a packet in 'data' while at it. Uses the read function without any validation checking.
data | Pointer to array where to store next 'packet'. |
data_len | Length of data. |
packet_len | Length of a normal packet/scan (bytes). |
Definition at line 495 of file di_serial_io.cpp.
const bool di_serial_io::di_valid | ( | const u_int8_t *const | data, |
const u_int8_t | packet_len | ||
) | [private] |
Current signature format is as follows:
0 = Last bit of first byte in 'packet'
1 = Last bit of remaining bytes in 'packet'
data | The 'packet' to test. |
packet_len | Normal packet size. |
Definition at line 466 of file di_serial_io.cpp.
const u_int16_t di_serial_io::disconnect | ( | ) |
Disconnects at all costs. Returns error codes of internal functions.
Definition at line 232 of file di_serial_io.cpp.
void di_serial_io::flush_receive | ( | ) |
Definition at line 442 of file di_serial_io.cpp.
const bool di_serial_io::is_comm_open | ( | ) |
Definition at line 258 of file di_serial_io.cpp.
unsigned int di_serial_io::m_baudrate [private] |
Definition at line 83 of file di_serial_io.h.
int di_serial_io::m_comm_fd [private] |
Definition at line 84 of file di_serial_io.h.
struct termios di_serial_io::m_old_termios [private] |
Definition at line 85 of file di_serial_io.h.
int di_serial_io::m_old_tiocm [private] |
Definition at line 86 of file di_serial_io.h.
u_int8_t di_serial_io::m_timeout [private] |
Definition at line 87 of file di_serial_io.h.