Hello,
I have a device which I'm interfacing via Serial Port. The device sends a response immediately after sending some bytes to the device.
Therefore I want to have a SPI-style WriteRead-Method, which sends some bytes to the device and returns the response.
I want to use AutoResetEvent.WaitOne to wait until the response is received. Is the response received calling AutoResetEvent.Set should unblock the WriteRead-Method call and returns the response.
public Byte[] WriteRead(Byte[] bytesToSend){ this.FrameReceived += Device_FrameReceived; this.Write(bytesToSend); Boolean signaled = this.autoResetEvent.WaitOne(); this.FrameReceived -= Device_FrameReceived; if (signaled) { return this.incomingBytes; } else { return null; }}
private void Device_FrameReceived(Object sender, FrameReceivedEventAgs e){ this.incomingBytes = e.Frame; this.autoResetEvent.Set();}
Using the WriteRead-Method before calling Thread.Sleep, in the Main-Method, the mechanism works. Once Thread.Sleep is called, the mechanism won't work anymore.
I attached an example application which demonstrate the problem. Maybe someone is willing to explain why the demonstration application don't work, and how to fix the problem.
To test the application, some random byte have to be generated on COM1.
P.S: It's a netduino Mini Application Projects, since I'm not using any netduino Mini specific code, it should run on any netduino, or not?