SerialData.Eof circumstances

In my SerialPort.DataReceived event handler, I check SerialData.Eof :

 void DataReceived(object sender, SerialDataReceivedEventArgs e) { if (e.EventType == SerialData.Eof) throw new NotImplementedException("SerialData.Eof"); // ... Read } 

In my development up to this point, I have never encountered this exception. But today, while working on another part of the protocol, he did.

My question is: what does SerialData.Eof mean? MSDN says:

The end of the file character was received and placed in the input buffer.

I am dealing with binary data. What is the "end of file character"?


This MSDN forum post states that

the DCB.EofChar element always gets initialized to 0x1A (Ctrl + Z)

In the reference sources for the SerialStream class on line 1343, we see what is really:

 dcb.EofChar = NativeMethods.EOFCHAR; 

And in Microsoft.Win32.NativeMethods :

 internal const byte EOFCHAR = (byte) 26; 

Does this mean that at any time when my device sends a 0x1A byte, I get a SerialData.Eof event? If so, should I just stop testing for him at all?

+7
source share
1 answer

My initial analysis in this MSDN post was correct. However, the source source shows the response from SerialStream.cs:

 dcb.EofChar = NativeMethods.EOFCHAR; //OLD MSCOMM: dcb.EvtChar = (byte) 0; // now changed to make use of RXFlag WaitCommEvent event => Eof WaitForCommEvent event dcb.EvtChar = NativeMethods.EOFCHAR; 

Ugh, they used the EvtChar DCB setting to detect Ctrl + Z. It was a really bad idea, given that there is no way to change it, and the value of byte 0x1a can certainly be displayed in binary. In practice, it almost always ends, because there will be something to read when a fire occurs. However, now there is a race condition that can trigger another event, this time SerialData.Chars, with which to not read anything after the previous event, caused the reading of all bytes. This will do the read block () until a byte is available. This usually works, but increases the likelihood that Close () locks are blocked. Chronic SerialPort problem.

The correct way to handle this:

 void DataReceived(object sender, SerialDataReceivedEventArgs e) { if (e.EventType == SerialData.Eof) return; // ... Read } 
+8
source

All Articles