SerialDevice IsRequestToSendEnabled Raspberry Pi

1

I am programming with vb and have a UWP project using a Raspberry Pi.

When I use a USB to RS485 converter I can use the IsRequestToSendEnabled from SerialDevice to sync communication between the Raspberry and the PLC, but when I try to use the UART, with a TTL to RS485 converter, the IsRequestToSendEnabled is not available.

The following error shows up:

The request is not supported. (Exception from HRESULT: 0x80070032)

And

System.AccessViolationException HResult=0x80004003 Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

The UART does not support it? I am no expert in UART, so how do I sync communication frames in Modbus then? The TTL to RS485 is Half-Duplex, I assume I have to sync communication somehow. It does work with a USB to RS485 converter, but I do not want to use it.

Here is a code sample:

    Private Async Function ListAvailablePorts() As Task(Of Boolean)
        Try
            Dim aqs As String = SerialDevice.GetDeviceSelector("UART0") '"UART0") '"COM1")
            Dim _portlist As DeviceInformationCollection = Await DeviceInformation.FindAllAsync(aqs, Nothing)

            client = Await SerialDevice.FromIdAsync(_portlist(0).Id)
            client.ReadTimeout = TimeSpan.FromMilliseconds(1000)
            client.WriteTimeout = TimeSpan.FromMilliseconds(1000)
            client.BaudRate = 9600
            client.Parity = SerialParity.None
            client.DataBits = 8
            client.StopBits = SerialStopBitCount.One
            client.Handshake = SerialHandshake.None ' SerialHandshake.None ' SerialHandshake.XOnXOff ' SerialHandshake.RequestToSend
            client.IsRequestToSendEnabled = False ' error here when using UART
            client.IsDataTerminalReadyEnabled = False

            Return True
        Catch ex As Exception
            Debug.WriteLine("Error listing ports: " & ex.Message)
            Return False
        End Try

    End Function
raspberry-pi2
uart
modbus
rs485
asked on Stack Overflow Jul 27, 2018 by Leo Magno

1 Answer

0

The error you are receiving can happen in various use cases. But I'm guessing the device capapability isn't set for serial access.

Make sure to enable the serial communication device capability in the package.appxmanifest.

   <Capabilities>
      <Capability Name="internetClient" />
      <DeviceCapability Name="serialcommunication">
         <Device Id="any">
            <Function Type="name:serialPort"/>
         </Device>
      </DeviceCapability>
   </Capabilities>

UWP on Raspberry Pi will not produce a proper SerialDevice.GetDeviceSelector when using

SerialDevice.GetDeviceSelector("COM0")

USB-Serial devices don't show as COM ports on Window IoT Core like they do on Windows 10 Desktop.

Also, if you are attempting to use a serial device OTHER than the onboard UART, or an FTDI serial device, it will not work unless you get drivers for the Pi installed on the Pi. Which is unlikely, as I have yet to find a manufacturer other than FTDI provide drivers for Windows IoT Core. (ala CH340)

The Windows.Devices.SerialCommunication namespace is flaky at best. I am willing to accept it that my troubles are of my own design but with the lack of support it's like pulling teeth to get a solution stable. Every iteration of enhancement provides their own set of difficulties.

Here's what I've learned over the past week on the subject of Serial Communication on the Raspberry Pi.

  1. The serial device that you can communicate with must be an FTDI device.
  2. You need to work around GetDeviceSelector and FindAllAsync short comings. (no "COM1") selector.
  3. DataReader.LoadAsync will await until the ENTIRE bufferLength you provide has been filled.
  4. Therefore, processing a buffer length of 1, provides the best way to get data of any length.
  5. DataReader.LoadAsync will await forever the first time after you write something to the serial device. Provide a cancellation token and try again. (Note this is probably my own shortcoming/lack of understanding)
  6. When you are done writing to the device - detach the writer's stream before attempting to read and vice versa.
  7. And a big one - you must keep the serialDevice open for as long as you need it. DO NOT attempt to Dispose of the SerialDevice until your program closes - it will hang.

Here's how I initialize an FTDI USB-Serial device.

var aqs = SerialDevice.GetDeviceSelector();
var devices = await DeviceInformation.FindAllAsync(aqs);
var deviceInfo = devices?.Where(i => i.Name == "FT232R USB UART").FirstOrDefault();

What I did was inspected ALL devices and chose a unique property to select my device. Supposedly, using SerialDevice.GetDeviceSelectorFromUsbVidPid should work, but it did not for me.

answered on Stack Overflow Oct 10, 2018 by rick

User contributions licensed under CC BY-SA 3.0