11

Is it just me, or is this a bug?

serialPort = await SerialDevice.FromIdAsync(Id);

serialPort is always null, even while Id is not.

I need to have this working. For now I am just writing very "quick and dirty" code to test serial communication from a Windows 10 Universal app. I debugged in both x86 and x64 with same result.

Here is where I am at for now, but I can't go very far without a serialPort being created....

public class SerialComm
    {
        private SerialDevice serialPort;
        DataWriter dataWriteObject = null;
        DataReader dataReaderObject = null;

        public async void StartTest()
        {

            var deviceSelector = SerialDevice.GetDeviceSelector("COM3");
            var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(deviceSelector);
            var myCurrentDevice = myDevices[0];
            string Id = myCurrentDevice.Id.ToString();

            try
            {
                serialPort = await SerialDevice.FromIdAsync(Id);
            }
            catch (Exception)
            {

                throw;
            }

            StringBuilder commandBuilder = new StringBuilder();

            while (true)
            {
                var rBuffer = (new byte[1]).AsBuffer();
                await serialPort.InputStream.ReadAsync(rBuffer, 1, InputStreamOptions.Partial);

                if ((char)rBuffer.ToArray()[0] != '\n')
                {
                    commandBuilder.Append((char)rBuffer.ToArray()[0]);
                }
                else
                {
                    string temp = "";

                    try
                    {
                        temp += rBuffer.ToString();
                    }
                    catch (Exception)
                    {
                        temp = "Error";
                    }

                    commandBuilder.Append(temp);
                }

                string stringToDisplay = commandBuilder.ToString();
            }

Thanks for your help and advices....

5
  • Checking the "official" sample SerialSample found on GitHub, it does the same. This is with 10.0.10240.0 and Windows 10 version 10240
    – BernardG
    Commented Aug 15, 2015 at 11:06
  • 1
    The docs are entirely too shoddy to have an informed answer. I'll randomly guess that, since there is no explicit Open() method, you can't get one because the device is already in use. Or you forgot to ask for the "serialcommunication" capability in the manifest. Commented Aug 15, 2015 at 11:21
  • Thank you for your answer, Hans. I should I have said that I DID add the capability in the manifest. Also please note that the same function give also a null in the sample made and distributed my Microsoft, so it's not just my code. I would guess that, if the device was already in use, there would be an exception thrown, not a null returned... (just a guess..)
    – BernardG
    Commented Aug 15, 2015 at 17:36
  • I'm having the same problem with an FTDI device, both with my code and with the SerialSample on GitHub. Did you ever find a solution? Commented Sep 8, 2015 at 20:18
  • Great answer, but for the benefit of future googlers, if you don't Dispose of an instance of SerialDevice, it will remain unavailable until the GC runs again and will manifest very similar symptoms.
    – RubberDuck
    Commented Jun 24, 2016 at 15:03

2 Answers 2

8

I had the same problem with a Maxbotix sensor that was using an FTDI chip for the USB-to-serial communication. I could connect to the device fine in a terminal program, and I could use it from the real .NET Framework's SerialPort class, but in both the UWP SerialSample from GitHub and my code, SerialDevice.FromIdAsync() returned null.

For me, the solution was in two parts.

The first part was to add the device capability in the Package.appxmanifest file:

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

The second part was to download an updated driver (I used version 2.12.06) from the FTDI Web site. As soon as I did this, it started working.

Full sample below:

            var aqsFilter = SerialDevice.GetDeviceSelector("COM3");
            var devices = await DeviceInformation.FindAllAsync(aqsFilter);
            if (devices.Any())
            {
                var deviceId = devices.First().Id;
                this.device = await SerialDevice.FromIdAsync(deviceId);

                if (this.device != null)
                {
                    this.device.BaudRate = 57600;
                    this.device.StopBits = SerialStopBitCount.One;
                    this.device.DataBits = 8;
                    this.device.Parity = SerialParity.None;
                    this.device.Handshake = SerialHandshake.None;

                    this.reader = new DataReader(this.device.InputStream);
                }
            }
1
  • Do you have an idea why it never finishes FromIdAsync for me? I await there too, just like you do, but it just runs infinitely...
    – Exa
    Commented May 6, 2017 at 11:31
2

If the "COM3" Serial Port you are trying to open is an onboard Serial Port, then the current design of the Serial Communication class does not allow accessing on-board serial ports. The Serial Communication class only supports USB-To-Serial communication, not direct serial communication. In your code above, what is the value of myDevices[0].Port.Name?

4
  • Thanks fort your answer Prashant. It IS a USB to serial port (Arduino). In faat the problem was that the port was already busyn but also that I am not sure how to actually open it. the way serial communication is programmed on the arduino, it does send a welcome message as soon as the port is opened, but I am unable to get it.
    – BernardG
    Commented Aug 18, 2015 at 7:50
  • Only one process can have the port at a time, so if it is owned by some other process then your app won't be able to open it. This is by design... Commented Aug 18, 2015 at 20:29
  • Regarding the welcome string being read, can you tell me the following: 1. Which Arduino board are you using (UNO R3, Leonardo, etc)? 2. What program was the Arduino flashed with? i.e. Can you see the welcome screen with the standard “blink” sample? Or does it only occur with your app? 3. If it doesn’t occur with a standard sample, can you send a sample Arduino sketch which repros the issue? 4. What do you see when you run the “Terminal” inside the Arduino IDE when you reset the Arduino device? How does this compare to what you see in their program? Commented Aug 18, 2015 at 20:33
  • 1
    @PrashantHPhadke-MSFT Where did you see that statement "not allow on-board serial ports"? I got null port too, but I can't find that statement on Microsoft dev center. I'm using an IPC , so it is not an option to use USB-Serial Device. Any workaround to access on-board serial port?
    – joe
    Commented Jan 25, 2018 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.