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

J

JDJ

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
 
D

Dick Grier

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

JDJ

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.utf8encoding.aspx
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/serialcommunication.aspx
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 = 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
 

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