与c#uwp的串行设备通信
我正在尝试用c#开发一个用于串行通信的简单uwp应用程序。我有一个stm32板能够与putty终端正确通信。Putty的串行设置为:9600波特、8位、1位停止、奇偶校验无、流量控制xon/xoff。当我写入“1”字符时,led开关打开,我看到的是响应“ledon”,而当我写入“0”字符时,led开关关闭,我看到的是“ledoff”消息。 现在我不能用uwp c#向电路板发送字节。我读了很多类似的问题,但没有发现任何实用性。 我的代码可以正确找到端口,我已经给出了串行功能。我还更新了ftdichip中的VCP驱动程序,正如我在其他问题中所读到的。 我以这种方式打开端口:与c#uwp的串行设备通信,c#,uwp,serial-port,cdc,C#,Uwp,Serial Port,Cdc,我正在尝试用c#开发一个用于串行通信的简单uwp应用程序。我有一个stm32板能够与putty终端正确通信。Putty的串行设置为:9600波特、8位、1位停止、奇偶校验无、流量控制xon/xoff。当我写入“1”字符时,led开关打开,我看到的是响应“ledon”,而当我写入“0”字符时,led开关关闭,我看到的是“ledoff”消息。 现在我不能用uwp c#向电路板发送字节。我读了很多类似的问题,但没有发现任何实用性。 我的代码可以正确找到端口,我已经给出了串行功能。我还更新了ftdich
private async void OpenDevice(){
try
{
usbSerialGateway = await SerialDevice.FromIdAsync(usbSerialDevice.Id);
usbSerialGateway.BaudRate = 9600;
usbSerialGateway.DataBits = 8;
usbSerialGateway.Parity = SerialParity.None;
usbSerialGateway.StopBits = SerialStopBitCount.One;
usbSerialGateway.Handshake = SerialHandshake.XOnXOff;
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message.ToString());
}
finally
{
Debug.WriteLine("Opened device for communication.");
}
}
public void SendByte(byte b){
try
{
if (usbSerialGateway != null)
{
// Writing a byte to the serial device.
DataWriter dw = new DataWriter(usbSerialGateway.OutputStream);
dw.WriteByte(b);
}
}
catch (Exception)
{
Debug.WriteLine("byte not sent");
}
}
通过断点,我看到usbSerialDevice.Id=“\\?\USB”VID#u 0483&PID#u 5740#6D873F814953#{86e0d1e0-8089-11d0-9ce4-08003e301f73}”和FromIdAsync没有得到任何异常
以这种方式发送字节后:
private async void OpenDevice(){
try
{
usbSerialGateway = await SerialDevice.FromIdAsync(usbSerialDevice.Id);
usbSerialGateway.BaudRate = 9600;
usbSerialGateway.DataBits = 8;
usbSerialGateway.Parity = SerialParity.None;
usbSerialGateway.StopBits = SerialStopBitCount.One;
usbSerialGateway.Handshake = SerialHandshake.XOnXOff;
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message.ToString());
}
finally
{
Debug.WriteLine("Opened device for communication.");
}
}
public void SendByte(byte b){
try
{
if (usbSerialGateway != null)
{
// Writing a byte to the serial device.
DataWriter dw = new DataWriter(usbSerialGateway.OutputStream);
dw.WriteByte(b);
}
}
catch (Exception)
{
Debug.WriteLine("byte not sent");
}
}
我使用带有按钮的SendByte,并将字节写入文本框,因此我使用byte.Parse(文本)转换来获取字节。我知道putty发送一个字符,所以我写49发送“1”
对于功能,我将其写入package.manifest:
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>
请帮帮我!我快疯了!谢谢我解决了这个问题:
我写
await dw.StoreAsync();
之后
现在它开始工作了。我必须添加异步修饰符