为什么我的SerialPort.ReadLine()方法没有执行?

时间:2018-07-17 15:50:20

标签: c# serial-port

我试图将值存储到int对象中,但是serialPort.ReadLine()正在挂断(花费很长时间执行)。有没有办法我可以使用try-catch块或类似于测试的东西?

private void resetPos()
{
    serialPort.Write("getCurrXpos");
    int currX = Convert.ToInt32(serialPort.ReadLine());
    serialPort.Write("getCurrYpos");
    int currY = Convert.ToInt32(serialPort.ReadLine());`

    if (currX == 0 && currY == 0)
    {
    }
    else if (currX == 0 || currY == 0)
    {
        serialPort.Write("reset");
        serialPort.ReadLine();  serialPort.ReadLine();
        currXpos.Text = "0"; currYpos.Text = "0";
    }
    else
    {
        serialPort.Write("reset");
        serialPort.ReadLine(); serialPort.ReadLine();
        serialPort.ReadLine();
        currXpos.Text = "0"; currYpos.Text = "0";
    }
}

0 个答案:

没有答案