Just found the answer by changing
mySerialPort.DataBits = 8;
to
mySerialPort.DataBits = 7;
But I'm not sure why 8
is not working!!
Frage
I have normal code to read input from COM port:
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM1");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Debug.Print("Data Received:");
Debug.Print(indata);
}
And this code is working without any problem, but I'm receiving like this
??BH003?00
and value on the device screen 003100KG
.
So how I can format it or convert it to be readable?
Thank you.
Lösung
Just found the answer by changing
mySerialPort.DataBits = 8;
to
mySerialPort.DataBits = 7;
But I'm not sure why 8
is not working!!