Question

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.

Was it helpful?

Solution

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();

OTHER TIPS

In Hercules context menu (right click on data window) you need to set special char treatment to hex (away from text mode).

I'd suppose Hercules is filtering non text characters like 0x01, 0x02, 0x03, 0x04. You could test that, if you put 0x41, 0x42, 0x43, 0x44 (which is ABCD) in your array and see what happens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top