Windows UWP Windows.Devices.SerialCommunication.SerialDevice 不工作 [英] Windows UWP Windows.Devices.SerialCommunication.SerialDevice Not working
问题描述
是我一个人的问题,还是bug?
Is it just me, or is this a bug?
serialPort = await SerialDevice.FromIdAsync(Id);
serialPort
始终为 null,即使 Id
不是.
serialPort
is always null, even while Id
is not.
我需要让这个工作.现在我只是在编写非常快速而肮脏"的代码来测试来自 Windows 10 通用应用程序的串行通信.我在 x86 和 x64 中都调试了相同的结果.
I need to have this working. For now I am just writing very "quick and dirty" code to test serial communication from a Windows 10 Universal app. I debugged in both x86 and x64 with same result.
这是我目前所处的位置,但如果不创建 serialPort,我就无法走多远......
Here is where I am at for now, but I can't go very far without a serialPort being created....
public class SerialComm
{
private SerialDevice serialPort;
DataWriter dataWriteObject = null;
DataReader dataReaderObject = null;
public async void StartTest()
{
var deviceSelector = SerialDevice.GetDeviceSelector("COM3");
var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(deviceSelector);
var myCurrentDevice = myDevices[0];
string Id = myCurrentDevice.Id.ToString();
try
{
serialPort = await SerialDevice.FromIdAsync(Id);
}
catch (Exception)
{
throw;
}
StringBuilder commandBuilder = new StringBuilder();
while (true)
{
var rBuffer = (new byte[1]).AsBuffer();
await serialPort.InputStream.ReadAsync(rBuffer, 1, InputStreamOptions.Partial);
if ((char)rBuffer.ToArray()[0] != '\n')
{
commandBuilder.Append((char)rBuffer.ToArray()[0]);
}
else
{
string temp = "";
try
{
temp += rBuffer.ToString();
}
catch (Exception)
{
temp = "Error";
}
commandBuilder.Append(temp);
}
string stringToDisplay = commandBuilder.ToString();
}
感谢您的帮助和建议....
Thanks for your help and advices....
推荐答案
我在 Maxbotix 传感器上遇到了同样的问题,该传感器使用 FTDI 芯片进行 USB 到串行通信.我可以在终端程序中很好地连接到设备,我可以从真正的 .NET Framework 的 SerialPort
类中使用它,但在来自 GitHub 的 UWP SerialSample 和我的代码 SerialDevice 中都可以使用它.FromIdAsync()
返回 null
.
I had the same problem with a Maxbotix sensor that was using an FTDI chip for the USB-to-serial communication. I could connect to the device fine in a terminal program, and I could use it from the real .NET Framework's SerialPort
class, but in both the UWP SerialSample from GitHub and my code, SerialDevice.FromIdAsync()
returned null
.
对我来说,解决方案分为两部分.
For me, the solution was in two parts.
第一部分是在 Package.appxmanifest
文件中添加设备功能:
The first part was to add the device capability in the Package.appxmanifest
file:
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
第二部分是从 FTDI 网站.一旦我这样做了,它就开始工作了.
The second part was to download an updated driver (I used version 2.12.06) from the FTDI Web site. As soon as I did this, it started working.
完整示例如下:
var aqsFilter = SerialDevice.GetDeviceSelector("COM3");
var devices = await DeviceInformation.FindAllAsync(aqsFilter);
if (devices.Any())
{
var deviceId = devices.First().Id;
this.device = await SerialDevice.FromIdAsync(deviceId);
if (this.device != null)
{
this.device.BaudRate = 57600;
this.device.StopBits = SerialStopBitCount.One;
this.device.DataBits = 8;
this.device.Parity = SerialParity.None;
this.device.Handshake = SerialHandshake.None;
this.reader = new DataReader(this.device.InputStream);
}
}
这篇关于Windows UWP Windows.Devices.SerialCommunication.SerialDevice 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!