2

我的程序需要通过串口接收字符串。问题是程序在没有输入时卡在 ReadLine 上。当 SP.ReadLine 读取一行时,有没有办法调用函数 startReading?

void GetInput()
{
        SerialPort SP = new SerialPort();
        SP.PortName = "COM3";
        SP.Open();
        string Line = string.Empty;
        While(Line == "")
         {
                    Line = SP.ReadLine();
                    Application.DoEvents();
         }
          StartReading();
}

void StartReading()
{
}
4

1 回答 1

0
    private void GetInput()
    {
        SerialPort SP = new SerialPort();
        SP.PortName = "COM3";
        SP.DataReceived += new SerialDataReceivedEventHandler(SP_DataReceived);
        SP.Open();
    }

    private void SP_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort serialPort = (SerialPort)sender;
        string line = serialPort.ReadLine();
        StartReading();
    }

    private void StartReading()
    {
    }
于 2012-05-24T14:27:27.593 回答