我正在尝试使用UWP平台对设备执行请求响应模式。我可以使用此代码成功进行写操作,但是读操作始终返回0个字节。我使用交叉连接电缆来确认与另一个串行端口的写入。使用UWP示例,我可以返回和读取响应,但是下面的代码永远不会读取任何数据。
https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/CustomSerialDeviceAccess
下面是我尝试发送请求并接收响应的方法
async public void FindDevices()
{
string comDeviceSelector = SerialDevice.GetDeviceSelector("COM2");
var UsbDevices = await DeviceInformation.FindAllAsync(comDeviceSelector);
var device = await SerialDevice.FromIdAsync(UsbDevices.First().Id);
device.BaudRate = 115200;
device.DataBits = 8;
device.StopBits = SerialStopBitCount.One;
device.Parity = SerialParity.None;
device.Handshake = SerialHandshake.None;
device.BreakSignalState = false;
device.IsDataTerminalReadyEnabled = false;
device.IsRequestToSendEnabled = false;
device.WriteTimeout = TimeSpan.FromMilliseconds(50);
device.ReadTimeout = TimeSpan.FromSeconds(30);
var writer = new DataWriter(device.OutputStream);
var requestWriteAccess = new byte[] { 0xA5, 0x3F, 0x02, 0x07, 0x00, 0x01, 0xB3, 0xE7, 0x0F, 0x00, 0x10, 0x3E};
writer.WriteBytes(requestWriteAccess);
var bytesWritten = writer.StoreAsync().AsTask();
writer.DetachStream();
writer = null;
uint ReadBufferLength = 1024;
var reader = new DataReader(device.InputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;
var readTask = reader.LoadAsync(ReadBufferLength).AsTask();
var bytesRead = await readTask;
}
以下是我的appxmanifest中的内容:
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>