多线程和串口 [英] Multithreading and Serial Ports

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

问题描述

Ok..this将是漫长的,但我需要先解释一些背景知识。

Ok..this will be lengthy but I need to explain some background first.

我的软件中的这一部分进行排序顺着输送带项目。我使用的Modbus为传送带
。的Modbus将在特定时间打开门,让一个项目经过门。件将经过基于重量的某些门。

This part of my software is for sorting items running down a conveyor belt. I am using Modbus for the conveyor belt. Modbus will open gates at a specific time to let an item go through the gate. Items will go through certain gates based on weight.

我监测的传感器,以确定何时一个项目是在一个尺度。当传感器被阻塞,该项目被称重并发送到相应的栅极。定时器设置为打开/关闭大门。

I am monitoring a sensor to determine when an item is on a scale. When the sensor is blocked, the item is weighed and sent to the appropriate gate. Timers are set to open/close the gate.

我的代码将为this..the问题的工作,也不会为多个项目工作。我的意思,而门打开没有被监控的传感器,直到门被关闭。因此,尽管A项是在路上的大门,B项不会得到打压规模时,它阻止传感器。我可以上线多达8项的一次。下面是我现在正在运行的代码:

My code will work for this..the problem is, it will not work for multiple items. By that I mean, while a gate is open the sensor is not being monitored until the gate is closed. So while Item A is on the way to the gate, Item B will not get weighed on the scale when it blocks the sensor. I could have up to 8 items on the line at once. Here is the code I am running now:

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

private void SensorThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    //if sensor is blocked
    if (sensorstatus == 0)
    {
        //the timers just start the thread
        scaleTimer.Start();
    }
    else
    {
        sensorTimer.Start();
    }
}

private void ScaleThread_DoWork(object sender, DoWorkEventArgs e)
{
  if (ScaleThread.CancellationPending == true)
    {
        e.Cancel = true;
    }
    else
    {
        ReadScale();
        //SaveWeight();
        prevgate = gate;
        gate = DetermineGate();
        SetOpenDelay();
        SetDuration();
    }
  }

private void ScaleThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    //if gate = 0, this means the weight of meat on scale 
    //is not in any weight range. Meat runs off the end.
    if (gate == 0)
    {
        txtStatus.Invoke(new UpdateStatusCallback(UpdateStatus), new object[] { meatweight.ToString() + 
                                                                                "lbs is out of range"});
        sensorTimer.Start();
    }
    else
    {
      //open gate
      //then close gate
    }
  }

此代码工作正常,我只需要能够解释就行了多个项目。
任何建议????

This code works fine, I just need to be able to account for multiple items on the line. Any suggestions????

我也试过如下:

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

private void SensorThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
  sensorTimer.Start();
}

  private void ScaleThread_DoWork(object sender, DoWorkEventArgs e)
{
  if (ScaleThread.CancellationPending == true)
    {
        e.Cancel = true;
    }
    else
    {   
        //sensor blocked
        if (sensorstatus == 0)
        {
          ReadScale();
          //SaveWeight();
          prevgate = gate;
          gate = DetermineGate();
          SetOpenDelay();
          SetDuration();

          //if gate = 0, this means the weight of meat on scale 
          //is not in any weight range. Meat runs off the end.
          if (gate == 0)
          {
            txtStatus.Invoke(new UpdateStatusCallback(UpdateStatus), new object[] { meatweight.ToString() + 
                                                                                "lbs is out of range"});
          }
          else
          {
            //open gate
            //close gate
          }
    }
}

private void ScaleThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
   scaleTimer.Start();
}

当我这样做,我开始两个线程按下启动按钮时。我得到的各种异常,程序最终引发SEHException和崩溃。其他的错误我收到说串行口已经打开或I / O错误。

When I did this, I started both threads when the start button was pressed. I get all kinds of exceptions and the program eventually throws the SEHException and crashes. The other errors I get say "Serial port already opened" or "I/O Error".

推荐答案

我想你需要的东西喜欢这个。不知道是否需要的锁,但我加了他们的安全,因为你得到的错误

I think you need something like this. Don't know if the locks are needed but I added them for safety since you are getting errors

private void SensorThread_DoWork(object sender, DoWorkEventArgs e) 
{
    int sensor = 1;
    while(!SensorThread.CancellationPending == true) 
    {
        int newSensor;
        lock(this)
        {
            newSensor = ReadSensor(); 
        }

        //sensor state changed
        if(newSensor != sensor)
        {
            //sensor was 1 and changed to 0
            if(newSensor==0)
            {
               scaleTimer.Start(); 
            }
            sensor = newSensor;
        }
        Thread.Sleep(1);
    }
    e.Cancel = true; 
}     

private void ScaleThread_DoWork(object sender, DoWorkEventArgs e) 
{ 
    //sensor blocked 
    //if (sensorstatus == 0) 
    { 
        lock(this)
        {
            ReadScale(); 
        }
        //SaveWeight(); 
        prevgate = gate; 
        gate = DetermineGate(); 
        lock(this)
        {
            SetOpenDelay(); 
            SetDuration(); 
        }

      //if gate = 0, this means the weight of meat on scale  
      //is not in any weight range. Meat runs off the end. 
      if (gate == 0) 
      { 
        txtStatus.Invoke(new UpdateStatusCallback(UpdateStatus), new object[] { meatweight.ToString() +  
                                                                            "lbs is out of range"}); 
      } 
      else 
      { 
        lock(this)
        {
        //open gate 
        }
        lock(this)
        {
        //close gate 
        }
      } 
  } 

这篇关于多线程和串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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