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,

CodePudding user response:

reference 1st floor C_gyl response:
Read operations (unfinished) occurs when 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 don't try to set the Read timeout, if don't have to try, may be stuck in there,

What is called "read out the questions?" Can think so, because only reading data, even if there is a problem just can't read the data?
Can I make a such an experiment: opens a serial port, but a serial port don't pick up any other end devices, so I send a data in the past, will not receive any return, so there is a "reads" overtime will still be considered "send timeout"?

CodePudding user response:

reference wf2397108372 reply: 3/f
this 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,

Charge less than data, but there is on my side, that is, I use the read method can't read the data in the cache, and then wait there, didn't go to catch it because of the timeout

CodePudding user response:

refer to the second floor xuzuning response:
from 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 the

Thank you for your explanation, in accordance with your instructions, each byte need seconds (1/960=0.00104), which is greater than 1 millisecond, that is between two bytes will need at least 1 millisecond time more, that I set ReadTimeout=1 (1 ms), read the first byte to the second byte should be more than 1 millisecond, calculate the timeout,
Don't know if my understanding right?

CodePudding user response:

Right, is the timeout
Serial port driver is reading the buffer is full and abnormal (such as timeout), the end of the read operation notice of
According to your Settings, it should be your S1Com. Read Read only to one byte at a time

CodePudding user response:

LaiLai_Fido
reference 4 floor response:
Quote: refer to 1st floor C_gyl response:

Read operations (unfinished) occurs when the number of milliseconds before the timeout, namely to Read properly, 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,

What is called "read out the questions?" Can think so, because only reading data, even if there is a problem just can't read the data?
Can I make a such an experiment: opens a serial port, but a serial port don't pick up any other end devices, so I send a data in the past, will not receive any return, so there is a "reads" overtime will still be considered "send timeout"?

You and another serial port communication, deliberately not let another serial port to give you return, the Read will timeout, helps you to set the timeout, and Write the general could be the own serial port or control of the serial port connection problems, will timeout, which is set to Write will work overtime,

CodePudding user response:

That you are using a Windows operating system? ! Windows desktop operating system is designed for desktop graphics operations, often thousands of threads at the same time, to do less than once every 1 millisecond call your process,

If you entangled with low-level industrial control train of thought, do not use advanced Windows operating system, you use "bare-metal" then use assembly language or pure c language to write your own operating system and application system, you deal with the BIOS, you control and interrupt port, speaking, reading and writing, etc.,

With only the underlying industrial operation, is not suitable for the advanced development of Windows,

CodePudding user response:

@ sp1234 didn't come to "see you bed for many years, P elder brother is still very active,

Just into the row of PC with serial ports under the control of a machine, as it happens, the bed they found this problem,

Master this question, I began to make than you also didn't force,

First of all, your computer CPU speed compared with the lower machine CPU, that's quite fairly quickly,

For example, an adult (PC) ask a 4 year old child (a machine), "7 + 2 is equal to a few ah?" , children just snapping fingers to calculate, need a time waiting for a reply,

PC sent out a message, need a wait time, waiting for the next bit machine operation is completed before answering,

The speed of the baud rate can be understood as a child to speak, but the wait time (timeout) can be understood as a maximum waiting child how long you work out,

Two seconds isn't much, but the two seconds what also not stem, just under a machine to answer, such as

Above, can understand it?

CodePudding user response:

references 9/f, interacting with professional developers response:
that you are using a Windows operating system? ! Windows desktop operating system is designed for desktop graphics operations, often thousands of threads at the same time, to do less than once every 1 millisecond call your process,

If you entangled with low-level industrial control train of thought, do not use advanced Windows operating system, you use "bare-metal" then use assembly language or pure c language to write your own operating system and application system, you deal with the BIOS, you control and interrupt port, speaking, reading and writing, etc.,

With only the underlying industrial operation, is not suitable for the advanced development of Windows,
you in the cow force your mother?
  •  Tags:  
  • C#
  • Related