How to setup the serial port?
I'm using Linux OS as a programming platform. And I want to create a program that uses serial(/dev/ttyS0) port as a communication 开发者_如何转开发medium. I already know how to open, read, write and close the serial port using standard Linux function.
Now my question is how can I able to setup the serial port? What I mean is I want to change the setting values of a serial device programmatically.
Many thanks.
The man page you need to read is termios(3). It describes the POSIX functions to manipulate the TTY line discipline, which is where the serial parameters are.
You will need to call tcgetattr()
and tcsetattr()
to change the baud rate, stop bits, etc.
When dealing with device ports, it's often a good idea to use I/O with timeouts. See select()
and poll()
for that.
精彩评论