Wednesday, May 31, 2017

Programming serial port in C++ with wxWidgets for Windows and Linux

We would like to present a class library 'serial.h' developed by us to use serial port (com port) on both Windows and Linux. This cross-platform 'Serial' class is written in C++. A simple example for C++ console program is demonstrated. Using it with wxWidgets for GUI application is also presented. The source code can be found at

https://github.com/yan9a/serial


Figure. A wxWidgets GUI application using 'Serial' class with Visual Studio 2017


A simple example console program using 'Serial' class is shown below.
#include < stdio.h >
#include "serial.h"
using namespace std;
int main()
{
 Serial com(3,9600);//initialize port 3 , baud rate= 9600


 printf("Opening port %ld.\n",com.GetPortNumber());
 long r = com.Open();//return 0 if success

 bool successFlag;
 printf("Writing.\n");
 char s[]="Hello";
 successFlag=com.Write(s);//write a string
 successFlag=com.WriteChar('!');//write a character

 printf("Waiting 3 seconds.\n");
 delay(3000);//delay 3 sec to wait for a character

 printf("Reading.\n");
 char c=com.ReadChar(successFlag);//read a char
 if(successFlag) printf("Rx: %c\n",c);

printf("Closing port %ld.\n",com.GetPortNumber());
 com.Close();
return 0;
}

The statement #include "serial.h" is declared to use the class. Then, an object called 'com' is declared with two parameters - port number and baud rate. For this example, it will be COM3 on Windows and ttyS3 on Linux. For a USB device on Linux, add 100 to the port number. For example, port number 100 corresponds to ttyUSB0.
The com port is opened using com.Open() statement. Use Write or WriteChar methods to send a null terminated string or a character from the serial port. ReadChar method reads a character from the serial port in non-blocking way. If there is no received character, the successFlag will be false. Close method closes the com port. It is also possilbe to control RTS and DTR lines. Status lines such as CTS and DSR can also be read.
The above example program "serialcon.cpp", can be compiled and run on Ubuntu Linux using the following commands.
g++ serialcon.cpp serial.h -o serialcon
sudo ./serialcon

On Windows, it can be complied and run with Visual Studio or a compiler such as tdm-gcc, mingw as follows.
g++ serialcon.cpp serial.h -o serialcon.exe -std=c++11
.\serialcon.exe

The source code also include an example wxWidgets GUI application - serial.cpp. The figure above illustrates using it with Visual C++ 2017.
Without modifying, this serial.cpp program can also be built and run on Ubuntu (16.04 in my case) using the following commands.
g++ serial.cpp serial.h  `wx-config --cxxflags --libs` -o serial -DNDEBUG
sudo ./serial


For the above commands to work, there should be wxWdigets installed in the machine. And installing wxWidgets on Linux is straightforward.
sudo apt-get install build-essential
sudo apt-get install libwxgtk3.0-dev


Running serial application on Ubuntu


The following link is an old article by us about using PC serial port with VB6 and Visual Basic 2005.

Related post:

No comments:

Post a Comment

Comments are moderated and don't be surprised if your comment does not appear promptly.