Click here to Skip to main content
15,846,091 members

Comments by S Houghtelin (Top 122 by date)

S Houghtelin 9-Jun-17 12:20pm View    
Then convert it to a string.
textBox2.Text = ToString(strHEX);

Not been using C# for too long? It does get easier the more you use it.

A useful resource I use for C# questions is https://www.dotnetperls.com/ (Code Project is a great resource as well).
S Houghtelin 9-Jun-17 11:52am View    
Here you go.

byte[] bytesToSend = new byte[14] { 0x68,0x99,0x99,0x99,0x99,0x99,0x99,0x68,0x23,0x02,0x61,0x00,0xEC,0x16 };


serialport1.Write(bytesToSend, 0, 14);

System.Threading.Thread.Sleep(200); // wait for .5 sec


byte[] buffer = new byte[serial.BytesToRead];
serialport1.Read(buffer, 0, buffer.Length);

StringBuilder strHEX = new StringBuilder();

for (int i = 0; i < buffer.Length; i++)
strHEX.AppendFormat("{0:X2} ", buffer[i]);

textBox2.Text = strHEX;

If I do more, I'll have to start billing. ;)
S Houghtelin 9-Jun-17 11:21am View    
String strHEX;
byte byteVal = 0x0F;
// To read back as a hex format
strHEX = string.Format("{0:X2}", byteVal);

Be aware that this is a string, it will look like the hex value but you will not be able to perform numerical calculations. To do that use the byte value as you would an integer value.
S Houghtelin 9-Jun-17 11:05am View    
In C# there is the "byte" variable that you can use to parse filestreams. IF you want to write in hex format use the 0x00 format, for instance:
byte byteVar = 0x0F; // Init with value of 15
S Houghtelin 30-Jan-17 8:06am View    
Yes, you are correct, the problem is in your code. I will ask you a question; If you close the serial port before setting up the receive event, how is the serial port going to receive new data if it is closed? Go back to the sample you linked to and carefully note the order as ppolymorphe so clearly laid out for you.

Look at it this way, if someone knocks on your door and you say "come in", and then you close the door in their face, how are they to enter?