Communication via serial ports
Calculator |
Before a serial port in Linux can be used, the user must have read and write rights to the corresponding device, which
is /dev/ttyS0 for COM1, /dev/ttyS1 for COM2 etc. This can sometimes be done by adding the user to the group "uucp".
Another possibility is to change the access rights of the device files with the command "chmod".
We start with a short example, which writes a textstring to serial port 1 (COM1 or /dev/ttyS0) and reads in
a text string:
#include <vflserial.h>
extern "C" bool run() {
SerialDevice d(0);
d.write("fetch?");
CharArray resp = d.read();
return true;
}
This writes the text string "fetch?" to serial port 1 and reads in some data, which is stored in the
variable resp. Of course there are many possibilities how to configure a serial port, like baud rate etc.
and this only works if the device on the serial port matches the default settings.
Now, how to configure the serial port? Most of it is done via parameters which are passed to the constructor
of SerialDevice. The SerialDevice class can automatically append termination characters and allows
specification of an input termination character. The input and output termination characters can also
be changed after the construction of a SerialDevice object using the functions SetInputEos
and SetOutputEos. After constructing a SerialDevice object the successful initialisation of the port
can be checked via the function isOk.
The SerialDevice class provides automatic locking, so that no port can be accessed by multiple threads at the same
time. Now here is the reference of the SerialDevice class:
- SerialDevice(int port = 0, tcflag_t control_flags = B9600 | CS8 | CLOCAL | CREAD,
int timeout_dezi = 20, int inputEos = 0x13,
int outputEos1 = 0x13,int outputEos2 = -1);
- Constructor of the SerialDevice object with it's default settings. port is the number of the
serial port, port 0 means the first serial port. Port configuration is done via the argument control flags.
The flags are passed to the function tcsetattr via the c_cflag argument of the termios structure.
For description of these flags look at the manpage of tcsetattr. But don't worry, normally only baud rate or the byte size
must be changed, for example B2400 instead of B9600.
timeout_dezi is the timeout for reading and writing in dezi seconds, so the default of 20 means two seconds timeout.
If something else than -1 is specified at inputEos, a read is terminated when receiving a char with this ASCII value.
The two outputEos chars are automatically appended on each string that is send with write. Set them to -1 to disable this.
- bool isOk()
- Returns true if the port could be initialized successfully
- int getfd()
- Returns the file desciptor of the serial port or -1 if it couldn't be opened. This can be used for accessing the port directly for
example to specify different parameters using tcsetattr or similar.
- void setInputEos(int eos = -1)
- This allows setting the input string terminator to a different value than specified when constructing the SerialDevice object.
- void setOutputEos(int eos1 = -1, int eos2 = -1)
- This allows setting the output string terminators to a different value than specified when constructing the SerialDevice object.
- CharArray read(int len = 1024)
- This reads a string from the serial port and returns it as a CharArray. The read is terminated when
- As many characters are read as specified by the len argument.
- An input terminator character was received. The input terminator character is not included in the returned string.
- A timeout occurred.
If an error occurred, an empty CharArray is returned.
- bool write(CharArray& data)
- This writes a string to the port. If outputEos characters were specified, they are automatically appended. The function returns
true if the write was successful and false otherwise.
Back
The Vimms User Manual