gpt4 book ai didi

c# - 收到 readLine 时如何调用函数

转载 作者:太空宇宙 更新时间:2023-11-03 16:31:13 25 4
gpt4 key购买 nike

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

最佳答案

    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()
{
}

关于c# - 收到 readLine 时如何调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10739060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com