I use a serial connection to control a device in the lab in two ways: for periodic data transfers (automatic) but also sporadically (triggerred by user) for sending control commands to the device. Since the control commands can occure at any time, there can be a conflict that both types of communication block/corrupt each other, e.g. the reply of the device is linked to the wrong command. The serial communication is embedded in an graphical application (developped with AppDesigner) but the issue is not related to this implementation.
More information about the two types of communication
- every second I read out about 20 status parameters from the device (each parameter is read out by sending a specifc command, e.g. 'read temp', and the device replies the current value as a string, e.g. '28.94')
- The control commands are a simple string, e.g. 'set temp 32'. The device confirms the command by sending 'ACK' back.
I tried to tackle this issue in the following way
- Before sending a command via serial, check whether the transferStatus of the serial object is 'idle'.
- If it is idle, send the command and read-out the reply of the device.
- If it is not idle, set-up a timer (execution mode 'singleShot') which tries the same command after a specified time, e.g. 0.01 s. Delete the timer after its execution.
With this i managed that no command is "forgotten" but it still happened that the reply of the device were linked to the wrong command.
I'm looking for a design pattern that synchronizes both types of communication and hence prevents conflicts between them, e.g. which ensures that a user-triggered control command does not corrupt a currently ongoing data transfer but sends the command directly after the data transfer is completed.
This issue is so basic that I expected to find many examples in the web/literature/MATLABCentral but I wasn't successful. Maybe it is just due to wrong search items...
I would be happy if somenone could help me out.
Thanks in advance!