I have have a small problem and need your help please, I have an array and a textbox , the user will enter a decimal value of maximum 32 bits in the text box , this value needs to be added at the end of the array and sent via serial port as binary value, hence the **var **addressAndOpcode
I am struggling because the textbox input is string , how can I do this please ?thanks
private void btnSendData_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
byte[] payload= System.Text.Encoding.Unicode.GetBytes(tBoxDataOut.Text);
var addressAndOpcode = new byte[] { 0x0A, 0x08, 0x04, 0x01, 0x02, 0x03 };
addressAndOpcode[6]= payload; // this is wrong it should be size of 4 bytes after position 5 which has value 0x03
try
{
** serialPort1.Write(addressAndOpcode, 0, addressAndOpcode.length ??);**
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
I am expelcting the following if the user type 65535 (0xFFFF)
to be added at the end of the array before I can send it via the serial port
like this:
{ 0x0A, 0x08, 0x04, 0x01, 0x02, 0x03,0xff,0xff,0xff,0xff };
many thanks in advance.
Mustapha Alkhafaaf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.