locked
How to parse serial data in C#? RRS feed

  • Question

  • Hi,

    I am working on my senior project and need sever help! I have a GUI that reads serial port 4 in a text box. The serial data is coming from a HCS12 microcontroller. The data looks like this: UUUUU0$PATIENT001,A,3347.3790,N,11755.6760,W,98.5.

    I only need the data starting from the A and seperate it from the 98.5. This data is two seperate information. The first portion is GPS location and the last 3 digits is body temperature. Ideally I would like to have the GPS data parsed and linked to google maps and the body temperature displayed onto the GUI and stored into a MySQL database. I am using VS2010.

    Here is my code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace Simple_Serial1
    {
        public partial class Form1 : Form
        {
            string RxString;

            public Form1()
            {
                InitializeComponent();
            }

            private void buttonStart_Click(object sender, EventArgs e)
            {

                serialPort1.PortName = "COM4";
                serialPort1.BaudRate = 600;

                serialPort1.Open();
                if (serialPort1.IsOpen)
                {
                    buttonStart.Enabled = false;
                    buttonStop.Enabled = true;
                    textBox1.ReadOnly = false;
                }
            }

            private void buttonStop_Click(object sender, EventArgs e)
            {

                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                    buttonStart.Enabled = true;
                    buttonStop.Enabled = false;
                    textBox1.ReadOnly = true;
                }
            }

            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (!serialPort1.IsOpen) return;

                char[] buff = new char[1];


                buff[0] = e.KeyChar;

                serialPort1.Write(buff, 0, 1);

                e.Handled = true;

            }

            private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {

                RxString = serialPort1.ReadExisting();
                this.Invoke(new EventHandler(DisplayText));
            }

            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {

                if (serialPort1.IsOpen) serialPort1.Close();

            }

            private void DisplayText(object sender, EventArgs e)
            {
                textBox1.AppendText(RxString);
            }
        }
    }

    Highly appreciate the help!

     

     

     

     

     

     

     

           

    Wednesday, July 27, 2011 4:16 AM

Answers

  • You can use RegEx but for this application it might be easier to just use String.Split(',').http://msdn.microsoft.com/en-us/library/system.string.split.aspx

    " UUUUU0$PATIENT001,A,3347.3790,N,11755.6760,W,98.5".Split(','). Will return back an array of strings that looks something like below (each line is an element of the array):

     UUUUU0$PATIENT001

    A

    3347.3790

    N

    11755.6760

    W

    98.5



    • Marked as answer by Cookie Luo Thursday, August 4, 2011 5:41 AM
    Wednesday, July 27, 2011 4:53 AM
  • Hi

    Or

    You can also do 

     

    var first = data.Substring(data.IndexOf(',') + 1, 1);
    
    var last = data.Substring(data.LastIndexOf(',') + 1);
    


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Marked as answer by Cookie Luo Thursday, August 4, 2011 5:42 AM
    Wednesday, July 27, 2011 4:54 AM
  • Thanx a lot guys, I managed to get the result I wanted. My new dilema is to search for the "$" and then start displaying the data. I am working on an if else statement where it will search for the $ else continue looping untill it finds it. Any hints would be highly appreciate it. Working with IndexOF currently but any other ideas would be highly appreciated.
    Giovanni
    • Marked as answer by Cookie Luo Thursday, August 4, 2011 5:42 AM
    Wednesday, August 3, 2011 6:09 PM

All replies

  • You can use RegEx but for this application it might be easier to just use String.Split(',').http://msdn.microsoft.com/en-us/library/system.string.split.aspx

    " UUUUU0$PATIENT001,A,3347.3790,N,11755.6760,W,98.5".Split(','). Will return back an array of strings that looks something like below (each line is an element of the array):

     UUUUU0$PATIENT001

    A

    3347.3790

    N

    11755.6760

    W

    98.5



    • Marked as answer by Cookie Luo Thursday, August 4, 2011 5:41 AM
    Wednesday, July 27, 2011 4:53 AM
  • Hi

    Or

    You can also do 

     

    var first = data.Substring(data.IndexOf(',') + 1, 1);
    
    var last = data.Substring(data.LastIndexOf(',') + 1);
    


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Marked as answer by Cookie Luo Thursday, August 4, 2011 5:42 AM
    Wednesday, July 27, 2011 4:54 AM
  • Kris's recommendation can work as well. However, if you want to generate google maps you'll have to parse it out to coordinates anyway. I'm pretty sure that google requires a negative coordinate to indicate west rather then the actual 'W'.
    Wednesday, July 27, 2011 5:01 AM
  • Where would I insert this code in order for it to work?
    Giovanni
    Wednesday, July 27, 2011 6:15 AM
  • Thanks alot, I will try it and see where that takes me.
    Giovanni
    Wednesday, July 27, 2011 6:16 AM
  • I would think in  private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

    would be the right place to split it. But it really does depend on how you're using it. The only reason I didn't put DisplayText is simply that the method name didn't seem appropriate for parsing text.

    Wednesday, July 27, 2011 6:27 AM
  • Thanx a lot guys, I managed to get the result I wanted. My new dilema is to search for the "$" and then start displaying the data. I am working on an if else statement where it will search for the $ else continue looping untill it finds it. Any hints would be highly appreciate it. Working with IndexOF currently but any other ideas would be highly appreciated.
    Giovanni
    • Marked as answer by Cookie Luo Thursday, August 4, 2011 5:42 AM
    Wednesday, August 3, 2011 6:09 PM
  • gio can i get code from you? i really need help from you
    Tuesday, February 16, 2016 2:44 AM