reading data from serial port

R

Ringo

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
 
M

Michael A. Covington

sp.PortName = "COM53";

How did your port name get to be COM53 instead of COM1, COM2, COM3, or COM4?
 
M

Michael A. Covington

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?

This is going to be vague and unhelpful, but...

In C#, characters aren't bytes. An "encoding" is a way of converting one
into the other, and apparently you're using a 7-bit ASCII encoding.
(Various varieties of Unicode are native to C#.) Check whether SerialPort
has other methods for reading a byte without treating it as a character.

Got to go, or I'd look this up right now, since it's important.
 
J

Jon Skeet [C# MVP]

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?

If you want binary data, don't use ReadExisting which reads *text*
data. (The documentation is unfortunately misleading.) Use Read(byte[],
int, int) instead.
 
J

JR

And make sure you defined the port as 8 bit.

SerialPort.DataBits Property = 8 (the default).

Also your equipment must define 8 bits.

JR


Jon Skeet said:
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?

If you want binary data, don't use ReadExisting which reads *text*
data. (The documentation is unfortunately misleading.) Use Read(byte[],
int, int) instead.
 
H

hjgvhv uhhgvjuhv

JR said:
And make sure you defined the port as 8 bit.

SerialPort.DataBits Property = 8 (the default).

Also your equipment must define 8 bits.

Please, explain what this means ??

UARTS are always 8-bits !

Its in the software that decides to "fix" it.

donald
 
M

Miroslav Stampar [MCSD.NET / Security+]

UARTs (Universal Asynchronous Receiver Transmitter) are serial chips
on your PC motherboard (or on an internal modem card). The UART's
purpose is to convert bytes from the PC's parallel bus to a serial bit-
stream. Say you have a terminal hooked up to your PC. When you type a
character, the terminal gives that character to its transmitter (also
a UART). The transmitter sends that byte out onto the serial line, one
bit at a time, at a specific rate. On the PC end, the receiving UART
takes all the bits and rebuilds the (parallel) byte and puts it in a
buffer.

You should not worry about: "UARTS are always 8-bits !".

Set SerialPort.DataBits to 8 and use Read instead of ReadExisting.


hjgvhv uhhgvjuhv je napisao/la:
 
B

Ben Voigt

hjgvhv uhhgvjuhv said:
Please, explain what this means ??

UARTS are always 8-bits !

UARTs are *never* 8 bits. Well, ok, it is possible, but it must be one of
the rarest combinations.

UARTs have only one wire in each direction. On that wire is sent first a
start bit, then either 5, 6, 7, or 8 data bits, then optionally a parity
bit, then 1, 1.5, or 2 stop bits.

The most common combination is 10 bits -- one start, eight data, one stop.
Along with the baud rate, this is commonly written as "9600-8-N-1" or
"38400-8-N-1", indicating 8 data bits, no parity, one stop bit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top