简体   繁体   English

串口设备与c# uwp通信

[英]Serial Device communication with c# uwp

i'm trying to develop a simple uwp app in c# for serial communication.我正在尝试用 C# 开发一个简单的 uwp 应用程序以进行串行通信。 I have an stm32 board capable to communicate correctly with putty terminal.我有一个能够与腻子终端正确通信的 stm32 板。 Putty's serial settings are: 9600baud, 8bit, 1bit stop, parity none, flowcontrol xon/xoff. Putty 的串口设置为:9600baud、8bit、1bit stop、parity none、flowcontrol xon/xoff。 When i write '1' character a led switch on and i see the response "ledon" instead when i write '0' character the led switch off and i see "ledoff" message.当我写 '1' 字符时,LED 开关打开,我看到响应“ledon”,而当我写 '0' 字符时,LED 开关关闭,我看到“ledoff”消息。 now i can't send byte to board with uwp c#.现在我无法使用 uwp c# 将字节发送到板。 I readed many similar question but i not found any utility.我阅读了许多类似的问题,但没有找到任何实用程序。 My code can find correctly the port and i already given the serial capabilities.我的代码可以正确找到端口,并且我已经提供了串行功能。 i also updated the VCP driver from ftdichip as I read in other question.当我在其他问题中阅读时,我还从 ftdichip 更新了 VCP 驱动程序。 i open port in this manner:我以这种方式打开端口:

        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.");
        }

    }

with breakpoint i see usbSerialDevice.Id = "\\\\?\\USB#VID_0483&PID_5740#6D873F814953#{86e0d1e0-8089-11d0-9ce4-08003e301f73}" and FromIdAsync don't get any exception.使用断点我看到 usbSerialDevice.Id = "\\\\?\\USB#VID_0483&PID_5740#6D873F814953#{86e0d1e0-8089-11d0-9ce4-08003e301f73}" 并且 FromIdAsync 没有得到任何异常。

after i send a byte in this manner:在我以这种方式发送一个字节之后:

        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");
        }
    }

I use SendByte with a button and i write the byte in a textbox so i use byte.Parse( text ) conversion to get the byte.我使用带有按钮的 SendByte 并将字节写入文本框中,因此我使用 byte.Parse( text ) 转换来获取字节。 I know that putty send a character so i write 49 to send '1'.我知道腻子会发送一个字符,所以我写了 49 来发送“1”。

for capabilities i write this into package.manifest:对于功能,我将其写入 package.manifest:

    <DeviceCapability Name="serialcommunication">
       <Device Id="any">
        <Function Type="name:serialPort"/>
       </Device>
    </DeviceCapability>

please help me!请帮我! i'm going crazy!我要疯了! thank you谢谢你

I SOLVED THE ISSUE:我解决了这个问题:

i write我写的

await dw.StoreAsync();

after

dw.WriteByte(b)

and now it work.现在它起作用了。 I had to add async modifier我不得不添加异步修饰符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM