Home > Net > C # serial port WriteTimeout and ReadTimeout issues
C # serial port WriteTimeout and ReadTimeout issues
Time:11-28
Recently in the use of serial port to read data, due to use of two serial port to read the data, data back, of course, I must have successively, returned or a serial port to the complete data, part of the data received from a serial port, so I need to set the ReadTimeout time, but has been useless, even if I set the ReadTimeout=1, which is 1 millisecond is no use (the actual data to wait for more than 2 seconds, began to return, a return to 36 ASCII characters), I want to ask, this specific ReadTimeout time from when to begin to calculate? 1. Starts calculation method DataReceived events trigger? That triggers the start calculate the Timeout time after receiving data? I set the baud rate=9600, which is sending 9600 characters per second, learned that each character takes (1/9600) seconds, than I have ever set ReadTimeout=1 time, that even if the timeout, will be implemented according to the timeout to catch it, but did not actually, in the process of the program have been waiting for, 2. Starting from a serial port to send data? The middle need to wait for more than 2 seconds, affirmation also calculate a timeout,
So don't know how to understand the ReadTimeout time, CSDN in has not find I want to answer, please everybody can explain in detail, thank you!
private void S1Com_DataReceived (object sender, SerialDataReceivedEventArgs e) { int count=0; If (S1Com. IsOpen) { Try { Listening=true; Int n=S1Com. BytesToRead; Byte [] receivedData=https://bbs.csdn.net/topics/new Byte [n].//create the receiving an array of bytes S1Com. Read (receivedData, 0, n);//read the data //S1Com DiscardInBuffer (); Buffer1. AddRange (receivedData);
If (buffer1. Count==36) {
Buffer1. CopyTo (HexData_1 0, 0, 36).
String strResult1=Encoding. The ASCII. Get string (HexData_1);
This. TxtBoxS1. Invoke ((EventHandler) ( The delegate { This. TxtBoxS1. Text=Encoding. The ASCII. Get string (HexData_1); }));
TestResult1 (strResult1); }
} The catch (System. Exception ex) { MessageBox. Show (ex. The Message, "errors"); TxtBoxS1. Text=""; } The finally { Listening=false; } } }
CodePudding user response:
Read operations (not finished) the number of milliseconds before the timeout, also is the normal reading, this parameter is not action, it is to let you Read out, wait for after the timeout, also can return, you try not to set the Read timeout, if don't have to try, may be stuck in there,
CodePudding user response:
From the date the start reading
In addition you wrong understanding of baud rate=9600 Baud rate value is the number of bits per second, a byte 8 bits, add a start bit and end of a bit, that is, every 10 binary said a byte 9600 said 960 bytes per second transmission capacity SerialPort is to transmit data in bytes processing, don't confuse
CodePudding user response:
The Read timeout refers to
S1Com. Read (receivedData, 0, n);
timeout in this method, that is to say, if this method can read until data is no timeout problems,