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

G

Guest

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

Jon Skeet [C# MVP]

halukg said:
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?

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

Tedb

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

Tedb

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 said:
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 said:
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?
 
J

Jon Skeet [C# MVP]

Tedb said:
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.
 
G

Guest

I am trying to transmit unicode characters and receive them without any
conversions or encoding. I mean any b

Jon Skeet said:
halukg said:
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?

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

Guest

any binary values between 0 and 255.

Jon Skeet said:
halukg said:
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?

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

Guest

My port initialization code is:
com = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

Tedb said:
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 said:
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 said:
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?
 
G

Guest

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

Jon Skeet [C# MVP]

halukg said:
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?
 
T

Tedb

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
 
G

Guest

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

Tedb said:
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
 
G

Guest

OK I will check the effective encoding..

Jon Skeet said:
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...


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

Guest

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 said:
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
 
T

Tedb

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 said:
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?
 
J

Jon Skeet [C# MVP]

halukg said:
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.
 

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