Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Non printable character in C# serial port communication.
When i will send printable character it will received full data
but when i will send nonprinatble character it will give wrong output;
C#
int x=120;
char tdata=system.convert.tochar(x);
SerialPort sp = new SerialPort("Com1", 19200, Parity.None, 8, StopBits.One);
            if (!sp.IsOpen)
            {
                sp.Open();
                sp.Write(tdata);
                sp.BaseStream.Flush();
                sp.Close();
            }

It will give right output data
C#
but when x=129; //this is non printable characters 

it will give wrong output

[Edit]Moved code updates from solution.[/Edit]
Posted
Updated 22-Aug-11 21:24pm
v4
Comments
walterhevedeich 23-Aug-11 0:57am    
Are you sure this is the exact code? I can see that you have created tdata but have used TData instead.
walterhevedeich 23-Aug-11 1:25am    
Do NOT post comments/question as solution. If you have updates to the code, use the Improve question widget instead.

1 solution

Don't convert it to char - keep it as byte.
Byte is specifically an eight-bit unsigned data format, which is exactly what you want to send. Char is a longer data type with various encodings allowed, not a 8 bit signed type as in C.
 
Share this answer
 
Comments
BobJanova 23-Aug-11 4:53am    
Good spot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900