0

I am trying to do a request response pattern to a device using the UWP platform. I can successfully get a write with this code, but the read always returns 0 bytes. I used a cross connect cable to confirm the writes with another serial port. With the UWP sample I can return and read responses, but the code below never reads any data.

https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/CustomSerialDeviceAccess

Below is the method I am using to attempt to send a request and receive a response

async public void FindDevices()
        {
            string comDeviceSelector = SerialDevice.GetDeviceSelector("COM2");
            var UsbDevices = await DeviceInformation.FindAllAsync(comDeviceSelector);

            var device = await SerialDevice.FromIdAsync(UsbDevices.First().Id);
            device.BaudRate = 115200;
            device.DataBits = 8;
            device.StopBits = SerialStopBitCount.One;
            device.Parity = SerialParity.None;
            device.Handshake = SerialHandshake.None;
            device.BreakSignalState = false;
            device.IsDataTerminalReadyEnabled = false;
            device.IsRequestToSendEnabled = false;

            device.WriteTimeout = TimeSpan.FromMilliseconds(50);
            device.ReadTimeout = TimeSpan.FromSeconds(30);

            var writer = new DataWriter(device.OutputStream);

            var requestWriteAccess = new byte[] { 0xA5, 0x3F, 0x02, 0x07, 0x00, 0x01, 0xB3, 0xE7, 0x0F, 0x00, 0x10, 0x3E};
            writer.WriteBytes(requestWriteAccess);
            var bytesWritten = writer.StoreAsync().AsTask();
            writer.DetachStream();
            writer = null;

            uint ReadBufferLength = 1024;
            var reader = new DataReader(device.InputStream);
            reader.InputStreamOptions = InputStreamOptions.Partial;
            var readTask = reader.LoadAsync(ReadBufferLength).AsTask();
            var bytesRead = await readTask;
        }

The following is in my appxmanifest:

<DeviceCapability Name="serialcommunication">
      <Device Id="any">
        <Function Type="name:serialPort"/>
      </Device>
    </DeviceCapability>
Ken White
  • 123,280
  • 14
  • 225
  • 444
N Cheadle
  • 93
  • 1
  • 7
  • https://stackoverflow.com/a/8910167/17034 – Hans Passant Jan 24 '19 at 01:24
  • In general that answer appears to be for System.IO.Ports, and UWP uses Windows.Devices.SerialCommunication. In regards to the answer about DTR and RTS, I also used a loopback connector to attempt to just send the data back at the app assuming it would end up on the buffer and it would read what it just sent but that doesn't seem to work either. – N Cheadle Jan 24 '19 at 01:57
  • For such a high baud rate you should enable RTS/CTS + DTR+DSR flow control –  Jan 24 '19 at 04:40

0 Answers0