【问题标题】:How to call function when readLine is received收到readLine时如何调用函数
【发布时间】:2012-05-24 14:04:25
【问题描述】:

我的程序需要通过串口接收字符串。 问题是程序在没有输入时卡在 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()
{
}

【问题讨论】:

标签: c# serial-port readline


【解决方案1】:
    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()
    {
    }

【讨论】:

  • 谢谢,这正是我要找的!:)
猜你喜欢
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
相关资源
最近更新 更多