我有正常的代码来读取COM端口的输入:
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);
}
此代码正常运行,但我在设备屏幕
??BH003?00
上收到了003100KG
这样的值。
那么我如何格式化或将其转换为可读?
谢谢。
答案 0 :(得分:1)
通过更改
找到了答案mySerialPort.DataBits = 8;
到
mySerialPort.DataBits = 7;
但我不确定为什么8
无效!!