title: Serial port : converting data
body: <p>i have only recently started to code in c#. I took on the challenge to develop a GPS program at work.
   We have the data that is supplied by a serial port. The representation on screen is allready coded and ready to use.
   I just need to get the data from the serial port and put it in a database.
   The information i have are the settings for the port (Baudrate 9600, 8, parity.odd, 1).
   They also gave me the structure of the syntax (  ), its transmitted as a stream of bytes.
   The final thing i have is a screenshot of how the data looks like when its correctly received.
   <a href="http://i61.tinypic.com/2ajcpww.png" rel="nofollow">http://i61.tinypic.com/2ajcpww.png</a></p>
  
  <p>As far as the code goes, the database part is not a problem at all. Once i have the correct data from the serial port i will be able to load it in de database and then its available for the presentation software.</p>
  
  <p>Would anyone with experience be able to guide me on my way. Is there any more information i need to find out from the company who suplied the data system.</p>
  
  <p>The code i have to read from the serial port is this:</p>
  
  <pre><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;
  using System.IO;
  using System.IO.Ports;
  using System.Threading;
  using System.Diagnostics;
  
  namespace ClarityListener
  {
      public partial class Form1 : Form
      {
          private SerialPort comPort = new SerialPort();
          string RxString;
          string port = "COM1";
  
          public Form1()
          {
              InitializeComponent();
  
              comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
  
          }
  
          private void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
          {
              int count = comPort.BytesToRead;
              byte[] data = new byte[count];
              comPort.Read(data, 0, data.Length);
              RxString = (BitConverter.ToString(data));
              this.Invoke(new EventHandler(DisplayText));
          }
  
  
          private void DisplayText(object sender, EventArgs e)
          {
              richTextBox1.AppendText(RxString);
          }
  
  
          private void button2_Click(object sender, EventArgs e)
          {
              if (comPort.IsOpen)
              {
                  comPort.Close();
              }
              else if (!comPort.IsOpen)
              {
  
                  try
                  {
                      comPort.PortName = port;
                      comPort.BaudRate = 9600;
                      comPort.Parity = Parity.Odd;
                      comPort.DataBits = 8;
                      comPort.StopBits = StopBits.One;
                      comPort.Handshake = Handshake.None;
                      comPort.Open();
                  }
                  catch (Exception er)
                  {
                      MessageBox.Show(er.Message);
                  }
              }
              if (comPort.IsOpen)
              {
                  richTextBox1.Clear();
  
              }
          }
  
          private void Form1_FormClosing(object sender, FormClosedEventArgs e)
          {
              if (comPort.IsOpen)
              {
                  comPort.Close();
              }
              MessageBox.Show("Poort is gesloten");
  
              this.Dispose();
          }
  
          private void button3_Click(object sender, EventArgs e)
          {
              if (comPort.IsOpen)
              {
                  comPort.Close();
              }
              MessageBox.Show("Poort is gesloten");
  
              this.Close();
              this.Dispose();
          }
  
  
      }
  }
  </code></pre>
  
  <p>The final outcome should be readable by humans (database).
  The data that is transmitted is like 6005 1 500 1058 1609 0116 51°08'924 02°43'252.</p>
  
  <p>This i what my program atm reads from the stream:</p>
  
  <pre><code>02-71-00-00-0C-84-07-1317-11-05-23-00-76-26-8522-8C-00-00-27-FB-00-01A3-BF-0302-72-00-00-0C-85-    07-0C17-12-05-24-00-70-25-E021-3C-00-00-23-E9-00-019D-AF-0302-73-00-00-0C-8C-07-0E17-12-05-19-00-    76-25-541F-D8-00-00-17-E1-00-018F-BC-0302-74-00-00-0C-8F-07-0D17-12-05-2D-00-76-26-A823-70-00-00-    33-D0-00-01B0-AE-0302-75-00-00-0E-B4-04-C517-51-04-B6-00-70-26-0121-7B-00-00-06-7D-00-0180-5F-    0302-76-00-00-0E-B8-00-0000-00-00-00-00-00-00-0000-00-00-00-00-00-00-0111-2F-0302-77-00-00-0F-8C-    09-1013-94-05-1B-00-7D-25-3C35-0E-00-00-30-14-00-02BE-9F-0302-78-00
  </code></pre>