[SOLVED] How to parse UART string in C#

Status
Not open for further replies.

north2012

Member level 3
Joined
Apr 1, 2013
Messages
63
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Visit site
Activity points
1,813
Hello,

I am new in C# and I am trying to develop C# application for my relative humidity and temperature monitoring with SHT-11.

I found tutorials on net I manage to made serial connection and to receive string. Now I would like to parse received string and to extract humidity and temperature values. I made following protocol
START byte ('*') + 5 bytes rel. hum + SEPARATOR (':') + 5 bytes temperature + STOP byte (';')

I know how to parse string in C for microcontrollers but I am not sure how to do that in C# (Microsoft Visual 2013).

Here is my code
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RH_and_Temperaure_Monitoring
{
    public partial class Form1 : Form
    {
        string RxString;
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM12";
            serialPort1.BaudRate = 9600;

            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 the port is closed, don't try to send a character.

              if(!serialPort1.IsOpen) return;
  
              // If the port is Open, declare a char[] array with one element.
              char[] buff = new char[1];
              
              // Load element 0 with the key character.

              buff[0] = e.KeyChar;
  
              // Send the one character buffer.
              serialPort1.Write(buff, 0, 1);
  
              // Set the KeyPress event as handled so the character won't
              // display locally. If you want it to display, omit the next line.
              e.Handled = true;
        }

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

        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();
        }
    }
}

And screenshot of Windows Form..
https://obrazki.elektroda.pl/6159159500_1406711404.jpg

Later I would like to add plots and presentation of measured values in separated text boxes for temperature and humidity.

Thanks.
 

UART is a stream serial communication interface, clock self-included.
check some exist code as the example.
 

sample C# code
Code:
            string data = "*01245:00678;";
            char[] delimiterChars = { '*', ':', ';' };
            System.Console.WriteLine("Original text: '{0}'", data);
            string[] words = data.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
            System.Console.WriteLine("{0} words in text:", words.Length);
            foreach (string s in words)
                {
                int number=0;
                if(Int32.TryParse(s, out number))
                    System.Console.WriteLine("string  {0} number {1}", s, number);
                }
you need the StringSplitOptions.RemoveEmptyEntries otherwise you get an empty string at the start and end of the split

a run gives
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…