Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serial Port Communication C++ Linux [closed]

I'm looking for a easy to use C++ librairie to dialog with Serial Port under Linux.

I looked at Boost::Asio but it look like very complicated for my little usage. I just want to received some information on the Serial Port and record them in a database.

Do you know a simple Serial Port librairie (with a example it would be the best)

Thanks

like image 989
F4T4liS Avatar asked May 07 '14 13:05

F4T4liS


People also ask

How do I fix a serial port problem?

Go to Device Manager > Ports (COM & LPT) > mbed Serial Port, then right-click and select "properties". Choose "Port Settings" Tab, and click "Advanced" Under "COM Port Number", try selecting a different COM port. Unplug and replug the mbed to reload the driver - if the problem persists, try another COM port.

What is COM1 in Linux?

Linux names its serial ports in the UNIX tradition. The first serial port has the file name /dev/ttyS0, the second serial port has the file name /dev/ttyS1, and so on. This differs from the IBM PC tradition. The first serial port is named COM1:, the second serial port is named COM2:, and so on.

Does Linux use COM ports?

Linux uses ttySx for a serial port device name. For example, COM1 (DOS/Windows name) is ttyS0, COM2 is ttyS1, and so on.


3 Answers

Boost.Asio is really a good one. The problem is that its documentation is too complex and arranged erratically. If you just need to do the simplest serial port programming, you don't need to use all the advanced features.

Example usage of blocking I/O on serial port.

static boost::asio::io_service ios;
boost::asio::serial_port sp(ios, "/dev/ttyS2");
sp.set_option(boost::asio::serial_port::baud_rate(115200));
// You can set other options using similar syntax
char tmp[64];
auto length = sp.read_some(boost::asio::buffer(tmp));
// process the info received
std::string message = "hello, world";
sp.write_some(boost::asio::buffer(message));
sp.close();
like image 146
Siyuan Ren Avatar answered Oct 04 '22 14:10

Siyuan Ren


RS232 is used for serial communication. You may refer this link or you can try Boost Asio's serial ports and compile that into a library to be linked with your C application. It claims to be POSIX compatible, and OSX is POSIX.

like image 40
Rocoder Avatar answered Oct 04 '22 13:10

Rocoder


Most of what you need will be in the termios.h header. Take a look here.

like image 32
doron Avatar answered Oct 04 '22 13:10

doron