Serial Port receives 7F instead of FF

  • Thread starter Thread starter Rainer Queck
  • Start date Start date
R

Rainer Queck

Hi NG,

I have a little problem with the serial port.
If a external device sends my app (PocketPC) a 0xFF I only see a 0x7F within
my app. Why is that? What must I do to receive a 0xFF?
I have tried to set serialport.Encoding to UTF8Encoding, but VS2005 then
reports ".. Type is not vailid in this context..."

Thanks for help and hints....

Rainer
 
The "serial port" is a hardware device. If the problem is really there, I'd
say that the problem is that you've configured it for 7, not 8, data bits,
resulting in a maximum value that can be received of 0x7f.

If the problem is the software you're using to access the serial port, then
you'll have to show us the read code that you're using...

Paul T.
 
Hi Chris,

thanks for responding!

Here is how I initialize the port :

serialPort = new SerialPort("COM1");
serialPort.BaudRate = 19200;
serialPort.DataBits = 8;
serialPort.Handshake = Handshake.None;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;

reading incomming bytes :
msgLen = serialPort.BytesToRead;
if (msgLen == 0){
return;
}
rcvBytes = new Byte[msgLen];
serialPort.Read(rcvBytes,0,msgLen);
......
Here the first two bytes should be 0xFF but they are 0x7F
If I use the following code to read instead things work OK...

msgLen = serialPort.BytesToRead;
if (msgLen == 0){
return;
}
rcvBytes = new Byte[msgLen];
int aByte;
for(int i=0 ; i<msgLen ; i++){
aByte = serialPort.BaseStream.ReadByte();
rcvBytes = System.Convert.ToByte(aByte);
}

Why is this so?
Also now it looks I have the same problem , sending . The external device
does not recognize my messages. Since it expects a double 0xFF as a Message
Start, I think I am sending 0x7F instead of 0xFF....

here my send code....
serialPort.Write(aByteArr,0,aByteArr.Length);

Looking into the PDA I can see that the Message is OK (0xFF,0xFF......) but
it seams to be ignored by the external device.
If I use the same code within a Windows application every thing works OK

Thanks for further help!
Rainer
 
Hi Paul,
say that the problem is that you've configured it for 7, not 8, data bits,
resulting in a maximum value that can be received of 0x7f.
No, this is not the case. See my answer just posted to Chris.

Also, I am using a wraper class, that if used with in a Windows Application
works fine with the external device!

Regards
Rainer
 
Hi,

I guess you have to switch to another encoding method. The default encoding
for SerialPort is ASCII-Encoding, which uses only the ASCII-Characters (0x00
.... 0x7F). To use the full 8-Bit, you have to switch to e.g. UTF8. Just add
the following line to your initialization routine:

serialPort.Encoding = Encoding.UTF8;

For further information have a look at the SerialPort documentation in the
MSDN.

Have fun!
 
Back
Top