08-28-2019 11:09 PM
We know that one serial port is OCCUPIED by one instance before released.What is INSTANCE? Not include a thread?Like the example below,will there be error in one of the 2 loop?
Thanks .
08-29-2019 12:46 AM - edited 08-29-2019 12:49 AM
Hi avater,
what's the reason to open a single port twice?
Why not open just once before the loops?
(Using a shared resource like a serial port in parallel is screaming for problems. Create your own handler routine which handles the serial port and use queues etc. for communication between the handler and your other loops.)
08-29-2019 02:31 AM - edited 08-29-2019 02:33 AM
@avater wrote:
We know that one serial port is OCCUPIED by one instance before released.What is INSTANCE? Not include a thread?Like the example below,will there be error in one of the 2 loop?
Yes one of them will return an error since it is already opened by the other. Which one of the two it will be will be random.
Even if you would take the initialization outside of the loops and try to communicate from both loops with the same device you would indeed cause lots of trouble since device communication is usually bidirectional and you may end up reading answers from the device to commands that the other loop sent to it, messing up the entire communciation between each loop with the device.
The way to implement device communication with a single device from multiple locations in your app is by creating a service or deamon for your device, which is basically one single engine (can be a loop on your diagram or a loop inside a subVI that keeps running until it receives the shutdown command) and implement a message based command-response interface to this service by using queues for the commands and notifiers for possible responses that you want to send back to the command sender. Look for discussions about Producer-Consumer templates to get more information about possible examples and implementations.
Sounds complicated? Not really but it is some work for sure, but that is what programming is about in the end.
08-29-2019 04:08 AM
Thanks very much for your comment.
I know the serial port,and completely agree your way to play it.
The thing is that 2 while-loop mean 2 threads,but how can a single resource be operated without error in 2 threads.
I SUPPOSED one of the while-loop will result an error ,actually not.
08-29-2019 04:11 AM
Sorry the example is not clear enough.
Maybe we can just discuss the good way to OPERATE serial port in 2 threads,not just OPEN.
08-29-2019 04:20 AM
08-29-2019 06:16 AM
An option is to move all communication to an Action Engine (AE) and use that in both loops. That way each loop will be forced to wait until the device is free (as i suspect it's a "Send command and then recieve result" type of communication).
/Y
08-29-2019 07:00 AM
Your code won't throw an error out on the indicator. The error will be how your code behaves when add the code to read and write to the serial port. Suppose one loops sends a command "Read A", the other sends a command "Read B". And then after that both have a Read function to read the response. It would be a pure race condition as to which loop writes its command first, and which read function gets the Read response. Loop A might wind up reading the response intended for B and vice versa.
Keeping the serial communication in its own loop and using a producer consumer architecture to send it commands from both loops is a good concept. The action engine concept is also good as long as you keep all the read/write functions together within a specific action so that the action engine doesn't return until it has received the response for the command it had sent. The action engine then blocks the other loop from using it until the action for that first loop has been fully completed because action engines are non-reentrant subVI's.