Asked by:
c# serial port data parsing

Question
-
Hi,
I'm a total newbie to VC#, but have managed to scrape a basic terminal program together which reads the data a microcontroller is spewing out (async) using example code and tutorials :P. I'm a little stuck now, as I need to parse the data I am recieving via the serial port.
Example of data I am recieving:
M01000000L<CR><LB>M00001100L<CR><LB>M00010000L<CR><LB>M01100000L<CR><LB>M00000001L<CR><LB>
Which is displayed in my terminal window as:
M01000000L
M00001100L
M00010000L
M01100000L
M00000001L
(fyi, the 0 and 1s represent the on/off state of switches connected to the microcontroller, the M and L just indicate the beginning and end of the data set (for ease of reading :P))
I would like to automatically update a string with the content of each complete line as it is recieved, so I can check and manipulate it, however I have no idea how to create a string such as this, or how to 'stop adding' characters when it reaches the end of each data segment...
Obviously the program just sees a string of junk and displays it, and I don't know how to put into something more coherent xD
Here's the code of my program.using System; using System.Diagnostics; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.IO.Ports; namespace PSComm { public partial class Form1 : Form { string RxString; public Form1() { InitializeComponent(); string[] ports = SerialPort.GetPortNames();//gets the names of COM ports installed in the system and adds them to the port selection list foreach (string port in ports) { portBox.Items.Add(port); } portBox.DropDownStyle = ComboBoxStyle.DropDownList;//ensures user can't enter their own value portBox.SelectedIndex = 0;//selects the first port in the port selection list baudrateBox.Items.AddRange(new string[] { "2400", "9600", "19200", "38400" });//adds a list of baud rates to the baud rate selection list baudrateBox.DropDownStyle = ComboBoxStyle.DropDownList;//ensures user can't enter their own value baudrateBox.SelectedIndex = 1;//selects 9600bps baud rate } private void buttonStart_Click(object sender, EventArgs e) { try//attempts to set up and open the selected port { serialPort1.PortName = Convert.ToString(portBox.SelectedItem); serialPort1.BaudRate = Convert.ToInt32(baudrateBox.SelectedItem); serialPort1.Handshake = Handshake.None; serialPort1.Parity = Parity.None; serialPort1.DataBits = 8; serialPort1.StopBits = StopBits.One; serialPort1.Open(); if (serialPort1.IsOpen)//temporarily disables the options if the port is successfully opened { buttonStart.Enabled = false; buttonStop.Enabled = true; terminalBox.ReadOnly = false; portBox.Enabled = false; baudrateBox.Enabled = false; } } catch (Exception ex)//any errors during this operation are captured and displayed in a message box { MessageBox.Show(Convert.ToString(ex)); } } private void buttonStop_Click(object sender, EventArgs e) { if (serialPort1.IsOpen)//closes the serial port if it is open { serialPort1.Close(); } buttonStart.Enabled = true;//re-enables the options buttonStop.Enabled = false; terminalBox.ReadOnly = true; portBox.Enabled = true; baudrateBox.Enabled = true; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen)//closes the serial port if application is exited while it is open { serialPort1.Close(); } } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!serialPort1.IsOpen) return;//if the port is closed, don't try to send a character char[] buff = new char[1];//if the port is Open, declare a char[] array with one element buff[0] = e.KeyChar;//load element 0 with the key character serialPort1.Write(buff, 0, 1);//send the one character buffer e.Handled = true;//set the KeyPress event as handled so the character won't display locally } private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { RxString = serialPort1.ReadExisting();//reads the data in the serial port buffer and stores it in RxString this.Invoke(new EventHandler(DisplayText)); } private void DisplayText(object sender, EventArgs e) { terminalBox.AppendText(RxString);//updates the terminal box with the value of RxString } } }
Any advice is appreciated!
- Moved by jack 321 Tuesday, September 2, 2008 8:11 AM off topic (Moved from Visual C# General to Off-Topic Posts (Do Not Post Here))
Thursday, August 28, 2008 6:39 PM
All replies
-
Hi,
Thank you for your post! I would suggest posting your question in one of the MS Forums,
MSDN > Forums Home > .NET Development > .NET Remoting and Runtime Serialization
located here: http://social.msdn.microsoft.com/Forums/en-US/netfxremoting/threads/.Have a great day!
- Proposed as answer by Shrikant Maske Tuesday, November 4, 2008 9:26 AM
Tuesday, November 4, 2008 9:26 AM