473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

I am trying to get data from the serial port

TomInMaryland
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%23prog1.AVI of the program in operation you can see the problem I am having, Any help would be appreciated.

The simple program is below...
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Simple_Serial
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         // Add this variable
  15.  
  16.         string RxString;
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void buttonStart_Click(object sender, EventArgs e)
  24.         {
  25.             serialPort1.PortName = "COM18";
  26.             serialPort1.BaudRate = 9600;
  27.  
  28.             serialPort1.Open();
  29.             if (serialPort1.IsOpen)
  30.             {
  31.                 buttonStart.Enabled = false;
  32.                 buttonStop.Enabled = true;
  33.                 textBox1.ReadOnly = false;
  34.             }
  35.  
  36.  
  37.         }
  38.  
  39.         private void buttonStop_Click(object sender, EventArgs e)
  40.         {
  41.             if (serialPort1.IsOpen)
  42.             {
  43.                 serialPort1.Close();
  44.                 buttonStart.Enabled = true;
  45.                 buttonStop.Enabled = false;
  46.                 textBox1.ReadOnly = true;
  47.             }
  48.  
  49.         }
  50.  
  51.  
  52.           private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  53.           {
  54.               // If the port is closed, don't try to send a character.
  55.  
  56.               if(!serialPort1.IsOpen) return;
  57.  
  58.               // If the port is Open, declare a char[] array with one element.
  59.               char[] buff = new char[1];
  60.  
  61.               // Load element 0 with the key character.
  62.  
  63.               buff[0] = e.KeyChar;
  64.  
  65.               // Send the one character buffer.
  66.               serialPort1.Write(buff, 0, 1);
  67.  
  68.               // Set the KeyPress event as handled so the character won't
  69.               // display locally. If you want it to display, omit the next line.
  70.               e.Handled = true;
  71.           }
  72.  
  73.           private void DisplayText(object sender, EventArgs e)
  74.           {
  75.               textBox1.AppendText(RxString);
  76.             }
  77.  
  78.         private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  79.         {
  80.             RxString = serialPort1.ReadExisting();
  81.             this.Invoke(new EventHandler(DisplayText));
  82.  
  83.         }
  84.  
  85.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  86.         {
  87.             if (serialPort1.IsOpen) serialPort1.Close();
  88.  
  89.         }
  90.     }
  91. }
  92.  
Oct 1 '11 #1
12 11163
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
Oct 1 '11 #2
alexis4
113 100+
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.

http://www.dotnetspider.com/resource...ing-c-dot.aspx

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
Oct 1 '11 #3
I was able to brute force change the hex into ASCII within the microcontroller with assembler like this...

movff SECONDS,ALSECONDS
movff SECONDS,AUSECONDS
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
Oct 1 '11 #4
The temperature sensor datasrtucture looks like this... http://dl.dropbox.com/u/23363133/C_P...tureSensor.PNG
Oct 1 '11 #5
Examples of this microcontroller Real Time Clock Calemder and Temperature sensor can be found on my youtube channel "Hamradio2008" http://youtu.be/D1ctheXbfh8
Oct 1 '11 #6
alexis4
113 100+
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
Oct 2 '11 #7
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. Microcontrollers 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
Oct 2 '11 #8
alexis4
113 100+
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:

Expand|Select|Wrap|Line Numbers
  1. char[] CharArray = str.ToCharArray();
  2. byte[] ByteArray = new byte[CharArray.Length];
  3.  

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

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < CharArray.Length; i++)
  2. {
  3.   ByteArray[i] = Convert.ToByte(CharArray[i]);
  4.   ByteArray[i] += 0x30;
  5. }

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.
Oct 2 '11 #9
Good idea, I am just learning c# I have a few books here and will try out your suggestion.
Oct 2 '11 #10
alexis4
113 100+
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!
Oct 2 '11 #11
@alexis4
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 microcontrollers and such. Tom
Oct 2 '11 #12
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
Oct 3 '11 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Blaine HIlton | last post by:
I'm trying to send data over the serial port using PHP and Serproxy. I downloaded Serproxy from http://www.lspace.nildram.co.uk/freeware.html. From the manual page at...
4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
4
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. ...
4
by: Frank | last post by:
Hello, how to get information about all serial ports in the PC? I use the following code, but i got only the data of the FIRST serial port. All other serial port information are not available...
7
by: davetelling | last post by:
I'm a newbie that is still struggling with OOP concepts & how to make things work they way I want. Using Visual C# Express, I have a form in which I added a user control to display a graph, based...
13
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I...
4
by: Petr Jakes | last post by:
I am trying to save data it is comming from the serial port continually for some period. (expect reading from serial port is 100% not a problem) Following is an example of the code I am trying to...
2
by: colin | last post by:
Hi, Im having a tiresome amount of trouble with using a bluetooth serial link. The receiving end is a bluetooth-rs232 module conected to my embeded system. The PC has a little usb bluetooth...
9
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying,...
6
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.