VB Serial Communications Class


Topics:

OverviewOverview
Conditional Directives
Enumerations
Functions


Overview

The vbSerialComm class is a base class used to open a serial port for bi-directional communication. It includes several low-level functions needed by derived classes to initialize a serial port, transmit, and receive data.


Conditional Directives

__UNIX__ - Conditional directive required for all UNIX variants.

__WIN32__ - Conditional directive required for all WIN32 platforms


Enumerations

enum {
  // Internal error codes used to report the serial port last error
  vbSerialComm::scomm_NO_ERROR  = 0,      // No errors reported
  vbSerialComm::scomm_INVALID_ERROR_CODE, // Invalid error code

  vbSerialComm::scomm_BAUDRATE_ERROR,    // Invalid baud rate
  vbSerialComm::scomm_CS_ERROR,          // Invalid character size
  vbSerialComm::scomm_FLOWCONTROL_ERROR, // Invalid flow control
  vbSerialComm::scomm_INIT_ERROR,        // Initialization error
  vbSerialComm::scomm_INVALIDPARM,       // Invalid parameter
  vbSerialComm::scomm_OPEN_ERROR,        // Cannot open serial device
  vbSerialComm::scomm_PARITY_ERROR,      // Invalid parity
  vbSerialComm::scomm_RECEIVE_ERROR,     // Serial device receive error
  vbSerialComm::scomm_STOPBIT_ERROR,     // Invalid stop bit
  vbSerialComm::scomm_TRANSMIT_ERROR,    // Transmit error

  // Exception codes added to handle variable block errors
  vbSerialComm::scomm_BLOCKACK_ERROR,    // Acknowledgment error
  vbSerialComm::scomm_BLOCKHEADER_ERROR, // Bad variable block header
  vbSerialComm::scomm_BLOCKSIZE_ERROR,   // Bad variable block size
  vbSerialComm::scomm_BLOCKSYNC_ERROR    // Synchronization error
 };
  
enum {
  // Flow control constants
  vbSerialComm::scommHARD_FLOW,
  vbSerialComm::scommSOFT_FLOW,
  vbSerialComm::scommXON_XOFF,
  vbSerialComm::scommNO_FLOW_CONTROL,

  // Device access constants
  vbSerialComm::scommREAD_ONLY,
  vbSerialComm::scommWRITE_ONLY,
  vbSerialComm::scommREAD_WRITE
 };


Functions

vbSerialComm::vbSerialComm()
vbSerialComm::~vbSerialComm()
vbSerialComm::BinaryMode()
vbSerialComm::BytesMoved()
vbSerialComm::BytesRead()
vbSerialComm::CharacterMode()
vbSerialComm::Close()
vbSerialComm::DeviceHandle()
vbSerialComm::GetSerialCommError()
vbSerialComm::InitSerialPort()
vbSerialComm::OpenSerialPort()
vbSerialComm::RawRead()
vbSerialComm::RawWrite()
vbSerialComm::Recv()
vbSerialComm::ResetError()
vbSerialComm::ResetSerialCommError()
vbSerialComm::Send()
vbSerialComm::SerialCommExceptionMessage()
vbSerialComm::SetBaudRate()
vbSerialComm::SetCharacterSize()
vbSerialComm::SetFlowControl()
vbSerialComm::SetParity()
vbSerialComm::SetSerialCommError()
vbSerialComm::SetStopBits()
vbSerialComm::Send()

vbSerialComm::vbSerialComm() - Default class constructor.

virtual vbSerialComm::~vbSerialComm() - Class destructor used to automatically close an open serial device when a vbSerialComm object is deleted.

void vbSerialComm::BinaryMode() - Public member function used to toggle from character mode to binary mode. NOTE: This function has no effect under WIN32. The Win32 API does not support non-binary mode transfers.

int vbSerialComm::BytesMoved() - Public member function that returns the number of bytes moved following a write operation.

int vbSerialComm::BytesRead() - Public member function that returns the number of bytes read following a read operation.

void vbSerialComm::CharacterMode() - Public member function used to toggle from binary mode to character mode. NOTE: This function has no effect under WIN32. The Win32 API does not support non-binary mode transfers.

void vbSerialComm::Close() - Public member function used to close a previously opened serial device.

scommDeviceHandle vbSerialComm::DeviceHandle() - Public member function that returns the device handle for the currently opened serial device.

int vbSerialComm::GetSerialCommError() - Public member function used to retrieve that last reported serial port error. The return value will correspond to one of the integer constants defined in the error code enumeration.

int vbSerialComm::GetSerialCommError() - Public member function used to retrieve that last reported serial port error. The return value will correspond to one of the integer constants defined in the error code enumeration.

int vbSerialComm::InitSerialPort() - Public member function used to initialize a serial device. Returns zero to indicate success or -1 to indicate a serial port error.

int vbSerialComm::InitSerialPort(char *device_name, int sp = 9600, char pr = 'N', int cs = 8, int sb = 1, int flow = vbSerialComm::scommNO_FLOW_CONTROL,int bin_mode = 1) - Public member function used to initialize a serial device. Returns zero to indicate success or -1 to indicate a serial port error.

int vbSerialComm::OpenSerialPort(char *device_name) - Public member function to open a serial port for read/write operations depending on the device file permissions. This function will try to open the device for read/write, read, and then write access. Returns -1 if the device cannot be opened. This function will return a non-zero value corresponding to one of the integer constants defined in the device access enumeration if the serial device is opened successfully.

int vbSerialComm::RawRead(void *buf, int bytes) - Public member function used read a specified number of bytes from the serial port and return whether or not the read was completed. Returns the number of bytes received or -1 if an error occurred.

int vbSerialComm::RawWrite(const void *buf, int bytes) - Public member function used to write a specified number of bytes to a serial port and return whether or not the write was complete. Returns the total number of bytes moved or -1 if an error occurred.

int vbSerialComm::Recv(void *buf, int bytes) - Public member function used to receive a specified number of bytes from a serial port and does not return until all the byte have been read. Returns the total number of bytes read or -1 if an error occurred.

void vbSerialComm::ResetError() - Public member function used to clear the last reported serial device error.

void vbSerialComm::ResetSerialCommError() - Public member function used to clear the last reported serial device error.

int vbSerialComm::Send(const void *buf, int bytes) - Public member function used to write a specified number of bytes to a serial port and does not return until all the bytes have been written. Returns the total number of bytes written or -1 if an error occurred.

const char *vbSerialComm::SerialCommExceptionMessage() - Public member function that returns a null-terminated string that can be use to log or print a serial port exception.

void vbSerialComm::SetBaudRate(int br) - Public member function used to set the baud rate prior to initialization.

void vbSerialComm::SetCharacterSize(int cs) - Public member function used to set the character size prior to initialization.

void vbSerialComm::SetFlowControl(int f) - Public member function used to set the flow control prior to initialization. The "f" variable must equal one of the integer constants defined in the flow control enumeration.

void vbSerialComm::SetParity(char p) - Public member function used to set the parity prior to initialization.

void vbSerialComm::SetSerialCommError(int err) - Public member function used to set the serial device error code. The "err" variable must equal one of the integer constants defined in the error code enumeration.

void vbSerialComm::SetStopBits(int sb) - Public member function used to set the stop bits prior to initialization.

int vbSerialComm::Send(const void *buf, int bytes) - Public member function used to write a specified number of bytes to a serial port and does not return until all the bytes have been written. Returns the total number of bytes written or -1 if an error occurred.


End Of Document