The explanation with the level of detail you are seeking would take hours to formulate. Maybe it’s best to investigate the material that other people are recommending.
A quick C++ solution would be to use a ready-to-read/write serial library. If this is acceptable to you, the qt framework brings a multi-platform solution for very interesting serial communication.
The Qt documentation provides an example called Terminal, which shows how to identify the serial ports on the computer, connect to one of them and print the data on the console. Unfortunately, this example presents a GUI (Graphical User Interface) and that makes the sample code become a little bit swollen.
A few months ago I decided to remove all this part of UI and ended up providing a simpler and streamlined example on Github called Qtserial.
Basically, the process for reading from a serial port using Qt consists of:
List serial computer ports: the class QSerialPortInfo
offers static methods for this and provides information on each serial port found, such as location, manufacturer identification, product identification, etc.
Connect to a serial port: just instilling an object of the type QSerialPort
configure some items like: the port name, the baud rate, whether it has parity or not, the type of flow control and other things more.
Read from serial port: to accomplish this task you need to monitor 2 Signals of the object QSerialPort
unstable: readyRead()
and error(QSerialPort::SerialPortError)
. To do this you must declare a subclass of QObject
and implement both 2 slots which will be triggered automatically when these Signals happen.
In other words, readyRead()
is the signal sent by the object QSerialPort
when there is serial data to be received by your program. The signal error()
, of course, it is issued only in case of failure during communication.
An essential operation to ensure reading success is to invoke setDataTerminalReady(true)
. There are several questions in the OS from people who can’t make the reading work properly because they forgot to call this method.
Well, to do the reverse and send data by serial the process is easier. Just adjust the method call QSerialPort::open()
to request read and write permission on the serial port:
serial.open(QIODevice::ReadWrite);
To send data, run QSerialPort:write()
or QSerialPort:putChar()
. Meanwhile, write()
does not block execution and therefore returns immediately. Thus, when data is effectively sent by the serial port the Signal bytesWritten()
is issued. So be sure to implement a slot to connect to this signal and ensure that everything your program tried to send was actually submitted to the serial.
Well, I tested the Qtserial with more than one Arduino and with other devices. Please make a Fork from my repository and when encountering problems in this application request a pull request
. I’ll be happy to add your changes to this project.
I’ve never tried programming for serial communication, but, if you know English, Serial Programming HOWTO maybe help something.
– pmg
Thanks, I’ll give you a read. This will certainly be of great help.
– Avelino
This link is a good reference for serial port communication using Boost.Asio: http://www.webalice.it/fede.tft/serial_port/serial_port.html
– pepper_chico