Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
cRS232 Class Reference

Low-level communication class to access a serial port on Cygwin and Linux. More...

#include <rs232-cygwin.h>

Inheritance diagram for cRS232:
Inheritance graph
[legend]

Public Member Functions

void Close (void)
 Close the previously opened communication channel. More...
 
void Close (void)
 Close the previously opened rs232 port. More...
 
 cRS232 (int _port, unsigned long _baudrate, double _timeout, char const *_device_format_string="")
 
 cRS232 (int _port, unsigned long _baudrate, double _timeout, char const *_device_format_string="/dev/ttyS%d")
 
bool IsOpen () throw ()
 Return true if communication channel is open. More...
 
bool IsOpen (void) throw ()
 Return true if port to RS232 is open. More...
 
void Open (void)
 Open rs232 port port. More...
 
void Open (void)
 
ssize_t Read (void *data, ssize_t size, long timeout_us, bool return_on_less_data)
 
ssize_t Read (void *data, ssize_t size, long timeout_us, bool return_on_less_data)
 
char * readline (char *line, int size, char *eol, bool return_on_less_data)
 
virtual void SetTimeout (double _timeout)
 set the timeout for next readline() calls (negative value means: no timeout, wait for ever) More...
 
virtual bool UseCRC16 ()
 overloaded from cSerialBase::UseCRC16 since we want to use a CRC16 to protect binary RS232 communication More...
 
virtual bool UseCRC16 ()
 overloaded from cSerialBase::UseCRC16 since we want to use a CRC16 to protect binary RS232 communication More...
 
int write (char const *ptr, int len=0)
 Write data to a previously opened port. More...
 
int write (char const *ptr, int len=0)
 Write data to a previously opened port. More...
 
 ~cRS232 (void)
 
- Public Member Functions inherited from cSerialBase
 cSerialBase ()
 ctor More...
 
virtual char const * GetErrorMessage (tErrorCode dw)
 
virtual tErrorCode GetErrorNumber ()
 
char const * GetLastErrorMessage (void)
 return the last error message as string. The string returned will be overwritten by the next call to the function More...
 
virtual double GetTimeout ()
 get the timeout for next readline() calls (negative value means: no timeout, wait for ever) More...
 
virtual char * readline (char *line, int size, char const *eol="\n", bool return_on_less_data=false)
 Read a line from the device. More...
 
virtual ~cSerialBase (void)
 dtor More...
 

Protected Member Functions

tcflag_t BaudrateToBaudrateCode (unsigned long baudrate)
 Translate a baudrate given as unsigned long into a baudrate code for struct termios. More...
 

Protected Attributes

unsigned long baudrate
 the baudrate in bit/s More...
 
std::string device_format_string
 the sprintf format string to generate the device name from the port, see Constructor More...
 
int fd
 the file descriptor of the RS232 port More...
 
termios io_set_old
 
int port
 the RS232 portnumber to use More...
 
int status
 
- Protected Attributes inherited from cSerialBase
double timeout
 timeout in seconds More...
 
char ungetch
 an already read data byte of the next line More...
 
bool ungetch_valid
 Flag, true if ungetch is valid. More...
 

Private Attributes

HANDLE _hCOM
 
COMMTIMEOUTS comm_timeouts
 
long read_timeout_us
 

Additional Inherited Members

- Public Types inherited from cSerialBase
typedef int tErrorCode
 type of the error code, DWORD on windows and int on Linux/cygwin More...
 
- Public Attributes inherited from cSerialBase
cDBG dbg
 A stream object to print colored debug messages. More...
 

Detailed Description

Low-level communication class to access a serial port on Cygwin and Linux.

Low-level communication class to access a serial port on VCC Windows.

Definition at line 84 of file rs232-cygwin.h.

Constructor & Destructor Documentation

◆ cRS232() [1/2]

cRS232::cRS232 ( int  _port,
unsigned long  _baudrate,
double  _timeout,
char const *  _device_format_string = "/dev/ttyS%d" 
)

Constructor: constructs an object to communicate with an SDH via RS232

Parameters
_port- rs232 device number: 0='COM1'='/dev/ttyS0', 1='COM2'='/dev/ttyS1', ...
_baudrate- the baudrate in bit/s
_timeout- the timeout in seconds
_device_format_string- a format string (C string) for generating the device name, like "/dev/ttyS%d" (default) or "/dev/ttyUSB%d". Must contain a d where the port number should be inserted. This char array is duplicated on construction

Definition at line 107 of file rs232-cygwin.cpp.

◆ cRS232() [2/2]

cRS232::cRS232 ( int  _port,
unsigned long  _baudrate,
double  _timeout,
char const *  _device_format_string = "" 
)

Constructor: constructs an object to communicate with an SDH via RS232

Parameters
_port- rs232 device number: 0='COM1'='/dev/ttyS0', 1='COM2'='/dev/ttyS1', ...
_baudrate- the baudrate in bit/s
_timeout- the timeout in seconds
_device_format_string- ignored for this VCC version

◆ ~cRS232()

cRS232::~cRS232 ( void  )

Definition at line 107 of file rs232-vcc.cpp.

Member Function Documentation

◆ BaudrateToBaudrateCode()

tcflag_t cRS232::BaudrateToBaudrateCode ( unsigned long  baudrate)
protected

Translate a baudrate given as unsigned long into a baudrate code for struct termios.

Definition at line 258 of file rs232-cygwin.cpp.

◆ Close() [1/2]

void cRS232::Close ( void  )
virtual

Close the previously opened communication channel.

Implements cSerialBase.

◆ Close() [2/2]

void cRS232::Close ( void  )
virtual

Close the previously opened rs232 port.

Implements cSerialBase.

Definition at line 248 of file rs232-cygwin.cpp.

◆ IsOpen() [1/2]

bool cRS232::IsOpen ( )
throw (
)
inlinevirtual

Return true if communication channel is open.

Implements cSerialBase.

Definition at line 109 of file rs232-vcc.h.

◆ IsOpen() [2/2]

bool cRS232::IsOpen ( void  )
throw (
)
virtual

Return true if port to RS232 is open.

Implements cSerialBase.

Definition at line 240 of file rs232-cygwin.cpp.

◆ Open() [1/2]

void cRS232::Open ( void  )
virtual

Open rs232 port port.

Open the device with the parameters provided in the constructor

Implements cSerialBase.

◆ Open() [2/2]

void cRS232::Open ( void  )
virtual

Open the device as configured by the parameters given to the constructor

Bug:
The binary communication introduced in firmware 0.0.2.15 and SDHLibrary-C++ 0.0.2.0 did not work properly on Linux when RS232 was used for communication.
See also:

Implements cSerialBase.

Definition at line 121 of file rs232-cygwin.cpp.

◆ Read() [1/2]

ssize_t cRS232::Read ( void *  data,
ssize_t  size,
long  timeout_us,
bool  return_on_less_data 
)
virtual

Read data from device. This function waits until max_time_us us passed or the expected number of bytes are received via serial line. if (return_on_less_data is true (default value), the number of bytes that have been received are returned and the data is stored in data If the return_on_less_data is false, data is only read from serial line, if at least size bytes are available.

Implements cSerialBase.

◆ Read() [2/2]

ssize_t cRS232::Read ( void *  data,
ssize_t  size,
long  timeout_us,
bool  return_on_less_data 
)
virtual

Read data from device. This function waits until max_time_us us passed or the expected number of bytes are received via serial line. if (return_on_less_data is true (default value), the number of bytes that have been received are returned and the data is stored in data If the return_on_less_data is false, data is only read from serial line, if at least size bytes are available.

Implements cSerialBase.

Definition at line 341 of file rs232-cygwin.cpp.

◆ readline()

char * cRS232::readline ( char *  line,
int  size,
char *  eol,
bool  return_on_less_data 
)

Definition at line 292 of file rs232-vcc.cpp.

◆ SetTimeout()

void cRS232::SetTimeout ( double  _timeout)
virtual

set the timeout for next readline() calls (negative value means: no timeout, wait for ever)

Reimplemented from cSerialBase.

Definition at line 183 of file rs232-vcc.cpp.

◆ UseCRC16() [1/2]

virtual bool cRS232::UseCRC16 ( )
inlinevirtual

overloaded from cSerialBase::UseCRC16 since we want to use a CRC16 to protect binary RS232 communication

Reimplemented from cSerialBase.

Definition at line 130 of file rs232-vcc.h.

◆ UseCRC16() [2/2]

virtual bool cRS232::UseCRC16 ( )
inlinevirtual

overloaded from cSerialBase::UseCRC16 since we want to use a CRC16 to protect binary RS232 communication

Reimplemented from cSerialBase.

Definition at line 163 of file rs232-cygwin.h.

◆ write() [1/2]

int cRS232::write ( char const *  ptr,
int  len = 0 
)
virtual

Write data to a previously opened port.

Write len bytes from *ptr to the rs232 device

Parameters
ptr- pointer the byte array to send in memory
len- number of bytes to send
Returns
the number of bytes actually written

Implements cSerialBase.

◆ write() [2/2]

int cRS232::write ( char const *  ptr,
int  len = 0 
)
virtual

Write data to a previously opened port.

Write len bytes from *ptr to the rs232 device

Parameters
ptr- pointer the byte array to send in memory
len- number of bytes to send
Returns
the number of bytes actually written

!! dwWritten is always 0! Damn bloody windows

Implements cSerialBase.

Definition at line 321 of file rs232-cygwin.cpp.

Member Data Documentation

◆ _hCOM

HANDLE cRS232::_hCOM
private

Definition at line 75 of file rs232-vcc.h.

◆ baudrate

unsigned long cRS232::baudrate
protected

the baudrate in bit/s

Definition at line 95 of file rs232-cygwin.h.

◆ comm_timeouts

COMMTIMEOUTS cRS232::comm_timeouts
private

Definition at line 80 of file rs232-vcc.h.

◆ device_format_string

std::string cRS232::device_format_string
protected

the sprintf format string to generate the device name from the port, see Constructor

Definition at line 92 of file rs232-cygwin.h.

◆ fd

int cRS232::fd
protected

the file descriptor of the RS232 port

Definition at line 98 of file rs232-cygwin.h.

◆ io_set_old

termios cRS232::io_set_old
protected

Definition at line 105 of file rs232-cygwin.h.

◆ port

int cRS232::port
protected

the RS232 portnumber to use

the RS232 port number to use (port 0 is COM1)

Definition at line 89 of file rs232-cygwin.h.

◆ read_timeout_us

long cRS232::read_timeout_us
private

Definition at line 81 of file rs232-vcc.h.

◆ status

int cRS232::status
protected

Definition at line 103 of file rs232-cygwin.h.


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


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Wed Mar 2 2022 01:00:59