Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i need to connect via serial port to a micro controller. if i click on a button, it sends a packet of data to micro controller and if it receives this, sends back special data.
for ex., i send by press on button: "AA BB 80 01 FF 01 80"
that "AA BB" is start of frame, "01 80" CRC, and others are data
reply of this message from micro controller is: "BB AA 80 32 (some of data) (2 bytes CRC)"
that "BB AA" is start of frame, "2 bytes CRC" CRC, and others are data
all of these data are as hex code!

question1: when i press on button and send data to micro controller, and i wrote on button_click event
serialPort1.WriteLine("AA BB 01 FF 01 80");
. shall this data("AA BB 01 FF 01 80") change to hex? because micro controller just read hex!

question2: if this data received by micro controller and it replies data as "BB AA 80 32 (some of data) (2 bytes CRC)". but now, i need to use "some of data" that are in hex code, and display these on text box. what shall i do?
Posted

You probably should be using the SerialPort.Write[^] and SerialPort.Read[^] methods, to handle pure hex data. You can then convert the input bytes to string data to display in your text box.
 
Share this answer
 
Comments
CPallini 21-Oct-13 4:32am    
5.
mohsenvasefi 21-Oct-13 4:34am    
are you sure?
serialPort1.WriteLine("AABB8001FF0180");
serialPort1.ReadExisting();
string vtype = Convert.ToString(serialPort1.ReadLine());
is it true? can Convert.ToString convert hex to string fo display? and for display, shall i write on button_click event or text box change? which one?
Richard MacCutchan 21-Oct-13 4:56am    
Your WriteLine call is not sending what you think. The character string "AABB8001FF0180", is actually 00410041004200420038003000300031004600460030003100380030 in hex. You need to start with byte[] buffers for both input and output and convert to printable caharacters where necessary.
mohsenvasefi 21-Oct-13 7:51am    
he character string "AABB8001FF0180", is actually 00410041004200420038003000300031004600460030003100380030 in hex.

why did you write "0041" instead of "41" for "A"???
Richard MacCutchan 21-Oct-13 8:39am    
OK, my mistake, serialPort uses ASCII encoding be default, so the actual hex bytes will be as above but without all the 00 prefix bytes.
Quote:
shall this data("AA BB 01 FF 01 80") change to hex? because micro controller just read hex!


Probably (very probably, indeed) the microcontroller 'understands' binary data (hexadecimal is just a textual representation of numbers, that is, binary data).
As already suggested by Richard you have to write (and read) byte buffers, e.g.
C#
byte[] buf = { 0xAA, 0xBB, 0x80, 0x01, 0xFF, 0x01, 0x80 };
serialPort1.Write(buf, 0, buf.Length);
 
Share this answer
 
Comments
mohsenvasefi 21-Oct-13 8:01am    
did you mean write and readbyte function? by readbyte function, we can just read one byte synchronously, not more (for ex., 20bytes)
CPallini 21-Oct-13 14:14pm    
Nope, I meant read/write byte buffers:
http://msdn.microsoft.com/en-us/library/ms143549(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/ms143551(v=vs.110).aspx
mohsenvasefi 22-Oct-13 1:30am    
ok, thank you
but my project give back an error about using serialPort1.Read

byte[] buff={0};
serialPort1.Read(buff, 10, 10);

it's in "button_click" event, when i pressed on button, give back an error and display "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."
what does it mean?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900