Click to See Complete Forum and Search --> : Byte string vs Char string


Sam_Rhodes
June 12th, 2009, 09:23 AM
I'm writing an app that communicates with a piece of hardware through an RS-232 port. I want to use the PORT.ReadExisting() functionality to read all received bytes from the buffer. However, the 'ReadExisting()' function returns a character string (each character being 0-127 in value).

I need to read in values outside of the standard ASCII set upto values of 255. I can use ReadByte to get the values, but there is no 'Get all bytes from bufer' function, and I'm not sure exactly where to start making one.

Anyone have any ideas?

Cheers,
Sam.

pcaldredbann
June 12th, 2009, 09:29 AM
Hi,

ReadExisting() method returns everything in the RX buffer and stores it into whatever unicode (default encoding of a string in ASP.Net) which is 16-bits per character. What you need to do then is encode this string to UTF8 or ASCII using:

byte[] theStream = Encoding.ASCII.GetBytes(rxBuffer)... where rxBuffer is = port.ReadExisting()

Same principle applies for UTF which is Encoding.UTF8.GetBytes(rxBuffer). Then you can use the IEnumerator interface for example:

foreach (char c in theStream)
Console.WriteLine(c.ToString())

Or something similar. Hope that helps?