I'm having a problem trying to solve how to split data received from serial port. The basic idea is to receive measurement data from serial port and then save it to .xml file.
So far I have other "modules" in my software working but I haven't yet figured how to split the data and save it to the .xml file properly. The data I receive is a list of measurement values separated by comma and with start and stop letters. length and form of the received data will be always same, only measurement values change...
So it'll look like this: &,24,21,53,54,33,24,36,37,54,33,33,22,33,23,$
How should this be splitted so that it would be easiest to save to .xml file?
form of the .xml file is something like:
I'm quite new to C#, previously I have coded only C-language and some HTML stuff, so my ideas might have been a bit too "conservative" ;)
- Vertti
Samuel VaesyPosted Sep 13, 2008, 8:40 AM
Thanks for helping, now I've getting to understand this "language" a bit better :)
This code example you gave me works so far quite well, but there is one thing which I believe didn't occur in my example of the contents of the original .xml file, and because of that this example of yours doesn't work like my actual idea was originally.
There are several temperature sensors in my "project" and they need to have somekind of description. And in my example this
Is there some neat way to make a copy of the original file and update measurement values to it?
AlanPosted Aug 28, 2008, 10:09 AM
It's best to split the values first (assuming the start and end characters are present) and then iterate through the resulting array and add them to the xml file. Something like this:
using System.Xml;
// ....
string data = SP.ReadExisting();
if (data.StartsWith("&,") && data.EndsWith(",$"))
{
string[] values = data.Substring(2, data.Length - 4).Split(',');
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create("measurement.xml", settings))
{
writer.WriteStartElement("measurement");
for (int i = 0; i < values.Length; i++)
{
writer.WriteStartElement("sensor" + (i + 1).ToString());
writer.WriteStartElement("name");
writer.WriteString("temp" + (i + 1).ToString());
writer.WriteEndElement(); //
writer.WriteStartElement("value");
writer.WriteString(values[i].ToString());
writer.WriteEndElement(); //
writer.WriteEndElement(); //
}
writer.WriteEndElement(); //
writer.Flush();
}
}
Using the specimen data you posted earlier, the resulting xml file should look like this:
As I wasn't sure what to put in the childnodes, I've just put something generic for now.
Samuel VaesyPosted Aug 27, 2008, 9:17 AM
A microcontroller measures temperatures from certain process with several different sensors, after measurements are ready, it sends those values to serial port, and it adds starting mark "&" and an end mark "$". Amount of sensors is always the same(if it does matter), and value can be either 1, 2 or 3 numbers long.
These measurement values needs to be stored to the .xml file, so that first value after starting mark(&) goes to the
So, is it neater to first copy these splitted values to to some array and then save to .xml file, or to save one value at a time during the "splitting process"?
So, would something like this work?
string received = SP.ReadExisting();
string[] values = received.Split(',');
Also it would be good to have this to start the splitting only if the first received mark is "&" and if it doesn't receive "$" after n numer of values it "resets"... but this I can probably figure out myself...
- Vertti
AlanPosted Aug 27, 2008, 7:22 AM
Well, splitting the data received is easy enough:
string data = "&,24,21,53,54,33,24,36,37,54,33,33,22,33,23,$";
string[] values = data.Substring(2, data.Length - 4).Split(',');
though I'm not entirely clear what goes where in the XML file or can you sort that bit out yourself?