如何为SerialDevice创建“ DataReceived”事件?

时间:2019-10-11 14:31:48

标签: c# events serial-port

我正在使用SerialDevice类在串行端口上使用RS-485。当前没有接收数据的事件。目前,我必须使用DataReader轮询侦听器,而该数据读取器并不总是捕获其缓冲区中的所有数据。

我正在寻找一种为SerialDevice类创建接收器事件的方法,这样我就不必进行轮询,因此可以在收到正确的数据时立即触发它。

我当前的方法:

async void Initialize()
{
        DeviceInformationCollection devices = await DeviceInformation.FindAllAsync();
        if (devices.Count > 0)
        {
            for (int i = 0; i < devices.Count; i++)
            {
                if (devices[i].Id.Contains("PNP0501#2"))
                {
                    GpioStatus = string.Format("Connecting... {0} found", devices[i].Id);
                    DeviceInformation deviceInfo = devices[i];
                    serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
                    serialDevice.BaudRate = 9600;
                    serialDevice.DataBits = 8;
                    serialDevice.StopBits = SerialStopBitCount.Two;
                    serialDevice.Parity = SerialParity.None;
                    serialDevice.Handshake = SerialHandshake.None;
                }
            }
        }
        Listen();
}


        Initialize();
        DataReader dataReader;
        SerialDevice serialDevice;
        CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        const uint ReadBufferLength = 1024;

        async void Listen()
        {
            try
            {
                dataReader = new DataReader(serialDevice.InputStream);
                dataReader.InputStreamOptions = InputStreamOptions.Partial;

                while (true)
                {
                    await ReadAsync(cancellationTokenSource.Token);
                }
            }
            catch
            {
                GpioStatus = "Failed to listen";
            }
        }

        private async Task ReadAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            Task<UInt32> loadAsyncTask = dataReader.LoadAsync(ReadBufferLength).AsTask(cancellationToken);

            UInt32 bytesRead = await loadAsyncTask;
            GpioStatus = dataReader.ReadString(bytesRead);
        }

任何想法将不胜感激!

1 个答案:

答案 0 :(得分:1)

由于它是串行协议,因此您需要在进入时进行解释。如果期望的第一件事是(例如)4字节长,然后是数据包的其余部分,则:

while (dataReader.UnconsumedBufferLength > 0)
{
  // Note that the call to readString requires a length of "code units" 
  // to read. This is the reason each string is preceded by its length 
  // when "on the wire".
  uint bytesToRead = dataReader.ReadUInt32();
  receivedStrings += dataReader.ReadString(bytesToRead) + "\n";
}

摘录自https://docs.microsoft.com/en-us/uwp/api/windows.storage.streams.datareader

的代码段