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.

Hachi'

Member Since 03 Nov 2013
Offline Last Active Feb 09 2015 07:43 PM
-----

Topics I've Started

Free article: Windows Phone & netduino

18 February 2014 - 09:37 PM

Hi,

 

there is a free article published in a popular german .NET magazine regarding netduino and Windows Phone: http://www.dotnetpro...rticle4787.aspx

 

 

Happy reading.


Threading with ResetEvent and SerialPort problem

04 November 2013 - 08:20 PM

Hello, I'm interfacing a device via SerialPort. The device will send a response immediately after sending some bytes to the device. Therefore I want a SPI-style WriteRead-Method, which sends some bytes, reading and returning the response-bytes. I'm trying to achive this with the AutoResetEvent class and the DataReceived event. The WriteRead-Method sends some bytes to the device, blocks the call until receiving a response, using AutoResetEvent.WaitOne and AutoResetEvent.Set.
public Byte[] WriteRead(Byte[] bytesToSend){    this.FrameReceived += Device_FrameReceived;    this.Write(bytesToSend); // Send bytes to the device    Boolean signaled = this.autoResetEvent.WaitOne(); // Wait for response (FrameReceived event)    this.FrameReceived -= Device_FrameReceived;    if (signaled)    {        return this.incomingBytes;    }    else    {        return null;    }}
After receiving the bytes, AutoResetEvent.Set is called, to unblock the WriteRead-Method.
private void Device_FrameReceived(Object sender, FrameReceivedEventAgs e){     this.incomingBytes = e.Frame;     this.autoResetEvent.Set();}
The code above works if it's executed before Thread.Sleep is called in the Main-Method. If the code is executed after Thread.Sleep, the code doesn't work. Device_FrameReceived is never called.
public static void Main(){    device = new Device();    device.FrameReceived += device_FrameReceived;     // This will work, because Thread.Sleep isn't called    // Byte[] bytes = device.WriteRead(bytesToSend);    Thread.Sleep(Timeout.Infinite);}private static void device_FrameReceived(Object sender, FrameReceivedEventAgs e){    // This won't work, beacause Thread.Sleep is already called    Byte[] bytes = device.WriteRead(bytesToSend);}
I attached a sample application which demonstrates the problem, maybe someone is willing to explain me the problem and how to solve it.

Threading, ResetEvent and SerialPort problem

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?

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.