Hansi Mausi

Hansi Mausi

  • NA
  • 4
  • 0

Displaying raw data hex-bytes from SerialPort.DataReceived method

Sep 7 2010 3:47 AM
Hi,
how can I display data, I received on SerialPort, in a raw hex byte format ?

It took me several approaches to convert the data basically, but there still seems to be an ASCII / UTF7 / UTF8 problem. I am now using a byte array instead of a former char-array, but still I don't get any value larger than 127 dec (0x7F). When sending decimal 128, I receive 0x3F on RX-Side.

Here's the current DataReceiveHandler:

 void PortBDataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(200);
string bytestr = "";
bytestr = _portB.ReadExisting();
bytestr = bytestr.Replace("\r", "");
UTF8Encoding utf = new UTF8Encoding();
byte[] myarray = utf.GetBytes(bytestr);
string hexvalues = "";
foreach (byte b in myarray)
{
hexvalues = hexvalues + (b.ToString("X2")) + " ";
}
UpdGuiEvent.Invoke(hexvalues);
}


Does anybody know how to display the hex values for incoming bytes from 0x80 up to 0xFF ?

Answers (1)