In Linux all devices have a file in /dev directory, so the communication with these devices is very simple, just need to open necessary file, and make read and write operations upon them.
In this article is shown how to connect to serial port in Linux, using c++.
First of all we include all needed libraries, and declare all necessary variables:
Next step, we connect to device through associated file and check the connection:
fd1=open(“/dev/ttyS0″, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd1 == -1 )
perror(“open_port: Unable to open /dev/ttyS0 – “);
printf(“Port 1 has been sucessfully opened and %d is the file description\n”,fd1);
Where, “/dev/ttyS0” is associated with COM1 port.
With following code we send to device some bits:
And for reading response from device:
printf(“Bytes sent are %d \n”,rd);
At the end, close the connection: