ringo42

I have a micro controller that sends a stream of 102 bytes to the pc.
The values range from 0 to 255. I set up my serialport like this

sp.BaudRate = 115200;
sp.PortName = "COM53";
sp.DataBits = 8;
sp.StopBits = System.IO.Ports.StopBits.One;
sp.Parity = System.IO.Ports.Parity.None;
sp.ReadTimeout = 0;
sp.Open();
sp.DataReceived += new
System.IO.Ports.SerialDataReceivedEventHandler(sp_DataReceived);

and then I have this

void sp_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{

string Mystring = sp.ReadExisting();
byte testbyte = 254;
// Gather all the bytes until 102 is reached
foreach (byte c in Mystring)
{
if(pixelcount<102)
pixel[pixelcount] = c;
pixelcount++;
if (c > 126)
Console.WriteLine("big number {0}", c);// biggest
number ever printed is 127
}
//got all the bytes, now draw them
if (pixelcount == 102)
{
Console.WriteLine("testbyte = {0}", testbyte);
oldx = 0;
pixelcount = 0;
pictureBox_rawData.Invalidate();
}
}

My problem is that "c" is never over 127. Even when I hard code some
254's into the micro the set seen as 63's.
What am I missing here Readexisting says it reads bytes based on
encoding, but I can't figure out exact;y what that means, is there
another serial port property I need to set

thanks
Ringo



Re: .NET Base Class Library reading bytes from the serial port

nobugz

When you use ReadExisting(), you're reading characters, not bytes. The default encoding for SerialPort is ASCII, byte values 128 through 255 are not valid ASCII character codes and are returned as question marks (character code 63). You could use a different encoding, in your case just use Read() to read bytes.