Serial Port Programming on Linux(转载)
This is a tutorial on how to program the Serial Ports on your Linux box.Serial Ports are nice little interfaces on the PC which helps you to interface your embedded system projects using a minimum number of wires.In this tutorial we will write a small program that will send and receive ASCII characters to a microcontroller development board.
One problem with the traditional serial ports is that they are now legacy hardware and are being phased out by the PC manufacturers and most laptops have USB ports only,this problem can be easily solved by using a variety of USB to Serial Converters available in the market,eg USB2SERIAL V2.0.
Please note that in this tutorial I am using a USB to Serial Converter based on FT232 from FTDI. You can use any FT232 based board ( other USB converter boards based on different chip sets should work,but i have not tested it ) or you can buy the one I am using here.
If you have traditional D subminiature (DB9) Serial Ports,just identify the relevant pins (RXD, TXD,RTS,DTR and Ground) and continue with the tutorial.
In this tutorial we are going to use the C language to program the serial port on a Linux Operating system(Ubuntu/LinuxMint).The compiler used is gcc which is freely available along with the OS.
Sourcecodes
All the C sourefiles used in this tutorial can be downloaded from our GitHub Page.If you are new to Github ?Check this article to download code .
If you are not comfortable with C ,you can check our tutorial on Serial Communication using Python (PySerial).
www.man7.org.
After you have configured all of the required bitfields of the terminos structure.You can use thetcsetattr() function to set them.tcsetattr() takes three arguments,the first and last arguments are same as in the tcgetattr()function.The second one TCSANOW tells to make the changes now without waiting.
tcsetattr(fd,TCSANOW,&SerialPortSettings)
Hardware Connections
Now we will create a small serial link between a microcontroller board and a PC running Linux OS to test out reading and writing from a PC serial port .
Microcontroller used here is MSP430G2553 from Texas instruments on Launchpad development board.Please note that you can use any microcontroller like 8051 or Atmel AVR on the embedded system side.Since this is an article about setting up and configuring the serial port on Linux,the microcontroller side code is explained here.
The PC is connected to the microcontroller board using a null modem cable.The RX of the PC (serial port) is connected to the TX of the Microcontroller and vice versa.The Grounds of PC and microcontroller are connected together.
Here my PC do not have a hardware DB9 serial port(9 pin),So i am using a FTDI based USB to Serial converter called USB2SERIAL which converts the USB signals to TTL compatible serial outputs (RXD and TXD). you can directly connect the outputs of USB2SERIAL to MSP430 microcontroller as shown in the below block diagram.USB2SERIAL also offers selectable 3V/5V TTL outputs for interfacing to3.3V logic microcontrollers like MSP430.
Please noter that if you are using a DB9 RS232 SerialPort of your PC ,you will have to build a RS232 signal level converter at the microcontroller side to decode the RS232 signal.Connecting the RS232 Lines from the PC directly to microcontroller UART directly will damage the chip.
http://xanthium.in/Serial-Port-Programming-on-Linux