[Rk_2014_0910] possible causes of incomplete serial data reception in serial Programming

Source: Internet
Author: User
Tags usleep

Scenario 1]

[Ubuntu14.04 --> Win8]

[Environment description]

PC1: Win8, run serial port debugging tool: USR-TCP232-Test.exe; Serial Port COM1 parameters set to "9600,8, 1, N ".

PC2: ubuntu14.04; set the serial port/dev/ttyusb0 parameter to "9600,8, 1, N ".

 

[Code description]

// Void * ctesteth: threadfunchandleserialporttransfer (in void * Arg) {If (null = Arg) {return NULL ;} ctesteth * pc_test_eth = (ctesteth *) ARG; // open the serial port if (pc_test_eth-> serialportopen (pc_test_eth) {cout <"pc_test_eth-> serialportopen () error! "<Endl; return NULL;} while (1) {// serial transmission function, send the string cyclically:" Hello, ubuntu14.04 \ n ". If (pc_test_eth-> ethtoserialporttransfer (pc_gap_eth) // usleep (100000); // The latency is 100000 milliseconds, that is, 100 subtle // sleep (1 ); // delay 1 second} // close the serial port pc_test_eth-> serialportclose (pc_gap_eth); return NULL ;}

 

[Problem description]

On PC2, start the serial transmission thread and send the string "Hello, ubuntu14.04 \ n" cyclically ".

The serial port debugging tool on pC1 receives incomplete data, sometimes receives "hello,", sometimes receives "Hello, ubun", and sometimes receives "H ".

 

[Cause Analysis]

The cause of similar problems is

(1) the serial port configuration code is incorrect at the sender or receiver.

(2) Open frequently and close the serial port.

(3) The sender program does not set a delay. As a result, the recipient program at the peer end cannot receive the program, or the receiver is incomplete.

(4) there may be other reasons. Please add them later.

 

[Solution]

(1) Check the serial port parameter settings of the sender and receiver. In addition, check the serial code at both ends carefully.

(2) Close the serial port instead of opening it frequently.

(3) Add the latency code to the sender program.

 

[Latency function]

The latency function in Ubuntu is:
Sleep () // seconds
Usleep () // millisecond level, 1 second = 10 ^ 6 milliseconds
Nanosleep () // nanoseconds, 1 second = 10 ^ 9 nanoseconds

 

[Other Reference Information]

(1) SerialPort. Open Method in msdn

Http://msdn.microsoft.com/zh-cn/library/system.io.ports.serialport.open (V = vs.110). aspx

Remarks

Each SerialPort object can have only one open connection.

For all applications, the best practice is to wait for a while after calling the close method and before trying to call the open method, because the port may not be closed immediately.

(2) Linux man page

Sleep (3)-Linux man page

Http://linux.die.net/man/3/sleep

Usleep (3)-Linux man page

Http://linux.die.net/man/3/usleep

Nanosleep (2)-Linux man page

Http://linux.die.net/man/2/nanosleep

 

 

Scenario 2]

[Ubuntu14.04 --> ubuntu14.04]

[Environment description]

PC1: ubuntu14.04; the serial port/dev/ttys0 parameter is set to "9600,8, 1, N ".

PC2: ubuntu14.04; set the serial port/dev/ttyusb0 parameter to "9600,8, 1, N ".

 

[Description]

When both ends are ubuntu14.04,

Start the serial transmission thread in pc2,

Enter commands in pC1 Terminal

# cat /dev/ttyS0

Even if the data transmission rate in PC2 is very fast and the data packet sent each time is large (several hundred bytes), the terminal in pC1 can receive data normally.

 

[End]

[Rk_2014_0910] possible causes of incomplete serial data reception in serial Programming

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.