My application connects to a specific hardware over a serial port. I send binary data commands while each command includes specific Opcode. I receive from the hardware the same Opcode or another in return. To do this, I have a queue of commands in thread, dedicated to send the data.Another thread dedicated to reading and handling all incoming replies and put it in another queue.
Now, lets say I need to send a command with opcode 1 and then I want to wait for the hardware to reply me with opcode 1(there might be different data between sending and receiving) , however this is going to be a little tricky cause there might be some other messages being received by my application, commands with opcodes 2 or 3 and so on, and I need to handle them properly without waiting to command with opcode 1. If I wait too long to command with opcode 1 I need to be informed.
- Do I need to open a thread timer for each command, and after amount of time call back a listener?
- Is there a design pattern and implementation for this kind?
I looked for some posts here and I don't think I can stop my search Look at this: c# wait for an echo on serial port, check it, and have a timout on the wait? Providing Asynchronous Serial Port Communication?
Can anyone give me some advice? Thanks!