Linux serial port multi-thread read/write

Source: Internet
Author: User
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

Options. c_cc [VMIN] = 0;
Options. c_cc [VTIME] = 0;
Tcsetattr (fd, TCSANOW, & options );

Printf ("\ nfd = % d \ n", fd );
Return fd;
}
*/

Int
Main (int argc, char * argv [])
{
Int iret;
Pthread_t id1, id2;

If (fd = open ("/dev/tts/0", O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY) =-1)
{
Perror ("Can't open serial1 port .");
Return-1;
}

Iret = pthread_create (& id1, NULL, (void *) readThread, NULL );
If (iret! = 0 ){
Printf ("\ nthreadappscreate is failed! \ N ");
}

Iret = pthread_create (& id2, NULL, (void *) writeThread, NULL );
If (iret! = 0 ){
Printf ("\ nthreadappscreate is failed! \ N ");
}

Pthread_join (id1, NULL );
Pthread_join (id2, NULL );

Close (fd );
Return 0;
}

Void readThread (void * arg)
{
Int nread, I;
Int rcount = 0;
Unsigned charbuf [21];
While (1 ){
Sleep (1 );
Printf ("\ nnow it begain to read words. \ n ");
// Printf ("\ nbefore reading, the words inf buf are % s \ n", buf );
Nread = read (fd, buf, 20 );
If (nread = 20 ){
Printf ("\ nnread = % d", nread );
For (I = 0; I <20; I ++ ){
Printf ("% c", buf [1]);
}
Rcount ++;
Printf ("\ nreceive packets: % d \ n", rcount );
}
}
}

Void writeThread (void * arg)
{
Int I, j;
Int fd;
Unsigned char k = 'a ';
Int m;
Int nwrite;
Int scount = 0;
Unsigned charserialTest [NUM] = {0 };

For (m = 0; m {
SerialTest [m] = k ++;
}
/* Open serial port */
While (1 ){
Sleep (1 );
Fd = open ("/dev/tts/0", O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY );
Nwrite = write (fd, serialTest, NUM );
Write (fd, "\ n", 1 );
If (nwrite = 20 ){
Printf ("\ nwriteRadioFramewriteRadioFramewriteRadioFramewriteRadioFrame \ n ");
Printf ("\ nnwrite = % d \ n", nwrite );
For (j = 0; j <NUM; j ++ ){
Printf ("% c", serialTest [j]);
}
Scount ++;
Printf ("\ nsend packets: % d \ n", scount ++ );
Printf ("\ nwriteRadioFramewriteRadioFramewriteRadioFramewriteRadioFrame \ n ");
}
Close (fd );
}
}

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.