Programmer's Corner Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programmer's Corner - Forums


How can I send Hex bytes through the serial port?

 
Post new topic   Reply to topic    Programmer's Corner Forum Index -> C#
Author Message
Nickdean1
Grasshopper


Joined: 09 Feb 2009
Posts: 1

PostPosted: Mon Feb 09, 2009 5:06 pm    Post subject: How can I send Hex bytes through the serial port? Reply with quote

My problem:

I need to send a Hex data string to a micro controller, the data string will be a variable number of bytes typed into a number of text boxes.

I thought I had this project all wrapped up as everything seemed to be working correctly until i connected it to the device it was designed to talk to. The device didnt respond because i am sending the data in the wrong format.

I am sending an ascii character string but i need to be sending Hex bytes, for example:

a user types into a number of textboxes the following data:
01 01 05 0a ff 09 ......et

i am transmitting: 3031 3031 3035 3061 6666 3039 ..... etc

i need to send 01 01 05 0a ff 09 .....

How I am currently transmitting the data:

string tx_data;
string address = textBox1.Text;
string type = textBox2.Text;
string parameter = textBox3.Text;
string data0 = textBox14.Text;
.
.
.
string checksum = textBox12.Text;

tx_data = address + type + parameter + data0 + checksum;
dataTx = tx_data;

serialPort1.WriteLine(dataTx);

What I have tried:

This has had me pulling my hair out all day, to do this in PIC C all i would need to do is use printf ("%x" , dataTx).
I have nearly give up trying with c# and tried to deal with it at the microcontroller end, but I am restricted in memory and dont like to just bodge things together.

I have been through many forums trying to figure out how to do this and the closest I have been is using,

serialPort1.WriteLine(dataTx.toString("x"));

this works when declaring a new variable as an int and testing the line above with a fixed value, but it does not like my variable dataTx,
cannot convert string to int,

I think my underlying problem is my understanding of the different data types and conversion from one to the other, but dont be harsh with me i am a relatively new programmer and this is my very first C# application

please help!
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Programmer's Corner Forum Index -> C# All times are GMT - 5 Hours
Page 1 of 1

 


Powered by phpBB © 2001, 2002 phpBB Group