Okay .. this will be lengthy, but I need to explain some background first.
This part of my software is designed to sort items passing through a conveyor belt. I use Modbus for conveyor belt. Modbus will open the gate at a specific time so that the element passes through the gate. Elements will pass through specific gates based on weight.
I control the sensor to determine when the item is on the scale. When the sensor is blocked, the element is weighed and sent to the appropriate gate. Timers are set to open / close the gate.
My code will work for this. The problem is that it will not work for multiple items. By this, I mean that when the gate is open, the sensor is not monitored until the gate is closed. Therefore, when item A is on its way to the gate, item B will not be weighted on a scale when it locks the sensor. I could have up to 8 items on the line right away. Here is the code I'm 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 consider a few elements in a line. Any suggestions????
I also tried the following:
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 {
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 a SEHException and crashes. Other errors I get say: "Serial port is already open" or "I / O error."