C#中的BackgroundWorker和COM端口问题 [英] C# BackgroundWorker and Com Port Problems

查看:182
本文介绍了C#中的BackgroundWorker和COM端口问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定。我有一个程序,监视2个COM端口。一个是钩到一个尺度,另一个是钩状到Modbus板

Ok. I have a program that monitors 2 COM ports. One is hooked to a scale and the other is hooked to a modbus board.

我的问题是在附连到的modbus板的COM端口。我的程序读取传感器(在MODBUS板)每100ms。它返回一个0或1(通过COM端口),以确定该传感器是否被阻塞。如果被阻塞,一个信号通过该端口向董事会发出。

My problem is in the COM port attached to the modbus board. My program is reading a sensor (on the modbus board) every 100MS. It returns a 0 or 1 (over the COM port) to determine whether the sensor is blocked. If it is blocked, a signal is sent over the port to the board.

我的问题是,我不能退出监视传感器,但我可以肯定的com口不使用发送我的其他信号之前。

My problem is I can't quit monitoring the sensor, but I have to be sure the com port is not in use before sending my other signal.

这是监控传感器的程序是在一个BackgroundWorker线程。一旦传感器被触发,另一个线程派生发送一个信号到Modbus板。所以,我需要暂停传感器螺纹,而我将信号发送到电路板上。我怎么会去这样做呢?

The routine that monitors the sensor is on a backgroundworker thread. Once the sensor is tripped, another thread is spawned that sends a signal to the modbus board. So I need to pause the "sensor thread" while I'm sending a signal to the board. How would I go about doing this?

记住这是一个BackgroundWorker,所以是的Thread.join不是一个选项。

Remember it is a BackgroundWorker, so Thread.Join is not an option.

下面是我的代码:

private void SensorThread_DoWork(object sender, DoWorkEventArgs e)
    {
        if (SensorThread.CancellationPending == true)
            e.Cancel = true;
        else
        {
            ReadSensor();
        }    
    }



该RunWorkerCompleted该线程刚刚重新启动线程。
以下线程持续监控sensorstatus看到当传感器被阻塞:

The RunWorkerCompleted for this thread just restarts the thread. The following thread is constantly monitoring the "sensorstatus" to see when the sensor is blocked:

public void ScaleThread_DoWork(object sender, DoWorkEventArgs e)
    {
        if (ScaleThread.CancellationPending == true)
        {
            e.Cancel = true;
        }
        else
        {
            //sensor is blocked
            if (sensorstatus == 0)
            {
                ReadScale();
                prevgate = gate;
                gate = DetermineGate();
                //SaveData();
                SetOpenDelay();
                SetDuration();
                //no gate was selected, meat out of range, runs off end
                if (gate == 0)
                {
                    txtStatus.Invoke(new UpdateStatusCallback(UpdateStatus), new object[] { meatweight.ToString() + 
                                                                                    "lbs is out of range"});
                }
                else
                {
                    //this triggers a thread to send a signal to the modbus board
                    gateTimer.Start();
                }
            }
        }
    }



RunWorkerCompleted此重新启动这个线程,使之成为一个循环

The RunWorkerCompleted for this restarts this thread, making it a loop

推荐答案

创建一个线程负责所有的发送操作。实施馈送该线程与消息发送到装置队列。您可以使用新的 BlockingCollection< T> 来轻松实现这种

Create a single thread responsible for ALL send operations. Implement a Queue that feeds this thread with messages to send to the device. You can use the new BlockingCollection<T> to implement this easily.

或者使用TPL,创建一个的TaskScheduler 如下所示: - http://msdn.microsoft.com/en-us/library/ee789351.aspx

OR, using TPL, create a TaskScheduler with a limited degree of parallelism of 1 as shown here:- http://msdn.microsoft.com/en-us/library/ee789351.aspx

现在,你可以简单地火任务的要发送到设备的时候,就会按顺序执行

Now you can simply fire of Tasks to send to the device and they will execute sequentially.

除非有一些需要进行通信的发送器和读取器之间的信息我将实施在阅读器操作其自己独立的线程。

Unless there is some need to communicate information between the sender and the reader I would implement the reader operation on its own separate thread.

这篇关于C#中的BackgroundWorker和COM端口问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆