Getting Serial Data

rpmaurer

New member
Joined
May 14, 2012
Messages
4
Programming Experience
Beginner
Ok, essentially, Im trying to pull in Serial data from an Arduino board. The board is sending a code every 28ms. I would like to grab this data as it comes in in real time. Preferably with an interval of 1-50ms. What is the best way of doing this? I have tried multi-threading, timers, etc... either nothing works or (more likely) I'm doing something wrong!

Please help!

Ryan
 
Hi,

I would suggest that you need to rethink your logic a bit since the documentation clearly shows the Port being opened on the main thread but then also goes on to explain that the data received is on a secondary thread. If you then need to update something on the main thread then you will need to use Invoke:-

The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.

Hope that helps.

Cheers,

Ian
 
Back
Top