Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

sync analoge reading with serial data


PeterJ Aug 18, 2011 08:13 AM

I have a serial sensor sending me packets at 31 bytes every 200 ms, and I would like to take an analogue reading every time I receive a packet from the serial device.

Here is the code to read the data from the serial device,

While (s <= nSamples) AND (Timer (3, msec, 4) < 500)
If (SerialInChk (COM1) >= 31) Then
SerialInBlock (COM1, mruHeader, 1) ' read the header
SerialInBlock (COM1, mruBuffer, 30) ' read the data

Call realMruValue(mruBuffer)

accelArray(s) = rAccelZ

LogString("INFO: MRU accel " & rAccelZ & " header 0x" & HEX(mruHeader >> 24) & " in " & Timer (3, msec, 4) & " ms" & " load " & loadArray(s))

Timer (3, msec, 2) 'reset and start timer 2

s += 1
Endif
Wend

I would like to read the analogue sensor with something like

VoltDiff(loadArray(i), 1, mV2500, 1, True, 0, _50Hz, 1.0, 0.0)

But it seems to stop working when I put this inside the while loop.

How can I syncronise the reading of an analogue sensor with the arrival of data from my serial sensor?

* Last updated by: PeterJ on 8/18/2011 @ 2:16 AM *


JDavis Aug 18, 2011 07:17 PM

I suggest looking at the help in CRBasic for WaitTriggerSequence.

You will have an unavoidable delay between the receiving the serial data and the analog measurement. It can be measured with a timer.

Also remember that there is a delay within the serial sensor between when the measurement is made and the data is output due to processing.

Measurements are easier to synchronize when the serial sensors can be polled for data instead of outputting a continuous stream.


PeterJ Aug 20, 2011 02:13 AM

Thanks for the WaitTriggerSequence hint. Is it ok to trigger this every 200 ms? What is the delay between a trigger and the sequence being run?

Both polled mode and a continuous stream have their delay problems. In Polled mode you never know when the character actually got sent or in more detail where the start and stop bits/chacter are. This is bacause the poll send also goes into a buffer as I understand. In contonuous (stream) mode you have the same problem, although on a micro an interrups could be used to detect the start bit.

I tried this,

SlowSeqence
Do
WaitTriggerSequence

VoltDiff(loadCell, 1, mV2500, 1, True, 0, _50Hz, 1.0, 0.0)
Loop

And triggering it from the main scan, but the VoltDiff command never seems to get executed.

from the CRBasic help manual, for WaitDigTrig

Note: If the program is running in sequential mode and has a slow sequence that includes a WaitDigTrig instruction, once triggered, that sequence will not be able to perform any measurement tasks when the main scan is running. The slow sequence will pause before its first measurement instruction, until the main scan is completed, after which it will continue. If the slow sequence contains only processing tasks, these tasks can run in conjunction with the main scan.

Is this the same for WaitTrigger?

* Last updated by: PeterJ on 8/19/2011 @ 9:06 PM *

Log in or register to post/reply in the forum.