Display and parse sensor data (text) in flex
i'm new to flex. I have a sensor that sends data as text file. Data format as follow: dat=110405120000+000.00+000.00+005.65开发者_运维技巧+000.00+040.71+000.00+000.00+000.20.
How can i parse this data? The output i want is:
date: 05-04-11 time: 12:00:00 ch01: 000.00 ch02: 000.00 ch03: 005.65 ch04: 000.00 ch05: 040.71 ch06: 000.00 ch07: 000.00 ch08: 000.20
you can use this function:
private function parse_data(input:String):Array {
input = input.replace("\x20","");
input = input.replace("dat=","");
var numbers:Array = input.split("+");
//get date
var year:String = String(numbers[0]).substr(0,2);
var month:String = String(numbers[0]).substr(2,2);
var day:String = String(numbers[0]).substr(4,2);
var date:String = day+"-"+month+"-"+year;
//get time
var hours:String = String(numbers[0]).substr(6,2);
var mins:String = String(numbers[0]).substr(8,2);
var secs:String = String(numbers[0]).substr(10,2);
var time:String = hours+":"+mins+":"+secs;
//output array
var output:Array = new Array();
output["date"] = date;
output["time"] = time;
//other chxx values
for (var index:int=1; index<numbers.length; index++) {
output["ch0"+index] = numbers[index];
}
return output;
}
this is how to call the function above:
var result:Array =
parse_data("dat=110405120000+000.00+000.00+005.65+000.00+040.71+000.00+000.00+000.20");
this is the array result:
{
date:"05-04-11",
time:"12:00:00",
ch01:"000.00",
ch02:"000.00",
ch03:"005.65",
ch04:"000.00",
ch05:"040.71",
ch06:"000.00",
ch07:"000.00",
ch08:"000.20"
}
to access this array:
var date:String = result["date"];
var time:String = result["time"];
var ch01:String = result["ch01"];
...
however ch01 above is a string, if you need integer value, you have to convert:
var ch01_value:int = int(ch01);
hope this may help
P.S.: BIG NOTE: the above function works well only if you have 'ch01' to 'ch09', if you have 'ch10' or more, you will need to amend it.
read the file: http://www.openwritings.net/content/read-text-whole-file use String.split() and other String functions: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html You can always add regular expressions to the formula too (RegExp) :]
blz
精彩评论