I am working on a project which involves communication to a device through a serial port and I have to send ASCII commands.
The problem arises when I try to send ASCII characters above 127. Those do not converted to ASCII properly. I know so because I am looking at the output using a serial port monitor.
Here is the code:
namespace SensorAppCSharp
{
public partial class Form1:Form
{
SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
public Form1( )
{
InitializeComponent( );
port.ReadTimeout = 2000;
port.Open( );
}
private void button1_Click(object sender, EventArgs e)
{
try
{
port.Write("tu<SOH>");
port.Write("t<STX>");
label1.Text = "t<STX>";
}
catch(System.Exception ex)
{
label1.Text = ex.ToString( );
}
}
}
}
---End of Code
Here is the output from our Serial Port Monitor:
<SOH> <STX>
I tried all the different things that I know about. I would apprechite any help.
Thanks,
Thorben

Serial Port - ASCII Char Write Problem
Metal_Fly
I think you really should use the Write(byte[], int, int) overload of the Write method of the SerialPort class.
The way a string is converted to bytes that can be send on the wire depends on the used encoding. The default encoding used by SerialPort is ASCII encoding and ASCII encoding does not support characters with codes above 127 so for these characters it gets you the " " character instead. You could try to use a different encoding like UTF8 but I doubt that there is an encoding that accept all character codes between 0 and 255 as valid characters, that's why I say you should use the Write(byte[], int, int) method.
squimmy
Yes going with the Write(byte[], int, int) overload works perfectly. Thanks for helping me.