jamesdlee Posted June 4, 2010 Share Posted June 4, 2010 Hi, 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? Thanks Link to comment Share on other sites More sharing options...
AzeoTech Posted June 4, 2010 Share Posted June 4, 2010 Is there a carriage return, line feed, or other delimiter to mark the end of the line? Link to comment Share on other sites More sharing options...
jamesdlee Posted June 5, 2010 Author Share Posted June 5, 2010 Yes sorry my copy and paste of the serial output line did not come out properly! The '1310' is supposed to represent the ascii characters 13 (carridge return) and 10 (line feed) which end the line. Link to comment Share on other sites More sharing options...
AzeoTech Posted June 7, 2010 Share Posted June 7, 2010 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): while(1) try private string datain = device.mydevice.readUntil(10) datain = parse(datain, -1, ",") H2.addvalue(strToDouble(datain[6])) CO.addvalue(strToDouble(datain[8])) catch() ? strLastError delay(0.5) endcatch delay(0.05) endwhile 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 More sharing options...
jamesdlee Posted June 7, 2010 Author Share Posted June 7, 2010 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? Thanks Link to comment Share on other sites More sharing options...
AzeoTech Posted June 7, 2010 Share Posted June 7, 2010 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 More sharing options...
silogarrett Posted April 12, 2012 Share Posted April 12, 2012 Guru, What's the reason for putting delays in the Catch statements? Mike Link to comment Share on other sites More sharing options...
AzeoTech Posted April 12, 2012 Share Posted April 12, 2012 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 More sharing options...
Simone88 Posted August 15, 2012 Share Posted August 15, 2012 "Often people forget and put a delay() inside the try block only." That was my problem Thanks for pointing the issue out Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.