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