Results 1 to 9 of 9

Thread: I need help Parsing strings in C#

  1. #1

    I need help Parsing strings in C#

    I have the following output from a serial connection:

    !!!AN0:0,AN1:0,AN2:0,AN3:0,AN4:101,AN5:0,RLL:0.00, PCH:-0.17,YAW:0.09,LAT:279601152,LON:-827158912,ALT:33.59,COG:90.00,SOG:0.00,FIX:1***

    I'm reading in the serial Data as "RxString" then I'm trying to "Split" using "comma or ," as the delimiter
    When I "AppendText" using tmpString I get results all over the board, When I stick with the original RxString it prints the whole string as read in perfectly fine.

    Code:
    private void DisplayText(object sender, EventArgs e)
            {
                textBox1.AppendText(tmpString);
                
            }
    
            private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                RxString = serialPort1.ReadExisting();
                string[] sensorData = RxString.Split(',');
                tmpString = sensorData[0];
                this.Invoke(new EventHandler(DisplayText));
                
            }
    My understanding is that
    Code:
    string[] sensorData = RxString.Split(',');
    should output strings to the array... I know I'm missing something easy can anyone give me a hand here?

  2. #2

    Re: I need help Parsing strings in C#

    Ok I've made some changes to my code and gotten it to parse... here is the new code:


    Code:
    private void DisplayText(object sender, EventArgs e)
            {
                textBox1.AppendText(RxString);
                textBox1.AppendText("\r\n");
                string[] an0 = sensorData[0].Split(':');
                an0textbox.Text = an0[1];
                string[] an1 = sensorData[1].Split(':');
                an1textbox.Text = an1[1];
                string[] an2 = sensorData[2].Split(':');
                an2textbox.Text = an2[1];
                string[] an3 = sensorData[3].Split(':');
                an3textbox.Text = an3[1];
                string[] an4 = sensorData[4].Split(':');
                an4textbox.Text = an4[1];
                string[] an5 = sensorData[5].Split(':');
                an5textbox.Text = an5[1];
                string[] rll = sensorData[6].Split(':');
                rolltextbox.Text = rll[1];
                string[] pch = sensorData[7].Split(':');
                pitchtextbox.Text = pch[1];
                string[] yaw = sensorData[8].Split(':');
                yawtextbox.Text = yaw[1];
                string[] lat = sensorData[9].Split(':');
                lattextbox.Text = lat[1];
                string[] lon = sensorData[10].Split(':');
                longtextbox.Text = lon[1];
                string[] alt = sensorData[11].Split(':');
                alttextbox.Text = alt[1];
                string[] cog = sensorData[12].Split(':');
                COGtextbox.Text = cog[1];
                string[] sog = sensorData[13].Split(':');
                SOGtextbox.Text = sog[1];
                string[] fix = sensorData[14].Split(':');
                fixtextbox.Text = fix[1];
            }
    
            private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                RxString = serialPort1.ReadLine();
                sensorData = RxString.Split(',');
                this.Invoke(new EventHandler(DisplayText));
                
            }
    Basically I changed from SerialPort.ReadExisting(); to ReadLine();

    Unfortunantly it seems to throw an "IOException was Unhandled" randomly when "stopping" on the line:
    Code:
    RxString = serialPort1.ReadLine();
    More coming in a sec...

    Also seemingly randomly, on "starting" I am getting "IndexOutOfRangeException was unhandeled by user code/Index was outside the bounds of the array" on this line
    Code:
    an0textbox.Text = an0[1];
    It seems to occur on this one more often but it does occur on others as well it seems very random, I'm sure I just need some sort of event handler in here somehow or something along those lines but I'm pretty newb when it comes to programming, I have alot of unstructured experience nothing really formal and I'm scatterbrained to boot. Any help would greatly be appreciated.

    If Needed I can post the VC# Sln files
    Last edited by nagmier; 10-30-2009 at 01:14 PM.

  3. #3

    Re: I need help Parsing strings in C#

    It would be easier to debug if you included more code and the detailed error output.

    One thing that comes to mind is that perhaps you should perform some validation on RxString before parsing and displaying it. Maybe there is garbage/noise being read sometimes? At the least you should make sure it's not null or empty after trimming and looks like your data...

    Maybe something like this:

    Code:
     
    // Keep reading until we get good data.
    while(RxString == null || RxString.Trim() == "" || !RxString.Trim().StartsWith("!!!AN0") {
    RxString = serialPort1.ReadLine();
    }
    // We got what looks like good data; display it.
    sensorData = RxString.Split(',');
    this.Invoke(new EventHandler(DisplayText));

  4. #4
    Join Date
    Apr 2008
    Location
    Sacramento, CA, USA Area
    Posts
    5,341
    Rep Power
    181

    Re: I need help Parsing strings in C#

    Probably running right off the edge of the buffer, but I was never good at psychic-debugging...
    I Void Warranties´┐Ż

  5. #5

    Re: I need help Parsing strings in C#

    The DataReceived event is not deterministic.

    The Microsoft DataReceived Documentation states "Serial received events can be caused by any of the items in the SerialData enumeration. Because the operating system determines whether to raise this event or not, not all parity errors may be reported."


    Another way of saying this is... You, as a programmer, have no idea what or how much stuff is in the serial port buffer when the event fires.

    So your code...
    Code:
    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                RxString = serialPort1.ReadLine();
                sensorData = RxString.Split(',');
                this.Invoke(new EventHandler(DisplayText));
                
            }
    ...reads a line of code Microsoft style (0xA 0xD) from the serial port buffer. The reality is the buffer could contain the middle of a line or 2 and 1/3 lines. Your code needs to handle these possibilities in the "DisplayText" event handler.

    Happy Threading
    Last edited by MikeG; 10-30-2009 at 09:54 PM.

  6. #6

    Re: I need help Parsing strings in C#

    Ok, what MikeG said makes sense. Notice that the data starts with "!!!" and ends with "***". You can use those to piece together a complete string that is spread over multiple events.

  7. #7

    Re: I need help Parsing strings in C#

    You might consider using another read method like readexisting or reading a specific number of bytes. When you read the serial data check the position of "!!!" and "***". Now you can determine if you have the tail of one response, the head of another, or neither.

    At some point you'll get a start without and end. When that happens place the partial response in a temp object. On successive serial buffer reads determine what you have and build the temp object accordinly. Once the temp variable fits your response pattern... do whatever processing you need. Be careful of threading errors and receiving more than one response pattern in the serial buffer.

  8. #8

    Re: I need help Parsing strings in C#

    Also be careful with the ReadLine method. ReadLine waits until a newline is detected but you have no idea how many newlines are in the buffer. You can end up with left over response values. Then at some point during runtime you could get a buffer overrun.

  9. #9

    Re: I need help Parsing strings in C#

    Thanks guys that helps out alot.. I'll try a few different error checking routines and I'll try and piece something together...

    My main concern is bad coding I wanted to make sure I wasn't trying the impossible... Thanks again for the direction everyone!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C# Bioloid Motion Sequence Program
    By DresnerRobotics in forum Mech Warfare
    Replies: 4
    Last Post: 11-25-2009, 04:55 AM
  2. Question(s) Bioloid C programming: including libCM-5.a in makefile
    By tjo5010 in forum Software and Programming
    Replies: 3
    Last Post: 04-08-2009, 05:29 PM
  3. programming RoboBuilder in C
    By JoeStrout in forum Humanoids, Walkers & Crawlers
    Replies: 5
    Last Post: 10-30-2008, 09:23 AM
  4. Discussion C Programming Recommendations
    By Alex in forum Software and Programming
    Replies: 13
    Last Post: 07-27-2008, 01:11 AM
  5. C# Help
    By metaform3d in forum Software and Programming
    Replies: 11
    Last Post: 04-21-2008, 10:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •