Hi
I need a pointer in the right direction.
I receive a string of data from hardware in the following format
" 01 1 2 1 0340 10 33 56 08 09 20 09 0002 0010 0000 0303 0303 0600 0000 0600 0000 0349 0302 0349 0301 0349 0303 0349 0203 0413 0500"
I need to put these values or words in an array with 5 elements. The non numerical char is a Ascii 4 to signal start of the string and the vertical lines is ascii 5 to signal a new element in the array. 4 elements will have a fixed lenght with the same amount of words everytime. the fourth element needs to be variable and receives two columns of words, each one is the temperature of a probe, problem is the recorder sometimes take temp reading for an hour and sometimes up to 5 hours at 5 second intervals. So this is why the fourth element must take a variable lenght.
these words need to be parsed into a float and used to draw a graph which should not be too difficult.
I just need help on how to get the string above into an 5 element array.
Any Ideas?

Hi,

I would try to split it into groups with use of Regular expressions.
I did not really understand what should the goal array look like, maybe you could make an example with marking the variable parts and I could try to fiture out the RegExp for it.

Edited 7 Years Ago by gotschai: n/a

Thanks
This might help. I attach the protocol in the text file.
There are basicaly 5 groups of data. I need to display 4 of the 5 groups just the value in a textBox. The fourth group's data I need to seperate from the rest as it needs to draw 2 curves in zedgraph. You will see that a ASCii 4 caracter is returned to the port after it was polled to dump the data from the datalogger. then the first group of values are send, followed by a ASCii5 to mark the next group and so forth. The manufacturer has working software, but it does not work in Vista, so he has to get it rewritten. No old source code and the guy who did it for him is probably dead or retired now. We cant get hold of him. There are too many of these recorders in the field to change the way he is dumping his data.

What language is the original software written in?

Also take the buffer you receive and write it to a text file and upload it here.

Edited 7 Years Ago by sknake: n/a

Comments
Most helpfull person on the forums

I see the control characters near the header and then a lot of text afterwards.. I thought these control characters would repeat evenly through the buffer, or does that only signify the start of a transmission?

Also is this the byte[] array written to a file or did you read this in as a string and write that? You should provide the raw bytes if possible

Lets call the string starting witt chr(004) and ending with Temperature probe serial number a message.
Are you able to wait till you get the whole message or you must generate the graph in realtime as the temp data flows as it can take long to complete?

Hi, this does the job if you have int in a file...or somewhere else where you can read the message and process it then.

string input = "";
            StreamReader sr = new StreamReader("c:/Vm_data/temp.txt");
            
            while (sr.Peek() >= 0)
            {

                input += sr.ReadLine();
                
            }
            string[] data = input.Split(Convert.ToChar(005));

You just need then to split the data[3] to the pairs xxxx yyyy, so you know what is what. I hope it helped. If you have more messages at once, you can split them with Char(004) and then use foreach or something similar, what fits your needs

Edited 7 Years Ago by gotschai: adding smth

I see the control characters near the header and then a lot of text afterwards.. I thought these control characters would repeat evenly through the buffer, or does that only signify the start of a transmission?

Also is this the byte[] array written to a file or did you read this in as a string and write that? You should provide the raw bytes if possible

the first character is a ASCII004 and signal a confirmation that the the data is dumping to the buffer, the other control characters is a ASCII 005 which signals the beginning of a new group of data, basically a seperator between the data groups for the old software.

I just read it in as a string from the buffer.
The last file "temp.txt" that I have uploaded is basically the raw data I get. I just used StreamWriter to write the string to the file.

Lets call the string starting witt chr(004) and ending with Temperature probe serial number a message.
Are you able to wait till you get the whole message or you must generate the graph in realtime as the temp data flows as it can take long to complete?

No I get all the data first in a string and then I need to draw the graph, nothing realtime. The rocorder does it's run on the line and after that gets connected to the PC for the download.

Hi, this does the job if you have int in a file...or somewhere else where you can read the message and process it then.

string input = "";
            StreamReader sr = new StreamReader("c:/Vm_data/temp.txt");
            
            while (sr.Peek() >= 0)
            {

                input += sr.ReadLine();
                
            }
            string[] data = input.Split(Convert.ToChar(005));

You just need then to split the data[3] to the pairs xxxx yyyy, so you know what is what. I hope it helped. If you have more messages at once, you can split them with Char(004) and then use foreach or something similar, what fits your needs

So what you suggest is that I read the buffer, store it in a string and then save it to a text file.
that I understand. How is the array going to look then and how do I split data[3] . Sorry i am not familair with the split method. Is the array with your solution multi dimensional or jagged. Looks like jagged

Edited 7 Years Ago by procomp65: n/a

You do not have to store it in a file. If you have it in a string variable, then just do string.split(delimiter), like in the example I wrote. Split works like in other languages (PHP, VBS, C++). It just divides the string at places where the delimiter is and assignes to an array (indexing starts with zero). Try the example I wrote, and watch the variables in a debuger. I personally would simply use split again (string[] temp = data[3].Split(' ')). arrays can be multidimensional (temp[x][y], where x can be Temperature chart index, and y 0 for Product temp and 1 for Spray temp ), but my opinion is you do not have to use them. It is simpler to split the data[3] into one array and use the odd indexes as product temp and even as spray temp.

Thanks Gotschai
Everything makes completely sense what you suggest. I tried it, but get an error " Illegal characters in path". I suppose its the Ascii characters - delimiters thats causing that. Any suggestions or don't I understand the statement incorrectly

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
         {
             this.Invoke(new EventHandler(DoUpdate));
            
             
         }

        private void DoUpdate(object s, EventArgs e)
        {
            //statusBar1.Text = "Getting Data";
            //statusBar1.Refresh();
            
            PortData = serialPort1.ReadExisting();
            string input = "";
            StreamReader sr = new StreamReader(PortData);

            while (sr.Peek() >= 0)
            {

                input += sr.ReadLine();

            }
            string[] data = input.Split(Convert.ToChar(005));

I have changed the code to only this.

string[] data = PortData.Split(Convert.ToChar(005));
            textBox10.AppendText(data[0]);

and it works, except it does not create a multidimensional array. It only creates data[0] and the ASCII005 character gets filtered out and an extra space created.

Hi, Illegal characters in path usually means that there is a space somewhere (if you get the error i the stream opening lines).

Hi, so under data[3] you have the measured values I guess. As far as I know it will not automatically create multi dimensional arrays. You need to do a little 'asistance' if you really need it in a multi array.

yes error is in the stream opening line.
no the measured values are in data[0] and all the other data, data[1 to 4] does not exist.
How do I access the measured values if its not in a mulidimensional array?

Edited 7 Years Ago by procomp65: n/a

solved the illegal characters in path. the code was way too quick for the serial port. had to do a thread.sleep, then everything was solved.
thanks for the help guys

This article has been dead for over six months. Start a new discussion instead.