Public Member Functions | Private Member Functions | Private Attributes

di_serial_io Class Reference

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.

Detailed Description

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.


Constructor & Destructor Documentation

di_serial_io::di_serial_io (  )

Definition at line 33 of file di_serial_io.cpp.

di_serial_io::~di_serial_io (  )

Definition at line 41 of file di_serial_io.cpp.

Here is the call graph for this function:


Member Function Documentation

const int16_t di_serial_io::bytes_in_receive (  )
Precondition:
Connected. Does not check!
Returns:
Number of bytes in the receive buffer.

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.

Parameters:
dev_fileDevice file path and name.
deviceDevice code for specific settings. See file comment.
Precondition:
Inactive connection. Does not check!
Postcondition:
Connection to device file, port set up.
Remarks:
This only applies settings, nothing else. It doesn't lock the device file or check if it is locked. It quits on any errors.
Returns:
Errors returned by open()
Errors returned by tcgetattr()
Errors returned by cfsetispeed()
Errors returned by cfsetospeed()
Errors returned by tcsetattr()
0 = No error.

Definition at line 64 of file di_serial_io.cpp.

Here is the call graph for this function:

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.

Precondition:
Connected. Does not check!
Remarks:
The beginning NULL must be in expected.
Parameters:
expectedPointer to array of expected data.
amountNumber of bytes to check.
Returns:
True = Echo matched.
False = Echo did not match.

Definition at line 527 of file di_serial_io.cpp.

Here is the call graph for this function:

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.

Parameters:
dataThe data read.
amountThe number of bytes to try and read.
packet_lenLength of normal packet/scan (bytes). If this value is zero, validation is not performed.
Precondition:
Connected. Does not check!
Returns:
True = Error occurred.
False = No error.

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.

Here is the call graph for this function:

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.

Precondition:
Connected. Does not check!
Parameters:
dataPointer to array of data to send.
amountNumber of bytes to send.
echoNumber of bytes to check for echo (except NULL).
retryNumber of times to try checking echo. Value of zero will prevent the entire send from occurring.
Returns:
True = Error occurred.
False = No error.

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.

Here is the call graph for this function:

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.

Precondition:
Connected. Does not check!
Parameters:
dataPointer to array where to store next 'packet'.
data_lenLength of data.
packet_lenLength of a normal packet/scan (bytes).
Returns:
False = Synchronized successfully.
True = Error occurred.

Definition at line 495 of file di_serial_io.cpp.

Here is the call graph for this function:

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'

Parameters:
dataThe 'packet' to test.
packet_lenNormal packet size.
Returns:
True = 'Packet' is valid.
False = Failed validation test.

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.

Precondition:
Connected. Does not check!
Postcondition:
Not connected.
Remarks:
This function attempts to reset the serial port settings. Calling it before connect() will result in undefined settings.
Returns:
Errors returned by close().
0 = No error.

Definition at line 232 of file di_serial_io.cpp.

Here is the call graph for this function:

void di_serial_io::flush_receive (  )
Precondition:
Connected. Does not check!
Remarks:
No error checking.

Definition at line 442 of file di_serial_io.cpp.

Here is the call graph for this function:

const bool di_serial_io::is_comm_open (  )
Returns:
True = Connected.
False = Not connected.

Definition at line 258 of file di_serial_io.cpp.


Field Documentation

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.

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.


The documentation for this class was generated from the following files: