468,505 Members | 1,756 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

reading data from serial port

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

May 26 '07 #1
8 36175
sp.PortName = "COM53";

How did your port name get to be COM53 instead of COM1, COM2, COM3, or COM4?
May 26 '07 #2
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.
May 26 '07 #3
Ringo <ri*********@gmail.comwrote:

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 26 '07 #4
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 [C# MVP]" <sk***@pobox.com???
??????:MP*********************@msnews.microsoft.co m...
Ringo <ri*********@gmail.comwrote:

<snip>
>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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 26 '07 #5
JR wrote:
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

>
JR
May 27 '07 #6
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:
JR wrote:
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


JR
May 27 '07 #7

"hjgvhv uhhgvjuhv" <as**@asdf.comwrote in message
news:eb**************@TK2MSFTNGP06.phx.gbl...
JR wrote:
>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 !
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.
>
Its in the software that decides to "fix" it.

donald

>>
JR

May 29 '07 #8

"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
> sp.PortName = "COM53";

How did your port name get to be COM53 instead of COM1, COM2, COM3, or
COM4?
Most likely a USB serial converter.
May 29 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Kris | last post: by
4 posts views Thread by Petr Jakes | last post: by
3 posts views Thread by Shark | last post: by
4 posts views Thread by cmdolcet69 | last post: by
9 posts views Thread by Hal Vaughan | last post: by
3 posts views Thread by Sean | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.