多线程和串行端口
问题描述
好的..这会很长,但我需要先解释一些背景.
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:
这段代码工作正常,我只需要能够考虑多个项目就行了.有什么建议吗????
This code works fine, I just need to be able to account for multiple items on the line. Any suggestions????
我还尝试了以下方法:
当我这样做时,我在按下开始按钮时启动了两个线程.我得到各种异常,程序最终抛出 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
这篇关于多线程和串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!