Garbled data on the read port!

Source: Internet
Author: User
Garbled data on the read port! -- Linux general technology-Linux technology and application information. For details, refer to the following section. The following is my source program. During system debugging, we found that the first part of the characters we read was garbled, and the latter part was correct. Please give us some advice!

/*************************************** ****************************/
If (fd1 = open ("/dev/ttyS02", O_RDWR | O_NOCTTY | O_NDELAY) <0)/* open the serial port PortA */
Exit (0 );
/*************************************** ****************************/

/* Prepare to start sending and receiving data */

Tcgetattr (fd1, & option );
Cfmakeraw (& option );

/************************ Baud rate setting ************* *************/
Cfsetispeed (& option, B9600);/* set Port baud rate to 9600bps */
Cfsetospeed (& option, B9600 );

Tcsetattr (fd1, TCSANOW, & option );

/* Prevent serial port blocking */
MaxFd = fd1 + 1;
TV. TV _sec = 0;
TV. TV _usec = 500000;

/*********************** Enter the command/response loop ********* *******************/
While (1> 0)
{
/************************ Receive data from Port ********* ********************/
/* Communicate with the device */
Usleep (2000000 );
Retv1 = write (fd1, sbuf1, 4);/* Send Communication Request */

Usleep (1000000 );
FD_ZERO (& readset );
FD_SET (fd1, & readset );
Select (MaxFd, & readset, 0, 0, & TV );

If (FD_ISSET (fd1, & readset ))
Retv2 = read (fd1, rbuf1, 23);/* Receives 23 char responses from the device */

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.