Search code examples
.netuartwindows-iot-core-109-bit-serialmultidrop-bus

9bit serial with .NET


I need to build communication between Multidrop bus nodes and main process running on Windows IoT over Raspberry Pi 3.
I know how to exchange data with 8 bit byte. It example of working code:

_serial_port = await SerialDevice.FromIdAsync(di.Id);

if (_serial_port == null) return false;

_serial_port.WriteTimeout = TimeSpan.FromMilliseconds(1000);
_serial_port.ReadTimeout = TimeSpan.FromMilliseconds(1000);
_serial_port.BaudRate = 9600;
_serial_port.Parity = SerialParity.None;
_serial_port.StopBits = SerialStopBitCount.One;
_serial_port.DataBits = 8;

dataWriteObject = new DataWriter(_serial_port.OutputStream);
dataReaderObject = new DataReader(_serial_port.InputStream);

dataWriteObject.WriteBytes(0xAA);
await dataWriteObject.StoreAsync();

await dataReaderObject.LoadAsync(1);
byte resp = dataReaderObject.ReadByte();

Here I transmit 1010 1010 and receive xxxx xxxx from the remote node.

The question.

  1. Lets say, remote node sends me 1010 1010 1
  2. Lets say I need send 1010 1010 1

What the code need to looks like?

UPDATE

I think about workaround solutions:

  1. To use parity bit of the UART. But I actually don't understand, how.
  2. Use COM -> USB converter, but actually there can be same problems of 9th bit.
  3. Use Adruino in middle that will implement RxTx 9 bit over GPIO and output data to Raspberry in our inner format.

Solution

  • SerialDevice.Parity is used for error-checking instead of data transmitting. And application can't access this bit.

    Use two bytes for 9 bits transmitting.

    Send part:

    dataWriteObject.WriteBytes(new byte[] { 0b10101010, 0b10000000});
    

    Receive part:

                    byte[] data = new byte[2] { 0, 0 };
                    dataReaderObject.ReadBytes(data);
                    int data1 = data[0];
                    int data2 = data[1];
                    data1 = data1 << 1;
                    data2 = data2 >> 7;
                    int data3 = data1 | data2;