Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Threading, ResetEvent and SerialPort problem

Threading Thread AutoResetEvent ManualResetEvent WaitHandle SerialPort

  • Please log in to reply
No replies to this topic

#1 Hachi'

Hachi'

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationEurope

Posted 03 November 2013 - 09:10 PM

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?

Attached Files







Also tagged with one or more of these keywords: Threading, Thread, AutoResetEvent, ManualResetEvent, WaitHandle, SerialPort

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.