I have an integer array d:int[] d = new int[]{1,2,3,4}
I want to send this through serial port (System.IO.Ports.SerialPort
).
What I have written was
serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);//SerialPort.GetPortNames()[0].ToString(), 9600, Parity.None, 8, StopBits.One
serialPort.Handshake = Handshake.None;
serialPort.RtsEnable = true;
serialPort.DtrEnable = true;
if(serialPort.IsOpen == false)
serialPort.Open();
try
{
//serialPort.Write(buffer_2_send, 0, buffer_2_send.Length);
serialPort.Write(d, 0, d.Length);
serialPort.WriteLine("43665");
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
And I am receiving this array d
in another PC with Hercules RS232 software.
I am not seeing anything in Hercules screen for the line serialPort.Write(d, 0, d.Length);
.
What would be the problem. The line serialPort.WriteLine("43665");
is writing the string "43665" to the Hercules screen.
You should convert your integer array to byte array before you send(serialPort.Write
) it to device
int[] d = new int[] { 1, 2, 3, 4 };
byte[] buf = d.SelectMany(i => BitConverter.GetBytes(i)).ToArray();
But there are also other issues. What size does your device assume for int? 2,4,8 bytes? What is endianness, Little-endian or big endian etc. Maybe it is as simple as
byte[] buf = d.Select(i => (byte)i).ToArray();