Re: SetRThreshold does not works as expected
You may not always be able to issue ReadFile in time. You may then be
calling SetRThreshold after one byte already arrived.
"clinisbut" <clinisbut@gmail.com> wrote in message
news:465c6709-108d-404b-9dc2-18aa6ce3dd9e@f47g2000hsd.googlegroups.com...
My application is receiving binary data from a device connected to
uart and have to analyze it byte to byte to identify frames.
I'm using MSCom Object from Active-X.
This is the code I use to open the serial port:
m_Comm.SetCommPort( 7 );
m_Comm.SetSettings( "4800,o,8,1" );
m_Comm.SetInputMode( 1 );
m_Comm.SetRThreshold( 1 ); //Event when data received every 1 byte
m_Comm.SetInputLen( 1024 );
m_Comm.SetInBufferCount( 0 );
m_Comm.SetPortOpen( true );
The Frames have different lengths, so I Set SetRThreshold to 1, to
OnCommEvent fires every single byte is received.
That's what I want to achieve.
The first "problem" is that OnCommEvent doesn't fires up every 1 byte,
well... only sometimes, usually it fires up every 2-3 bytes, but this
is not a problem if I receive the entire frame in little pieces.
Example: The device send me this frame: A2 03 A5
I receive this:
1 time OnCommEvent fires up: A2 //I get only 1 byte, that is
correct
2 time OnCommEvent fires up: 03 A5 //Although I receive 2 bytes, I
finally receive the entire frame, that's not a problem
Although this is not a hard problem, I would like to know why this is
happening. I setted SetRThreshold to 1, I don't understand this
behaviour.
The second problem is more serious.
The device sends me a frame every time the computer asks for
something, else it's in silence.
Well, the problem is sometimes I don't receive the entire frame, just
a piece. I get the other piece joined with the next answer from
device.
Example:
I ask for something an Device send me this frame: A2 03 A5
I receive this:
1T time OnCommEvent fires up: A2
2T time OnCommEvent fires up: 03
And that's all, I can't get byte "A5" until next answer from Device.
See it:
I make another request to Device: A3 04 A6
I receive this:
1T time OnCommEvent fires up: A5
2T time OnCommEvent fires up: A3 04
3T time OnCommEvent fires up: A6
I can assure that device send me the entire frame the first time.
I know there is a better way to read and write from serial ports, but
this is my only problem, I don't want to rewrite almost the entire
code of my app just for this problem until have no alternative.
I'm tracing with m_Comm.GetInBufferCount() and in the last example
shows this:
I ask for something an Device send me this frame: A2 03 A5
I receive this:
1T time OnCommEvent fires up: //GetInBufferCount() returns 1, the byte
received is A2
2T time OnCommEvent fires up: //GetInBufferCount() returns 1 , the
byte received is 03
And that's all, I can't get byte "A5" until next answer from Device.
See it:
I make another request to Device: A3 04 A6
I receive this:
1T time OnCommEvent fires up: //GetInBufferCount() returns 1, the
byte received is A5
2T time OnCommEvent fires up: //GetInBufferCount() returns 2, the
bytes received are A3 04
3T time OnCommEvent fires up: //GetInBufferCount() returns 1, the byte
received is A6
GetInBuffer() always returns the correct value acording the bytes I
receive from uart, but this doesn't mean that's the correct value!