[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

SerialPort read&write members display string value above 127 as "?

JDJ

11/5/2008 6:15:00 PM

We are currently working with C# 2008 (Serial Port Class)
Read the Serial port data and Write out to the serial port from and to our
control that sends and accepts a string of data
The data â??Byteâ? sent in the String is value of decimal 0 to 233 (this
relates to an ACSII hex value of 0-E9) Since the Write (and apparently the
Read) Methods are limited to Decimal 127 (Hex 7F) we get a Char 63 or â???â? for
all characters over 127

We think we need to implement an encoding method - UTF8Encoding but we are
not sure how to do this from the examples that are on the .NET Library


3 Answers

Peter Duniho

11/6/2008 12:02:00 AM

0

On Wed, 05 Nov 2008 10:15:01 -0800, JDJ <JDJ@discussions.microsoft.com>
wrote:

> We are currently working with C# 2008 (Serial Port Class)
> Read the Serial port data and Write out to the serial port from and to
> our
> control that sends and accepts a string of data
> The data â??Byteâ? sent in the String is value of decimal 0 to 233 (this
> relates to an ACSII hex value of 0-E9) Since the Write (and apparently
> the
> Read) Methods are limited to Decimal 127 (Hex 7F) we get a Char 63 or
> â???â? for
> all characters over 127
>
> We think we need to implement an encoding method - UTF8Encoding but we
> are
> not sure how to do this from the examples that are on the .NET Library

Without seeing an example of the code you're using to write the data, it's
hard to say. But ASCII only supports character codes up to 127. If you
want a different encoding that supports a wider range of character codes,
you do need to make sure you're using that encoding.

Note that UTF-8 may or may not be the encoding you want. In particular,
it handles character codes up to 127 in a single byte, just like ASCII.
But other characters may be longer. If your recipient doesn't support
UTF-8, then encoding as UTF-8 isn't going to improve matters. A common
single-byte encoding that does support characters up to 255 is
ISO-8859-1. But without knowing more about the recipient of the data,
it's impossible to say for sure what encoding would be appropriate.

Pete

Dick Grier

11/6/2008 5:39:00 PM

0

Both Read and Write can (and should) be used with Array of type Byte
arguments, not String. Thus all values 0-xFF may be transfered.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.


JDJ

11/8/2008 4:41:00 PM

0

Thank you for your response â?? Your response indicates that the problem that
we are having with the serial port class is what we thought
This project came from a need to communicate with our control hardware that
was originally done We have the running OK on HyperTerminal, Previous Visual
Basic 2003 (run On Frame work 1.X -3.X (used a public domain RS232 class)
and also did hardware interface to Rockwell PLC â??Logixsâ? family â?? We are
using an ANSI terminal emulation. â?? I always called it Extended ASCII
character set â?? But working on finding more about character sets I realize
that that is touted to be an incorrect description.

Serial Port Class methods of Read and Write default to ASCII (7 bit) code
Character - Decimal from 0 to 127 â?? (0 to 7F) Character above
We use character (values) from 33 to 233 (21 â?? E9 Hex) (! To é)
The control must have a set number of â??charactersâ? (actually there are 3
different control Communications stream lengths â?? Different hardware
products)

To see the problem we used the following example:
http://msdn.microsoft.com/en-us/library/system.text.utf8enc...
Changed it like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EncodeTest1
{
class Program
{
static void Main(string[] args)
{
// Create a UTF-8 encoding.
UTF8Encoding utf8 = new UTF8Encoding();
UTF7Encoding utf7 = new UTF7Encoding();
ASCIIEncoding ASCIIX = new ASCIIEncoding();


// A Unicode string with two characters outside an 8-bit code
range.
String unicodeString =
"This unicode string contains two characters " +
"with codes outside an 8-bit code range, " +
"Pi (\u03a0) and Sigma (\u03a3).";
Console.WriteLine("Original string:");
Console.WriteLine(unicodeString);


// Encode the string.
Byte[] encodedUTF8Bytes = utf8.GetBytes(unicodeString);
Byte[] encodedUTF7Bytes = utf7.GetBytes(unicodeString);
Byte[] encodedASCIIXBytes = ASCIIX.GetBytes(unicodeString);
Console.WriteLine();
Console.WriteLine("Encoded bytes:");
Console.WriteLine();
Console.WriteLine("UTF8 bytes:");
foreach (Byte b in encodedUTF8Bytes)

{
Console.Write("[{0}]", b);
}
Console.WriteLine();
Console.WriteLine("UTF7 bytes:");
foreach (Byte b in encodedUTF7Bytes)
{
Console.Write("[{0}]", b);
}

Console.WriteLine();
Console.WriteLine("ASCIIX bytes:");
foreach (Byte b in encodedASCIIXBytes)
{
Console.Write("[{0}]", b);
}
// Decode bytes back to string.
// Notice Pi and Sigma characters are still present.
String decodedString = utf8.GetString(encodedUTF8Bytes);
Console.WriteLine();
Console.WriteLine("Decoded bytes:");
Console.WriteLine(decodedString);
Console.WriteLine("END of program");

}
}
}
This showed us what some of the different encodings are doing
So we decided we do not really have to have the characters â?? what we really
need are the values instead â?? that is really what we use and the previous
method was just use to make it easy to use HyperTerminal to test the
communications

We found the example
http://www.codeproject.com/kb/cs/serialcommunic...
Worked though this (needed a few changes/fixes for us to use it) and so far
tested with the serial port read method â?? seems to do (emulate) what we need:
We use HyperTerminal to send us typical string â?? we use something like this
(#q#+!!!!#+#!!+!!!!!!!#!+!#+!#!+!#+!!¬!!!!!) 0r
(Oq!5%!!!']'%!+!!Iq?!!#!+!#+!#!+!#+!!w%!!!!) â?? not sure how this will be
received

private void readButton_Click(object sender, EventArgs e)
{
try
{
//clear the text box
textBox.Text = "";
//read serial port and displayed the data in text box
//textBox.Text = sp.ReadLine();
int bufferByteValue = sp.BytesToRead;
int[] controlData = new int[bufferByteValue];
int displayBytes;
int controlParameterValue;
for (int i = 0; i < bufferByteValue; i++)
{
displayBytes = sp.ReadByte();
controlData[i] = displayBytes;
controlParameterValue = (displayBytes - 33) / 2;

textBox.Text = textBox.Text + (" Index = " + "(" + i + ")" +
" ");
textBox.Text = textBox.Text + displayBytes.ToString() + " =
" + controlParameterValue + "\t";
}
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
Regards JDJ


"JDJ" wrote:

> We are currently working with C# 2008 (Serial Port Class)
> Read the Serial port data and Write out to the serial port from and to our
> control that sends and accepts a string of data
> The data â??Byteâ? sent in the String is value of decimal 0 to 233 (this
> relates to an ACSII hex value of 0-E9) Since the Write (and apparently the
> Read) Methods are limited to Decimal 127 (Hex 7F) we get a Char 63 or â???â? for
> all characters over 127
>
> We think we need to implement an encoding method - UTF8Encoding but we are
> not sure how to do this from the examples that are on the .NET Library
>
>