无法在Windows 10物联网使用SerialDevice.ReadTimeout [英] Unable to use SerialDevice.ReadTimeout in Windows 10 IoT

查看:606
本文介绍了无法在Windows 10物联网使用SerialDevice.ReadTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施我使用一个外部的 USB到RS-232,因为内置串口适配器树莓派2
在Windows 10物联网Modbus主站被保留用于内核调试。

I'm trying to implement a ModBus master on Windows 10 IoT on a Raspberry Pi 2. I'm using an external USB to RS-232 adapter since the internal serial port is reserved for Kernel Debugging.

串口工作。我的问题主要是关于阅读时超时

Serial port is working. My question is mainly about timeout when reading.

下面是我的代码:

// Initialization
serialDevice.ReadTimeout = new TimeSpan(0, 0, 0, allowedTimeBetweenBytes);
serialDataReader.InputStreamOptions = InputStreamOptions.Partial;

// Reading
uint bytesRead = await serialDataReader.LoadAsync(MaxBufferSize); // 256
// Now use ReadBytes to get actual bytes



由于没有字节awailable在串口RX输入,我期待LoadAsync方法等待后返回0。不幸的是,它永远不会返回。 (好吧,它确实收到256字节后返回,但是这不是我想要的)

With no bytes awailable at the serial port RX input, I'm expecting the LoadAsync method to return 0 after wait. Unfortunately, it never returns. (Ok, it DOES return after 256 bytes are received, but that is not what I want)

由于的Mo​​dBus集中使用超时,我不知道如何实现它。我什至不知道我能做到这一点...

Since ModBus intensively uses timeouts, I am not sure how to implement it. I am not even sure I could do it...

是否在Windows 10物联网串口人已经使用超时?

Does anyone already used timeouts on Windows 10 IoT serial ports?

推荐答案

是啊,我不能得到这个工作要么。我不知道在哪里 ReadTimeout 是实际使用 SerialDevice 类内部。但我确实最终被超时复制到 CancellationTokenSource 得到的东西的工作。

Yeah, I couldn't get this working either. I'm not sure where ReadTimeout is actually used by the SerialDevice class internally. But I did end up getting something working by copying the timeout to a CancellationTokenSource.

您可以看到它在使用在下面的例子中,我写了一个旧的串行梅特勒 - 托利多PS 60航运规模,其中设备是的 SerialDevice 。好像在我的情况下工作,至少。

You can see it in use in the following example I wrote for an old serial Mettler Toledo PS 60 shipping scale, where device is an instance of SerialDevice. Seems to work in my case, at least.

using (var writer = new DataWriter(device.OutputStream))
{
    writer.WriteString("W\r\n");

    using (var cts = new CancellationTokenSource(device.WriteTimeout))
    {
        await writer.StoreAsync().AsTask(cts.Token);
    }

    writer.DetachStream();
}

using (var reader = new DataReader(device.InputStream))
{
    using (var cts = new CancellationTokenSource(device.ReadTimeout))
    {
        var read = await reader.LoadAsync(12).AsTask(cts.Token);

        if (read >= 12)
        {
            var data = reader.ReadString(12);
            reader.DetachStream();

            return ExtractWeightChangedEventArgs(data);
        }
    }
}

这篇关于无法在Windows 10物联网使用SerialDevice.ReadTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆