473,467 Members | 2,224 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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?
Nov 17 '05 #1
16 7496
halukg <ha****@discussions.microsoft.com> wrote:
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?

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

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" <ha****@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
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?

Nov 17 '05 #3
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:ef**************@tk2msftngp13.phx.gbl...

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" <ha****@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
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?


Nov 17 '05 #4
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
I am trying to transmit unicode characters and receive them without any
conversions or encoding. I mean any b

"Jon Skeet [C# MVP]" wrote:
halukg <ha****@discussions.microsoft.com> wrote:
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?

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

Nov 17 '05 #6
any binary values between 0 and 255.

"Jon Skeet [C# MVP]" wrote:
halukg <ha****@discussions.microsoft.com> wrote:
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?

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

Nov 17 '05 #7
My port initialization code is:
com = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

"Tedb" wrote:
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:ef**************@tk2msftngp13.phx.gbl...

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" <ha****@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
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?



Nov 17 '05 #8
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:
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.

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

Nov 17 '05 #9
halukg <ha****@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
halukg <ha****@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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
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:MP************************@msnews.microsoft.c om...
halukg <ha****@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?

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

Nov 17 '05 #12
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" wrote:
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:MP************************@msnews.microsoft.c om...
halukg <ha****@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?

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


Nov 17 '05 #13
OK I will check the effective encoding..

"Jon Skeet [C# MVP]" wrote:
halukg <ha****@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?

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

Nov 17 '05 #14
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" wrote:
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:MP************************@msnews.microsoft.c om...
halukg <ha****@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?

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


Nov 17 '05 #15
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" <ha****@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
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" wrote:
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:MP************************@msnews.microsoft.c om...
> halukg <ha****@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?
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too


Nov 17 '05 #16
halukg <ha****@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.

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jt | last post by:
Ref: I'm building a new character array (alerts.msg). I have a character array with a mix of binary and text characters with is "alertmsg". Some of the characters are a DLE (0x10) char that I...
1
by: arssa2020 | last post by:
hello, i am writing a GPS app in evc++ 4.0, i am using c-style char* instead of CStrings, i want to parse NMEA sentence such as "GPZDA,162913.48,19,08,2005,,*63" problem is when i use strtok it...
1
by: David | last post by:
I have written an application in VB.NET 2003 that uses the SAX serial component for RS232 communications with hardware. The program sets up 2 serial ports so that it can talk to 2 different...
2
by: joe | last post by:
Has anyone seen any source code that shows how to use the serial port in VB 2005 (System.IO.Port)? Especially how to use the asynchronous read? John
1
by: henrycortezwu | last post by:
Hi All, I'm trying to connect to a virtual port (COM19, OUTGOING, "Bluetooth Serial Port") using VS2005 System.IO.Ports. When I ran the ff code below here's what happens. 1) VS2005 Compiles w/o...
5
by: Franklin M. Gauer III | last post by:
Hi All, I've written an ASP.NET application (webservice) that does simple serial communications via the .NET 2.0 SerialComm object. The application runs fine on my development machine. The...
2
by: evle | last post by:
haw to read data from an Infrared Infrared Remote Control
3
by: solosynergy | last post by:
hi i am new to VC++ and want send data to my serial port.I am able to send String ^ type variables. but i am not able to send a single char or an array of characters. i.e char c, and char c type i am...
2
by: Nasif | last post by:
Currently I am writing a program which sends and receives messages through serial port to a device. I am using C# and Microsoft Visual studio 2005 for windows program. But my problem is when i try...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.