Serial Device communication with c# uwp

503 Views Asked by At

i'm trying to develop a simple uwp app in c# for serial communication. I have an stm32 board capable to communicate correctly with putty terminal. Putty's serial settings are: 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. now i can't send byte to board with 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. 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.

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. I know that putty send a character so i write 49 to send '1'.

for capabilities i write this into package.manifest:

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

please help me! i'm going crazy! thank you

1

There are 1 best solutions below

1
On

I SOLVED THE ISSUE:

i write

await dw.StoreAsync();

after

dw.WriteByte(b)

and now it work. I had to add async modifier