1

I try to write some binary data to the Serial Interface with C#:

0x02 0x81 0xF4 ...

This is a command for a remote device and must be transferred exactly like this. And here starts the Problem. When I use

System.IO.Ports.SerialPort.Write(Byte[] data, int Offset, int Size)

the data gets encoded (to ASCII,UTF-8 or whatever).And that's exactly I don't can need,since my remote device doesn't understand any Encoding.

Is there a Workaround?

5
  • 4
    Are you sure it gets encoded? How can sending a raw byte array get encoded unless the other end is doing it? Commented Sep 30, 2014 at 8:24
  • 1
    What exactly do you expect on the line? A string as "0x02 0x81 0xF4" or the corresponding raw data? SerialPort.Write will certainly send raw data, unless you encode it before already. Beware that the raw-data is not printable. Commented Sep 30, 2014 at 8:27
  • 3
    Can you show the code where you are writing? I find what you describe very unlikely. Far more likely, IMO, is that the writing code is simply wrong... Commented Sep 30, 2014 at 8:35
  • 1
    How are you converting 0x02 0x81 0xF4 ... to byte array being transmitted.. Are you converting above string to bytes using Encoding.GetBytes? Commented Sep 30, 2014 at 9:12
  • Do you mean 0x02 0x81 0xF4 ... is a string?!? Or do you have the data in the form new byte[] { 0x02, 0x81, 0xF4, ... }? Commented Sep 30, 2014 at 11:29

2 Answers 2

1

you can try this :

using System.IO.Ports;

public void TestSerialPort()
{
SerialPort serialPort = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);
serialPort.Open();
byte[] data = new byte[] { 1, 2, 3, 4, 5 };
serialPort.Write(data, 0, data.Length);
serialPort.Close();
}
Sign up to request clarification or add additional context in comments.

1 Comment

As well the comments above have been write. Writing a byte array to the serial port doesn't encode the data. Sorry for missleading you here. It was my serial sniffer, which caused the encoding problem.
0

I recently solved that exact problem like this:

// Update Relay
serialPort2.Close();
serialPort2.PortName="COM3";
serialPort2.Encoding = System.Text.Encoding.GetEncoding("Windows-1252");
serialPort2.BaudRate=9600;
serialPort2.DataBits=8;
serialPort2.Parity=Parity.None;
serialPort2.StopBits= StopBits.One; 
try {serialPort2.Open();
    serialPort2.Write("\u00a0\u0001\u0001\u00a2");
} catch {
    // Open Serial Port Failed
    label1.Text=label1.Text+ " Fail";
}

Jeff

2 Comments

Hi Jeff, this "almost works". It doesn't work for the characters between 0x80 and 0x9F, since the Encoding table is not linear there. Thanks for your help anyway, Stefan
Maybe you can select a different encoding that will work. I don't understand why there isn't a simple "binary" encoding. I used to be able to send any byte I wanted directly for Xmodem, etc.

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.