SerialDevice IsRequestToSendEnabled Raspberry Pi

SerialDevice IsRequestToSendEnabled Raspberry Pi

我正在使用 vb 进行编程,并且有一个使用 Raspberry Pi 的 UWP 项目。

当我使用 USB 到 RS485 转换器时,我可以使用 SerialDevice 的 IsRequestToSendEnabled 来同步 Raspberry 和 PLC 之间的通信,但是当我尝试使用 UART 和 TTL 到 RS485 转换器时,IsRequestToSendEnabled 不是可用。

出现以下错误:

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

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

串口不支持?我不是UART专家,那么我如何在Modbus中同步通信帧呢? RS485 的 TTL 是半双工的,我想我必须以某种方式同步通信。它确实适用于 USB 到 RS485 转换器,但我不想使用它。

这是一个代码示例:

    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

您收到的错误可能发生在各种用例中。但我猜设备功能没有设置为串行访问。

确保在package.appxmanifest中启用串行通信设备功能。

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

Raspberry Pi 上的 UWP 在使用

时不会生成正确的 SerialDevice.GetDeviceSelector
SerialDevice.GetDeviceSelector("COM0")

USB 串行设备在 Window IoT Core 上不像在 Windows 10 桌面上那样显示为 COM 端口。

此外,如果您尝试使用除板载 UART 以外的串行设备或 FTDI 串行设备,除非您在 Pi 上安装 Pi 的驱动程序,否则它将无法工作。这不太可能,因为我还没有找到除 FTDI 之外的制造商为 Windows IoT Core 提供驱动程序。 (ala CH340)

Windows.Devices.SerialCommunication 命名空间充其量是不稳定的。我愿意接受我的麻烦是我自己设计的,但由于缺乏支持,这就像拔牙以获得稳定的解决方案一样。增强的每一次迭代都会带来一系列困难。

这是我过去一周在 Raspberry Pi.

上关于串行通信主题的学习内容
  1. 您可以与之通信的串行设备必须是 FTDI 设备。
  2. 您需要解决 GetDeviceSelector 和 FindAllAsync 的不足。 (没有"COM1")select或者.
  3. DataReader.LoadAsync 将等待直到您提供的整个 bufferLength 被填满。
  4. 因此,处理长度为 1 的缓冲区提供了获取任意长度数据的最佳方式。
  5. DataReader.LoadAsync 将在您向串行设备写入内容后第一次永远等待。提供取消令牌并重试。 (注意这大概是我自己的理解shortcoming/lack)
  6. 完成写入设备后 - 在尝试读取之前分离写入器的流,反之亦然。
  7. 而且很大 - 您必须在需要时保持 serialDevice 打开。在程序关闭之前不要尝试处理 SerialDevice - 它会挂起。

以下是我如何初始化 FTDI USB 串行设备。

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

我所做的是检查所有设备,并选择一个独特的 属性 到 select 我的设备。据推测,使用 SerialDevice.GetDeviceSelectorFromUsbVidPid 应该可以,但对我来说不行。