0

so I am trying to send some bytes with hex values to setup my microcontroller on the other side of Serial Port. The thing is I am not quite sure how to properly do it and in what format to send them. For example I need to send two bytes and their hex values are 57 and B0. When I try to send it as a char array and I read it back I always get the ASCII Hex values of those characters like 53 and then 55 for the value 57. So I wanted to format the hex value as a byte and send both of them at the same time from byte array but I am not getting anything when reading the response. After formatting it to byte, the MessageBox is showing it's decimal value and I don't know if it supposed to be like that. I am providing my code below.

Info_Byte_Dec += Protocol_Set + Protocol_Unit + Protocol_Write + Protocol_Settings; //stores decimal value
Data_Byte_Dec = Mode * Protocol_Mode_Offset + ODR * Protocol_ODR_Offset + Scale; //stores decimal value

Info_Byte_Hex = Info_Byte_Dec.ToString("X"); //convert to hex value
Data_Byte_Hex = Data_Byte_Dec.ToString("X"); //convert to hex value
string Merged = $"{Info_Byte_Hex} {Data_Byte_Hex}";
MessageBox.Show("Merged1: " + Merged);
byte[] Mergedbytes = Merged.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray();
MessageBox.Show("Merged2: " + Mergedbytes[0] + Mergedbytes[1]);

port.Write(Mergedbytes, 0, 2);

I am not sure whether I should just send the decimal value 87, or I should format it to 57 hex value, or even to 0x57.

3
  • I am not sure wheter I should just send... - maybe you could lookup the code in the micro controller to see what the program is expecting there? Commented Mar 26, 2019 at 20:09
  • @MarkusSafar I was trying to find it. But this is the first time I am working with any kind of microcontroller and I am still kinda lost in the datasheets. Maybe I was just looking for wrong thing. Gonna try again. Commented Mar 26, 2019 at 20:16
  • @MarkusSafar sorry, I didn't undrestand it correctly. In the microcontroller code I am using this format 0x57. But the problem is when I try to send it that way, I am not getting the right values and I don't know why. Commented Mar 26, 2019 at 20:23

1 Answer 1

1

usually in the microcontroller world when you use hex, you mean actual bytes, the hex is just a convenient notation to write binary byte values in, it usually never means send the ascii hex values. Having said that, there are no rules, and sometimes people do actual ascii hex.

In your case, if you are using 0x57 on the stm32, it likely means you are using a byte literal, and not a ascii representation. You would have to do extra work to turn a 0x57 into a string.

So that means in C# just send bytes, not the ascii hex like you are at the moment

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.