Reputation: 193
I have an application which I want to communicate over a serial link to an embedded device. The communication protocol is binary - meaning that I have a structured message that I want to copy from memory into the serial byte stream unaltered (no encoding).
I am using Microsoft VS 2012 writing the application in C# with the .NET framework. I inherited a shell of an app which appears to be using the System.IO.Ports.SerialPort class to read/write to the com port.
Several coworkers have told me that I would have problems because modern communication libraries, or even the drivers for USB and serial, will assume you are communicating using some form of encoding like ASCII or Unicode. If anyone has an idea of how to start this please let me know. I appreciate any help.
Upvotes: 0
Views: 4943
Reputation: 3572
C# makes it pretty simple.
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();
}
And as mentioned, you can send any raw binary values you want, it doesn't need to be character data.
Upvotes: 1
Reputation: 27974
The starting point you are probably looking for is the Write(byte\[\], int, int)
method which allows you send an arbitrary chunk of binary data to the serial port.
And no, it's not true that serial (RS-232) communication requires character data. The physical communication like doesn't care of what data you send through it. Whether you can send 'raw binary' data or text commands (such as the so-called AT
commands used by modems) is a matter of the actual application protocol (if any) used on top of that communication link.
Upvotes: 1