重量级设备通过COM到USB端口输入不可读

时间:2014-03-31 09:44:21

标签: c# visual-studio input port

我有正常的代码来读取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这样的值。

那么我如何格式化或将其转换为可读?

谢谢。

1 个答案:

答案 0 :(得分:1)

通过更改

找到了答案
mySerialPort.DataBits = 8;

mySerialPort.DataBits = 7;

但我不确定为什么8无效!!