多线程和串行端口

Multithreading and Serial Ports(多线程和串行端口)

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

问题描述

好的..这会很长,但我需要先解释一些背景.

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.

我的代码适用于此..问题是,它不适用于多个项目.我的意思是,当门打开时,传感器不会被监控,直到门关闭.因此,当物品 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 
        }
      } 
  } 

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

本文标题为:多线程和串行端口

基础教程推荐