0

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.

2
  • Is Hercules listening? Commented May 23, 2013 at 8:04
  • I wonder how this can compile. AFAIK, there's no overload of serialPort.Write that works with an int[]. Are you, by chance, running and old version? Commented May 23, 2013 at 8:10

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

@Vinod Not surprise. I mentioned the problems. Read your device manual to learn the format how it accepts integers.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.