For more information about serial port communication in Linux-general Linux technology-Linux programming and kernel, see the following. In the past few days, due to the needs of the Changchun door inspection system project, the serial port reading of the card reader information has been involved, so I have some experience reading the serial port information in Linux.
1. Open the serial port
Like other device programming methods, in Linux, operations and control of serial ports are also performed by operating device files. In Linux, the serial device files are/dev/ttyS0 or/dev/ttyS1. Therefore, to read and write the serial port, we must first open the serial port:
Char * dev = "/dev/ttyS0"; // Serial Port 1
Int fd = open (dev, O_RDWR );
// | O_NOCTTY | O_NDELAY
If (-1 = fd)
{
Perror ("Can't Open Serial Port ");
Return-1;
}
Else
Return fd;
2. Set the serial port speed
After the serial port is opened successfully, we can read and write it. First, set the baud rate of the serial port:
Options. c_cc [VTIME] = 0;/* set timeout to 0 seconds */
Options. c_cc [VMIN] = 13;/* define the minimum bytes data to be readed */
If (tcsetattr (fd, TCSANOW, & options )! = 0)
{
Perror ("SetupSerial 3 ");
Return (FALSE );
}
Return (TRUE );
}
In the above Code, there are two important sentences:
Options. c_cc [VTIME] = 0;/* set timeout to 0 seconds */
Options. c_cc [VMIN] = 13;/* define the minimum bytes data to be readed */
These two statements determine the read () function of the serial port reading function. I will focus on their impact on the read () function.
The structure for serial port operations is
Struct {
Tcflag_t c_iflag;/* mark the input mode */
Tcflag_t c_oflag;/* output mode mark */
Tcflag_t c_cflag;/* Control Mode mark */
Tcflag_t c_lflag;/* mark the local mode */
Cc_t c_line;/* line procedure */
Cc_t c_cc [NCCS];/* control symbol */
};
Cc_t c_line is only used in some special system programs (such as network protocols set to communicate with tty devices. The element corresponding to two subscripts (VTIME and VMIN) in the array c_cc is not a control operator and is only valid in the original mode. Only in the original mode, they determine when the read () function returns. In standard mode, unless the O_NONBLOCK option is set, it is returned only when the file terminator or the characters of each row have been edited.
There is a complex relationship between the control operator VTIME and VMIN. The VTIME definition requires zero to several hundred milliseconds of waiting time (usually an 8-bit unsigned char variable with a value not greater than cc_t ). VMIN defines the minimum number of bytes to wait (not the number of bytes to be read-the third parameter of read () is the maximum number of bytes to be read ), the number of bytes may be 0.
L if VTIME is 0, VMIN defines the minimum number of bytes to be read. Function read () is returned only when VMIN bytes are read or a signal is received.
L if VMIN is set to 0, VTIME defines that even if no data is available, the read () function will wait several hundred milliseconds before returning. At this time, the read () function does not need to encounter a file end sign as it normally does to return 0.
L if neither VTIME nor VMIN is set to 0, VTIME defines that the waiting time is calculated after the first byte of data is received. If you can obtain data when calling the read function, the timer starts timing immediately. If no data is readable when the read function is called, the timer starts timing after the first byte of data is received. The read function may return the data of VMIN bytes after reading the data, or return the data after the timing is completed. This mainly depends on which condition is implemented first. However, the function reads at least one byte of data, because the timer starts timing when the first data is read.
L if both VTIME and VMIN are 0, the function read will return immediately even if no data is read. At the same time, the return value 0 indicates that the read function does not need to wait for the end mark of the file to return.
This is the effect of these two variables on the read function. The card reader I use transmits 13 bytes of data each time. At the beginning, I set them
Options. c_cc [VTIME] = 150
Options. c_cc [VMIN] = 0;
As a result, only 8 bytes are read each time, and the remaining 5 bytes can be received only after the next punch. This is why. According to the first rule above, I set VTIME to 0 and VMIN to 13, which is exactly equal to the number of bytes to be received at a time. In this way, 13 bytes are read at a time. At the same time, it is concluded that if the data sent by the card reader is n Bytes, VMIN = n, so that the information read at one time is exactly the information sent by the card reader, in addition, loop reading is not required during reading.
4. Read data
With the above function, I set the basic information of the serial port and set the corresponding parameters according to our actual situation to read the data.
Void getcardinfo (char * buff ){
Int fd;
Int nread, count = 0;
Char tempbuff [13];
Char * dev = "/dev/ttyS0"; // Serial Port 1
Fd = OpenDev (dev );
Set_speed (fd, 9600 );
If (set_Parity (fd, 8, 1, 'n') = FALSE ){
Printf ("Set Parity Error \ n ");
// Return-1;
}
While (1) // read data cyclically
{
Count = 0;
// Sleep (1, 5000 );
While (1)
{
If (nread = read (fd, tempbuff, 13)> 0)
{
// Printf ("\ nLen % d \ n", nread );
Memcpy (& buff [count], tempbuff, nread );
Count + = nread;
}
If (count = 13)
{
Buff [count + 1] = '\ 0 ';
// Printf ("\ n % s", buff );
Break;
}
}
// Break;
}
// Return buff;
Close (fd );
Pthread_exit (NULL );
// Close (fd );
// Exit (0 );
}
This is my original program. In fact, after setting VMIN, you can change it:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service