How to read a value from a streaming serial port


Recommended Posts


I need to be able to read values from a streaming serial port from an instrument that measures atmospheric CO and H2 levels. On the COM monitor, the instrument outputs the follwing line:

Rx (11:37:53.529): 2010-06-04 11:37:55,10105,R,S,0,00000000,H2 ,55993,CO ,26301, , , , , , , , ,1310

I need top be able to put into 2 channels the number that comes after H2 (55993 in this example) and the number that comes after CO (26301 in this example). The slight complication is that these numbers could vary from 1 to 6 figures in length.

Can you advise on how to do this?


Link to comment
Share on other sites

Right. That's HTML for you!

OK, then its pretty easy. Use readUntil(10) to read the whole line into a string. Then use parse() to parse that string into an array of values. Parse on the comma, and pass -1 for the index to an array. Then just grab the elements you need. Something like this (in a sequence):

	  private string datain = device.mydevice.readUntil(10)
	  datain = parse(datain, -1, ",")   
	  ? strLastError

This assumes you have channels called H2 and CO with timing of 0, and you've named your serial device "mydevice". It'll have a NULL protocol.

Link to comment
Share on other sites

Thats great thanks!

A couple of questions:

The instrument outputs a string every 210 seconds so could I make the program run more efficiently by increassing the delay time?

I need to divide the numbers by 100 to get values in ppb. I have tried using a conversion for each channel but it doesn;t seem to work. Is there any reason why using a conversion shouldn't work with this way of logging the data?


Link to comment
Share on other sites

1) yes, increasing the delay would help. I'd just increase it to 1. Larger than that and you could miss a reading (not miss, more just get it really late). You might also eliminate the ? strLastError so you don't see all the timeout errors while its waiting.

2) conversions don't work when using AddValue() as DF expects you to convert the data before doing AddValue. Just add /100 outside the strtodouble() function.

Link to comment
Share on other sites

  • 1 year later...

Often people forget and put a delay() inside the try block only. Then, if there is an error, the loop runs without any delay, potentially hanging DAQFactory. In this case, I put the delay() outside the try/catch, so it doesn't matter, but its a good habit to put one in the catch() as well anyway.

Link to comment
Share on other sites

  • 4 months later...


This topic is now archived and is closed to further replies.