How does serial communication work and how do I use C/C++?

Asked

Viewed 9,428 times

8

I use the Debian 7.1 and need to make serial communication between a computer and a microcontroller using C/C++. I’ve done a lot of research on the subject, but the articles and examples I found are very confusing, unclear and not very customizable.

I would like to understand how serial communication works, if possible at hardware and operating system level, as well as understand how I can implement it in C/C++ or some other low/medium level language (so I can understand the integration with hardware and operating system in detail).

I am not asking for ready-made codes (although codes would be of great help), but guidelines on how I should proceed to do so and an explanation of how this process works. It can be through an API or library.

  • 3

    I’ve never tried programming for serial communication, but, if you know English, Serial Programming HOWTO maybe help something.

  • Thanks, I’ll give you a read. This will certainly be of great help.

  • 1

    This link is a good reference for serial port communication using Boost.Asio: http://www.webalice.it/fede.tft/serial_port/serial_port.html

3 answers

7


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.

  • 1

    Perfect! Very grateful for the answer, it was something like this I was looking for. I will test your application and the example that Qt offers over the next few weeks.

5

Apparently you are asking about the implementation of serial communication instead of simply asking how it is used.

Good, for this recommend a good book of Linux drivers, recommend the Linux Device Drivers, specifically the chapter on TTY drivers should be of interest to you.

The fourth edition of this book is on its way.

In relation to hardware, the English page of the Wikipédia referring to the RS-232 standard and the page about serial doors has plenty of information. Another with plenty of information about is in Wikibooks.

If in the end you don’t want to go into this level of detail (which would be unnecessary to carry out a simple communication with a device), follow the links passed to you in the comments of your question, this is one more.

3

The issue is a little complicated.

In the theory read and write to a serial port is equal read/write to any other file; the difference is its name ( /dev/ttyS0 for the first serial, /dev/ttyS1 for the second and so on).

The same problem is to configure the communication before sending/receiving the data; I like to configure via termios but I have already seen applications that call the setserial via system() to configure before opening the port.

Take a look at Linux Serial Programming.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.