Transmitting a 'char' array thru serial port in new C# 2005 Expres

12 views
Skip to first unread message

halukg

unread,
Jul 30, 2005, 9:52:17 AM7/30/05
to
I am trying to send a 6 byte char array from the serial port in new C# 2005
Express:
com.Write(new string(new char[] { (char)34, (char)14, (char)192, (char)51,
(char)0, (char)0 }, 0, 6));

I am receiving 34,14,63,51,0,0 from the port as I connected Tx and Rx pins
to each other by using the following code:

private void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
bytes = com.BytesToRead;
if (bytes >= 6)
com.Read(buffer, 0, bytes);
}

The hard thing to understand is: in this array if I replace 192 with 127 or
any number below I get the array back as it is. Any number above 127 is
received as 63.

Any ideas about the underlying reasons for this?

Jon Skeet [C# MVP]

unread,
Jul 30, 2005, 11:42:14 AM7/30/05
to

It sounds like it's using ASCII for its encoding - what encoding to you
want it to use?

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

Tedb

unread,
Jul 30, 2005, 4:44:34 PM7/30/05
to

63 is the default character that's used for parity errors, not sure how you
could a get a parity error sending and receiving data on the same port
though. If you want to send bytes out the port it may help to send them as
a byte array instead of as a string though.


"halukg" <hal...@discussions.microsoft.com> wrote in message
news:EB9F8D4D-4515-4A19...@microsoft.com...

Tedb

unread,
Jul 30, 2005, 5:35:33 PM7/30/05
to
One other thought, I have not tried this but, do you have the port databits
set to 7? I think that this would effectivly limit you to 0-127.


"Tedb" <an...@anon.com> wrote in message
news:efyMBeUl...@tk2msftngp13.phx.gbl...

Jon Skeet [C# MVP]

unread,
Jul 30, 2005, 6:15:48 PM7/30/05
to
Tedb <an...@anon.com> wrote:
> 63 is the default character that's used for parity errors, not sure how you
> could a get a parity error sending and receiving data on the same port
> though.

It's also '?' though, which is used when an encoding doesn't know how
to handle a character it's asked to encode. I suspect that's the
problem here.

> If you want to send bytes out the port it may help to send them as
> a byte array instead of as a string though.

Agreed.

halukg

unread,
Jul 31, 2005, 6:40:03 AM7/31/05
to
I am trying to transmit unicode characters and receive them without any
conversions or encoding. I mean any b

halukg

unread,
Jul 31, 2005, 6:41:02 AM7/31/05
to
any binary values between 0 and 255.

"Jon Skeet [C# MVP]" wrote:

halukg

unread,
Jul 31, 2005, 6:45:01 AM7/31/05
to
My port initialization code is:
com = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

halukg

unread,
Jul 31, 2005, 6:48:01 AM7/31/05
to
I was sending and receiving the same char array in VS2003 C# by utilizing
MSCOMM OCX without a hickup! I am suspecting there might be a bug in the new
serial port class (System.IO.Ports).

"Jon Skeet [C# MVP]" wrote:

Jon Skeet [C# MVP]

unread,
Jul 31, 2005, 7:39:19 AM7/31/05
to
halukg <hal...@discussions.microsoft.com> wrote:
> any binary values between 0 and 255.

That's very different to your previous reply of "any Unicode
character".

See http://www.pobox.com/~skeet/csharp/unicode.html

Jon Skeet [C# MVP]

unread,
Jul 31, 2005, 7:40:30 AM7/31/05
to
halukg <hal...@discussions.microsoft.com> wrote:
> I was sending and receiving the same char array in VS2003 C# by utilizing
> MSCOMM OCX without a hickup!

That's probably effectively assuming ISO-Latin-1 encoding, just
narrowing any Unicode character you give it down from 16 bits to 8. Try
pushing through character (say) 1000, and see what happens...

> I am suspecting there might be a bug in the new serial port class
> (System.IO.Ports).

Whereas I suspect there's a bug in your understanding of what encodings
do. Have you checked what Encoding the port is using?

Tedb

unread,
Jul 31, 2005, 9:46:09 PM7/31/05
to
halukg,

I agree with Jon, although I have never used the serial port to transmit
unicode characters, I took your code snippet and duplicated your results
(192 sent 63 received), with the serial port's encoding set at it's default
("us-ascii" code page 20127). When I changed the serial port's encoding to
match the default encoding ("iso-8859-1" code page 1252), the code worked as
expected (192 sent, 192 received).

Tedb


"Jon Skeet [C# MVP]" <sk...@pobox.com> wrote in message
news:MPG.1d56c909e...@msnews.microsoft.com...

halukg

unread,
Aug 1, 2005, 4:23:01 AM8/1/05
to
Thank you for drawing my attention to port encoding. I had a wrong assumption
of a single encoding system for all unicode characters. In my application all
used unicode characters are between 0 and 255. This caused me to not to
suspect anything about encoding!
Anyway I will change my encoding and see the result accordingly. I will let
you know of the result...

halukg

unread,
Aug 1, 2005, 4:25:02 AM8/1/05
to
OK I will check the effective encoding..

"Jon Skeet [C# MVP]" wrote:

halukg

unread,
Aug 1, 2005, 8:35:04 AM8/1/05
to
I found the current encoding is us-ascii with the following code:
retval = com.Encoding.EncodingName.ToString();
I dont know how to change this encoding to unicode or iso-8859. Could you
please show me how to do this?

Tedb

unread,
Aug 1, 2005, 11:53:27 AM8/1/05
to
halukg,

I don't have a lot of experience with encoding issues, so I wouldn't know
how to tell which encoding to pick. However, for the test program that I
did, I used to following to change the SerialPort encoding to match the
default encoding:

SerialPort s = new SerialPort("Com3");
s.Encoding = Encoding.Default;

Tedb


"halukg" <hal...@discussions.microsoft.com> wrote in message

news:2D5F6B98-82E1-4435...@microsoft.com...

Jon Skeet [C# MVP]

unread,
Aug 1, 2005, 12:38:10 PM8/1/05
to
halukg <hal...@discussions.microsoft.com> wrote:
> I found the current encoding is us-ascii with the following code:
> retval = com.Encoding.EncodingName.ToString();

Right, that makes sense.

> I dont know how to change this encoding to unicode or iso-8859. Could you
> please show me how to do this?

com.Encoding = new Encoding(28591);

will give you ISO-8859-1. However, I would still *strongly* recommend
that you don't try to use this to write binary data.

Reply all
Reply to author
Forward
0 new messages