【问题标题】:C# SerialPort DataReceived Event not updating main UIC# SerialPort DataReceived 事件未更新主 UI
【发布时间】:2012-09-02 22:19:14
【问题描述】:

我目前正在开发一个完全用 c# 编写的防盗报警解决方案。该程序与基于 USB 串行端口的 IO 板通信,该 IO 板具有硬连线的警报传感器。我遇到了一个问题,即 DataReceived 事件无法更新主 UI,除非我从 DataReceived 事件中调用一个名为 TextLog 的子(见末尾)。奇怪的是,来自查询区域 1 的 DataReceived 事件能够更新主 UI,但不能更新区域 2 或 3。此外,如果我在执行串行端口写入的行插入断点,它会按预期工作。

值得一提的是这些全局变量:

string ioCardRxString = "";

bool[] arrGlobalZoneStatus = new bool[4];

通过从设置文件中读取设置来打开串口(一切正常)。

private void OpenIOComPort()
    {
        bool error = false;

        else
        {
            // Set the port's settings
            spIOCard.PortName = Settings1.Default.ioComPort;
            spIOCard.BaudRate = int.Parse(Settings1.Default.ioBaudRate);
            spIOCard.DataBits = int.Parse(Settings1.Default.ioDataBits);
            spIOCard.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), Settings1.Default.ioStopBits);
            spIOCard.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), Settings1.Default.ioParity);
            spIOCard.Handshake = (System.IO.Ports.Handshake)Enum.Parse(typeof(System.IO.Ports.Handshake), Settings1.Default.ioHandshake);

            try
            {
                // Open the port
                spIOCard.Open();
            }
            catch (UnauthorizedAccessException) { error = true; }
            catch (System.IO.IOException) { error = true; }
            catch (ArgumentException) { error = true; }

            //On error, advise the user
            if (error)
            {
                MessageBox.Show("Could not open the I/O Board COM port.");                    
                globalIOCardError = true;
            }

            if (!error)
            {
                globalIOCardError = false;
                // Do Nothing
            }

        }
    }

定时器每 500 毫秒运行一次,它向串口写入 3 条命令,每条命令之间相隔 25 毫秒(硬件限制)。这些命令查询 IO 板以确定每个警报传感器的状态。

private void tmrAuditSensors_Tick(object sender, EventArgs e) 
    {
        try
        {
            if (globalIOCardError == false && Settings1.Default.disableSensorAudit == false)
            {
                if (Settings1.Default.zone1Armed == true)
                {
                    spIOCard.Write("~in01~");
                    System.Threading.Thread.Sleep(25);;
                }

                if (Settings1.Default.zone2Armed == true)
                {
                    spIOCard.Write("~in02~");
                    System.Threading.Thread.Sleep(25);;
                }

                if (Settings1.Default.zone3Armed == true)
                {
                    spIOCard.Write("~in03~");
                    System.Threading.Thread.Sleep(25);;
                }

                //Applicable results will appear on serial data received event
            }
        }
        catch
        {
            textLog("There was a problem writing to the serial port, check and restart app");
            emergencyHalt();
        }
    }

串行端口 DataReceived 事件读取返回的字符串并写入全局布尔数组。如果该区域打开 (=1),则为 True,如果该区域关闭 (=0),则为 False。注意:星号线。

private void spIO_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        ioCardRxString = spIOCard.ReadExisting();
        textLog(ioCardRxString); //*Cannot make it work without this

        if (ioCardRxString.Contains("in05=1") == true)
        {
            arrGlobalZoneStatus[1] = true;
        }

        if (ioCardRxString.Contains("in05=0") == true)
        {
            arrGlobalZoneStatus[1] = false;
        }

        if (ioCardRxString.Contains("in01=1") == true)
        {
            arrGlobalZoneStatus[2] = true;
        }

        if (ioCardRxString.Contains("in01=0") == true)
        {
            arrGlobalZoneStatus[2] = false;
        }

        if (ioCardRxString.Contains("in17=1") == true)
        {
            arrGlobalZoneStatus[3] = true;
        }

        if (ioCardRxString.Contains("in17=0") == true)
        {
            arrGlobalZoneStatus[3] = false;
        }

    }

另外,另一个计时器会定期(每 250 毫秒)检查每个数组成员的内容,然后相应地通过一些颜色更改和文本更新来更新主 UI。

 private void tmrCheckZoneStatus_Tick(object sender, EventArgs e)
    {
        if (arrGlobalZoneStatus[1] == true)
        {
            button10.BackColor = System.Drawing.Color.Red;
            textLog(button10.Text + " was activated");

            if (globalFullAlarmSet || globalNightAlarmSet || globalDoorsAlarmSet)
            {
                this.BeginInvoke(new EventHandler(delegate { checkAndActivateRelays(1); }));
            }
        }

        if (arrGlobalZoneStatus[1] == false)
        {
            button10.BackColor = System.Drawing.Color.Gray;
        }

        if (arrGlobalZoneStatus[2] == true)
        {
            button11.BackColor = System.Drawing.Color.Red;
            textLog(button11.Text + " was activated"); ;

            if (globalFullAlarmSet || globalNightAlarmSet || globalDoorsAlarmSet)
            {
                this.BeginInvoke(new EventHandler(delegate { checkAndActivateRelays(2); }));
            }
        }

        if (arrGlobalZoneStatus[2] == false)
        {
            button11.BackColor = System.Drawing.Color.Gray;
        }

        if (arrGlobalZoneStatus[3] == true)
        {
            button12.BackColor = System.Drawing.Color.Red;
            textLog(button12.Text + " was activated");

            if (globalFullAlarmSet || globalNightAlarmSet || globalDoorsAlarmSet)
            {
                this.BeginInvoke(new EventHandler(delegate { checkAndActivateRelays(3); }));
            }
        }

        if (arrGlobalZoneStatus[3] == false)
        {
            button12.BackColor = System.Drawing.Color.Gray;
        }

    }

textLog 子:

public void textLog(string logEntry)
    {
        textLines++;
        try
        {
            if (this.txtLog.InvokeRequired)
            {
                ChangeTextCallback MethodCallback = new ChangeTextCallback(textLog);
                this.Invoke(MethodCallback, new object[] { logEntry });
            }
            else
            {
                if (!logEntry.Contains("?"))
                {
                    txtLog.Text = txtLog.Text + DateTime.Now + " >: " + logEntry + "\r\n";
                    txtLog.SelectionStart = txtLog.Text.Length;
                    txtLog.ScrollToCaret();

                    if (textLines > 3000)
                    {
                        txtLog.Clear();
                        textLines = 0;
                        textLog("Text log cleared");
                    }

                    System.IO.StreamWriter sw = new System.IO.StreamWriter(logFile, true);
                    try
                    {
                        sw.WriteLine(DateTime.Now + " >: " + logEntry);
                    }
                    catch (Exception ex)
                    {
                        //
                    }

                    sw.Close();
                }
            }
        }
        catch
        {
            //
        }


    }

我想我需要在某个地方合并调用/委托,但作为一个菜鸟,我有点摸不着头脑。我们将不胜感激。

谢谢

【问题讨论】:

  • 不想听起来像个混蛋,但我们并不关心您系统上的所有复杂细节。能否请您简化问题,并仅显示可以说明您遇到的问题的最少代码和详细信息?

标签: c# delegates serial-port multithreading


【解决方案1】:

我没有通读所有内容,但这应该会让你走上正确的道路:

SerialPort.DataReceived 事件在单独的线程上引发。如果您需要异步处理数据的接收,并在其上使用 GUI 执行某些操作,您可以执行以下操作:

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) {
    var port = (SerialPort)sender;
    string data = port.ReadExisting();

    UpdateGui(data);
}

private void UpdateGui(string data) {
    if (this.InvokeRequired) {
        this.Invoke(new Action( d => UpdateGui(d) ));
        return;
    }

    this.txtBox1.Text = data;
}

现在,就是说.... 你真的想使用DataReceived吗?听起来你(主机)正在启动与外部板的所有通信。如果是这种情况,那么我建议您改用同步(阻塞)读取:

1. Write the request out the port
2. Call read() with the expected number of bytes
3. Process the reply.

【讨论】:

  • 确实;一旦串行端口缓冲区包含一些字节,DataReceived 将立即通知您,但它们可能是不完整的消息,您最终可能会丢失消息。如果硬件在其输出之间提供了一些分隔符,我建议您阅读直到遇到这些分隔符,以确保您正在处理完整的消​​息。这应该可以简化事情并使您免于异步头痛。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
相关资源
最近更新 更多