question

Fieel avatar image
0 Votes"
Fieel asked ·

Reading a serial device from an UWP app.

Hello,

how to properly read from a serial device from an UWP app? I'm trying to read data from an industrial scale; have tried every single possible solution i found on google and still have no idea what i'm doing wrong.

Can someone point me to the right direction? How am i supposed to communicate with a serial device? I have to send commands and read the received reply.

I tried using

Windows.Devices.SerialCommunication: I can find select and config my device but i can't communicate with it as i would like to. I need to send specific string commands and listen for replies, the replies might be ACK/NACK strings or a formatted string with the data i need. I can only read and write using writers but it's not what i need! I just want to send a string and read the response, simple as that! My tentative:


 string selector = SerialDevice.GetDeviceSelector("COM2");
             DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
             if (devices.Any())
             {
                 // Setup
                 DeviceInformation deviceInfo = devices[0];
                 SerialDevice serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
                 serialDevice.BaudRate = 9600;
                 serialDevice.DataBits = 8;
                 serialDevice.StopBits = SerialStopBitCount.One;
                 serialDevice.Parity = SerialParity.None;
    
    
                 // Write with DataWrite
                 // Read with DataReader
             }


The flow i'm looking for is more of "i send you this and then read the answer"

thanks in advance

noob software dev https://filipemadureira.com






windows-uwp
· 4
10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, If you are confused about how UWP uses serial devices, you can refer to the example provided by Microsoft. It can guide you to use the serial device correctly.

0 Votes 0 ·
Fieel avatar image Fieel RichardZhang-MSFT ·

Yes, I studied all of the examples before posting this question (as you can see from my code), I'm asking a more specific question though.

In the examples, it's showcased how to randomly read and write and it doesn't follow any logical real implementation of a serial device unless you simply have to write/read and forget about it: thus my question.

0 Votes 0 ·

Hi,

DataWrite and DataReader are only used to write data and read data. If you want to save the read data, there are many ways, such as creating variables to save or saving to a local file. If you encounter problems reading and writing data, please share your code on how to read and write. In the code you shared, you omitted them.

0 Votes 0 ·

Hello, has your issue been resolved? If there are new questions, please feel free to ask questions.

0 Votes 0 ·

0 Answers