|
-
July 18th, 2008, 11:44 AM
#1
Serial Comm. ReadFile() cannot read zeros
Hi,
I am trying to design a communication protocol between a device and a PC.
The device sends integers from 0 to 0xFF and the PC is supposed to read them.
The problem I am having is reading the zeros! All other values (from 1 to 0xFF) work fine.
The code of the PC program is (in a while loop with timeouts):
// read 1 character at a time
unsigned char c;
ReadFile(handle, &c, 1, &n_bytes_read, NULL);
When the device sends a 0 integer, the ReadFile() function returns with n_bytes_read = 0.
So there is no way finding out if ReadFile() didn't read any value OR it read a 0 integer (which is the NULL character in ASCII).
Help.
Thanks,
S.
-
July 18th, 2008, 12:13 PM
#2
Re: Serial Comm. ReadFile() cannot read zeros
I find it hard to believe that ReadFile isn't working. I use it all of the time.
Anyway, are you using blocking? If so, then the fact that ReadFile returned means that you got your character.
What method are you using to read the COM port? Sound like it needs some debugging. Post your method here.
-Erik
-
July 18th, 2008, 12:49 PM
#3
Re: Serial Comm. ReadFile() cannot read zeros
ReadFile() is working for all values except 0.
The function to read one value at the time is:
bool int_read_port_Ser(unsigned short *byte )
{
DWORD n_bytes_read;
// Validation
if(!byte)
return false;
// Initialization
DWORD time_start = GetTickCount();
// Read an incoming byte from the port
do
{
// Read
unsigned short c;
if(!ReadFile(handle, &c, 1, &n_bytes_read, NULL))
return false;
// convert character to short
*byte = c;
// Filter character
if(n_bytes_read == 1){
return true;
}
// Time
DWORD time_elapsed = elapsed_ms(time_start, GetTickCount());
// If timeout
if(time_elapsed >= (RX_TIMEOUT_MS ))
return false;
}while(1);
// Done
return true;
}
When the device is sending say: 1, 3, 0, 100, 255,
this function (ReadFile()) gives me: 1, 3, 100, 255
S.
-
July 18th, 2008, 01:05 PM
#4
Re: Serial Comm. ReadFile() cannot read zeros
What is your DCB setup? Do you have it in text mode?
-Erik
-
July 18th, 2008, 01:22 PM
#5
Re: Serial Comm. ReadFile() cannot read zeros
wsDcb.BaudRate = CBR_115200; // Current baud rate
wsDcb.fBinary = true; // Binary mode, no EOF check
wsDcb.fParity = false; // Enable parity checking
wsDcb.fOutxCtsFlow = false; // CTS output flow control
wsDcb.fOutxDsrFlow = false; // DSR output flow control
wsDcb.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type
wsDcb.fDsrSensitivity = false; // DSR sensitivity
wsDcb.fOutX = false; // XON/XOFF out flow control
wsDcb.fInX = false; // XON/XOFF in flow control
wsDcb.fErrorChar = false; // Enable error replacement
wsDcb.fNull = true; // null stripping
wsDcb.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control
wsDcb.fAbortOnError = true; // Abort reads/writes on error
wsDcb.ByteSize = 8; // Number of bits/Byte, 4-8
wsDcb.Parity = NOPARITY; // 0-4=no,odd,even,mark,buffer
wsDcb.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
-
July 18th, 2008, 01:26 PM
#6
Re: Serial Comm. ReadFile() cannot read zeros
Actually, I just discovered that setting
wsDcb.fNull = false;
solves the problem.
Thank you very much.
S.
Now I have to deal with a serious backward compatibility issue...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|