I am trying to get data from the serial port

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TomInMaryland
    New Member
    • Oct 2011
    • 9

    I am trying to get data from the serial port

    I am trying to get data from the serial port

    I have built a simple serial program that opens the port and displays to a text box. It is working in that it opens and closes the port alright.

    BUT the data some how needs to be converted. It is garbled the data is in 8bit bytes how do I get them to show in hexdecimal? This is what they should look like in hex values, 19 39 43 09 29 11 19 80 they represent the (hour, minutes, seconds, month, date, year, and temperature in deg C) that is being transmitted into the serial port. I have the right baud rate as can be seen in the video.

    Here is a public video http://dl.dropbox.com/u/23363133/C_Prog/SimpleSerialC%2 3prog1.AVI of the program in operation you can see the problem I am having, Any help would be appreciated.

    The simple program is below...
    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_Serial
    {
        public partial class Form1 : Form
        {
            // Add this variable
    
            string RxString;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void buttonStart_Click(object sender, EventArgs e)
            {
                serialPort1.PortName = "COM18";
                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();
    
            }
        }
    }
  • TomInMaryland
    New Member
    • Oct 2011
    • 9

    #2
    Here is a followup video that shows that the values are coming over the serial line as hex and showing in the C# GUI as ASCII how can I convert (correct this)

    Public Video Link is here... http://dl.dropbox.com/u/23363133/C_P...progVideo2.AVI

    Please help if you can,

    Thanks Tom

    Comment

    • alexis4
      New Member
      • Dec 2009
      • 113

      #3
      150 and 250Mb? I don't think that these videos will be downloaded from many people...

      Now if I understood correctly you need to convert ascii to hex right?
      If so, take a look at the following link, it explains how.



      But anyway, a simple google search for "C# ascii to hex" will give you plenty of results.

      If this is not what you need, post again and kindly explain what is it that you need.

      Regards,
      Alexandros

      Comment

      • TomInMaryland
        New Member
        • Oct 2011
        • 9

        #4
        Brute forced hex in ASCII for the C# GUI

        I was able to brute force change the hex into ASCII within the microcontroller with assembler like this...

        movff SECONDS,ALSECON DS
        movff SECONDS,AUSECON DS
        bcf ALSECONDS,4
        bcf ALSECONDS,5
        bcf ALSECONDS,6
        bcf ALSECONDS,7
        movlw 0x30
        addwf ALSECONDS,1
        swapf AUSECONDS,1
        bcf AUSECONDS,4
        bcf AUSECONDS,5
        bcf AUSECONDS,6
        bcf AUSECONDS,7
        movlw 0x30
        addwf AUSECONDS,1

        The results were positive now you can see in this video the GUI taking in the ascii data from the serial port... http://dl.dropbox.com/u/23363133/C_P...progVideo3.AVI

        Now the tricky one is the Temperature it is a 12 bit resolution sensor that transmits as two 8bit bytes A MSB and a LSB the MSB is 2^7,2^6,2^5,2^4 ,2^3,2^2,2^1,2^ 0 and LSB 2^-1,2^-2,2^-3,2^-4,0,0,0,0. Giving a 12 bit resolution for -55 to 125 degC accuracy with in 0.125 deg C The conversion for this is a little more complicated. But is looks like I will again try an brute force it into ASCII with assembler so the C# GUI can easily read it.

        Any suggestions welcome...

        Thanks, Tom

        Comment

        • TomInMaryland
          New Member
          • Oct 2011
          • 9

          #5
          The temperature sensor datasrtucture looks like this... http://dl.dropbox.com/u/23363133/C_P...tureSensor.PNG

          Comment

          • TomInMaryland
            New Member
            • Oct 2011
            • 9

            #6
            Examples of this microcontroller Real Time Clock Calemder and Temperature sensor can be found on my youtube channel "Hamradio20 08" http://youtu.be/D1ctheXbfh8

            Comment

            • alexis4
              New Member
              • Dec 2009
              • 113

              #7
              Post1:

              BUT the data some how needs to be converted. It is garbled the data is in 8bit bytes how do I get them to show in hexdecimal?
              Post4:
              But is looks like I will again try an brute force it into ASCII with assembler so the C# GUI can easily read it.

              I am really confused. I want to help you but I cannot understand your problem. Read your posts from the beggining and you will see it too.

              You send raw hex bytes from the MCU, these bytes are succesfully received by the GUI and your problem is that you need to convert these bytes to ASCII format so that they will be readable from humans? Am I right? Is this the scenario? Please clarify this first (the scenario) and then we can elaborate to the solution.

              So forget about clocks, sensors etc, and please confirm that this is the scenario. If not, please explain clearly what your problem is. Because at the beggining it seems you need to convert ASCII to hex and now it seems you need the other way around.

              Alexandros

              Comment

              • TomInMaryland
                New Member
                • Oct 2011
                • 9

                #8
                Sorry for the confusion, you are right. What I really need is for the C# program to accept the bytes coming in over the serial port (data in the Hex format) and the C# program convert them into ASCII to display in the text box. I got the GUI to work accepting bytes of ASCII data as seen in this youtube video on my Youtube channel... http://youtu.be/qSGybjs-F7g By brute force converting the hex into ASCII within the microcontroller using assembler (mostly this is done for numbers by adding 0x30).

                Back to the prefer'd operation is for the c# to accept databytes over the serial port and convert them int ASCII for display in the GUI. Microcontroller s mostly like to operate in hex and it seems the conversion and text strings display etc would be better suited to occur in the C# program.

                Thanks for your reply and please advise if you can.

                Tom

                Comment

                • alexis4
                  New Member
                  • Dec 2009
                  • 113

                  #9
                  I saw the youtube video, the application seems to work just fine. But I agree 100% that the conversion must be done by the GUI. So if you use on the GUI side the same method you used on the MCU side (that is adding 0x30 to the hex value), you will get the same desired result on the PC monitor.

                  So what you have is a string (let's name it str) from the RS232 port, that must be converted properly so that it can be displayed on the PC monitor. First of all save this string to a byte array, but through a char array:

                  Code:
                  char[] CharArray = str.ToCharArray();
                  byte[] ByteArray = new byte[CharArray.Length];

                  Then you can handle the sensor bytes separately. Convert them to the desired values. After you do that, implement a for loop like this:

                  Code:
                  for (int i = 0; i < CharArray.Length; i++)
                  {
                    ByteArray[i] = Convert.ToByte(CharArray[i]);
                    ByteArray[i] += 0x30;
                  }

                  The only thing left is to send these characters to the textbox.

                  Please note that I am an electronics engineer and not a software developer. Maybe a software developer could give a better solution, but I think that this one is also going to work just fine. If you have problems converting the sensor bytes to the desired form, post the datasheet to help you with the conversion algorithm.

                  Hope that answers your questions.

                  Comment

                  • TomInMaryland
                    New Member
                    • Oct 2011
                    • 9

                    #10
                    Good idea, I am just learning c# I have a few books here and will try out your suggestion.

                    Comment

                    • alexis4
                      New Member
                      • Dec 2009
                      • 113

                      #11
                      Well I learned C# for the same reason, just to display data sent by the MCU. Books are good to use, but to be honest I 've learned from googling.

                      If you are completely new and didn't spend a lot of time yet on C#, also take a look at Java, it is very close to C# and it's cross platform.

                      Cheers!

                      Comment

                      • TomInMaryland
                        New Member
                        • Oct 2011
                        • 9

                        #12
                        Originally posted by alexis4
                        Well I learned C# for the same reason, just to display data sent by the MCU. Books are good to use, but to be honest I 've learned from googling.

                        If you are completely new and didn't spend a lot of time yet on C#, also take a look at Java, it is very close to C# and it's cross platform.

                        Cheers!
                        Thanks for the hints, I'll do that, you might enjoy my youtube channel http://www.youtube.com/user/HamRadio2008 if you are interested in embedded microcontroller s and such. Tom

                        Comment

                        • TomInMaryland
                          New Member
                          • Oct 2011
                          • 9

                          #13
                          Heres the Youtube video I got the Temperature chip MCP9801 now showing in the GUI with the full resolution of 1/16th degree C

                          YouTube Link... http://youtu.be/wqDGz2HilIQ

                          Thanks All...

                          Tom

                          Comment

                          Working...