To write a 4-byte integer with a value of 1
, you first need to convert it to a byte array.
I am doing this using BitConverter
. You can also do this with Convert.ToByte
, as shown in the @sll figure.
Please note that it is very important to specify how many bytes you want to send to the serial port.
4 byte int? 2 bytes? one byte?
It seems you did not indicate this in your question.
int MyInt = 1; byte[] b = BitConverter.GetBytes(MyInt); serialPort1.Write(b,0,4);
source share