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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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[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


.



Relevant Pages

  • Re: psSerial: how to write a single byte value to the serial port?
    ... >> I assume the string print ser.writewill append null characters to the ... I wish to send a single byte to the serial port and nothing more. ...
    (comp.lang.python)
  • Re: Send a 4-character string via serial port
    ... serial port. ... If you need to convert ASCII codes to characters, ...
    (comp.soft-sys.matlab)
  • Re: How to convert Infix notation to postfix notation
    ... If this is for an error message, why isn't it using stderr for its output? ... array of 15 characters, and you call this function with the limit 15 on ... Making sure that the only string I allocate and append to, ... because mulFactor in all versions must needs incorporate the functions ...
    (comp.lang.c)
  • Re: Prothon should not borrow Python strings!
    ... """It does not make sense to have a string without knowing what encoding ... same cul de sac as Python. ... Prothon_String_As_ASCII // raises error if there are high characters ... Python's split between byte strings and Unicode strings is ...
    (comp.lang.python)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... put them in stupid places. ... Programming is difficult (as you must surely appreciate, ... > strings will be in the range 1...1000 characters. ... impose an artificially small limit on string length." ...
    (comp.programming)