Linux serial port multi-thread read/write-Linux general technology-Linux technology and application information. The following is a detailed description. I started two threads to operate the serial port: one for reading data and the other for writing data. This program is used for full-duplex serial communication between two ARM9. a serial line is used to connect the two boards.
The problem is: I don't know where to open the serial port. If it is opened in the main thread, both reading threads receive garbled code, at the same time, it can also detect exceptions in the characters sent by the writing thread. If the reading and writing threads open and close the serial port separately, the writing thread can issue characters, but the reading thread cannot read data at all times. Is there a problem with my thread?
At present, I am in great distress. I have posted the following code to help you:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define NUM 20
Int fd;
Void writeThread (void * arg );
Void readThread (void * arg );
/*
Int openport (void)
{
// Int fd;
Struct termios options;
If (fd = open ("/dev/tts/0", O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY) =-1)
{
Perror ("Can't open serial1 port .");
Return-1;
}
/* Tcgetattr (fd, & options );
Cfsetispeed (& options, B9600 );
Cfsetospeed (& options, B9600 );
Options. c_cflag | = (CLOCAL | CREAD); // ignore the control signal line and enable READ function
Options. c_cflag | = PARENB; // parity check
Options. c_cflag & = ~ PARODD; // parity check
Options. c_iflag | = (INPCK | ISTRIP );
Options. c_cflag | = CSTOPB; // two stop bits
Options. c_cflag & = ~ CSIZE;
Options. c_cflag | = CS8; // eight data bits
Options. c_lflag & = ~ (ICANON | ECHO | ISIG); // original input mode
Options. c_oflag & = ~ OPOST; // Original output
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.