C#串行端口以符号的形式接收数据

时间:2016-05-14 08:29:43

标签: c# serial-port communication

我有一个arduino RFID enter code here读卡器通过serail端口向我发送数据,但我

不接收正确形式的数据,如0001685839 025,47439

请帮我的代码是这个

using System;
using System.IO.Ports;

class PortDataReceived
{
    public static void Main()
    {
        SerialPort mySerialPort = new SerialPort("COM3");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.RtsEnable = true;

        mySerialPort.DataReceived += new     SerialDataReceivedEventHandler(DataReceivedHandler);

        mySerialPort.Open();
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }

    private static void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.Write(indata);
    }
}

2 个答案:

答案 0 :(得分:0)

您收到了哪些数据? com端口有一个缓冲区的概念。如果数据不适合端口缓冲区,最终可能会有两个读取事件。

您可以尝试这样的事情:

from subprocess import check_output
user_name = check_output('whoami').strip()
print user_name
#out: userX

答案 1 :(得分:0)

相关问题