locked
[UWP][C#]UWP Serial Communication NmeaParser RRS feed

  • Question

  • Hello,

    I am building Windows Universal Application with USB serial communication. For this reason I am trying to use NMEA Parser, which I installed by Nuget Manager from SharpGIS.NMEAParser.

    Here is my code:

    private async System.Threading.Tasks.Task PortSetup(string portName) { var selector = SerialDevice.GetDeviceSelector(portName); var devices = await DeviceInformation.FindAllAsync(selector); if (devices.Any()) { var deviceInfo = devices.First(); SerialDevice serialPort = await SerialDevice.FromIdAsync(deviceInfo.Id); serialPort.BaudRate = 9600; serialPort.DataBits = 8; serialPort.Parity = SerialParity.None; serialPort.StopBits = SerialStopBitCount.One; var device = new NmeaParser.SerialPortDevice(serialPort); await device.CloseAsync(); device.MessageReceived += Device_MessageReceived; await device.OpenAsync(); } }

     private void Device_MessageReceived(object sender, NmeaParser.NmeaMessageReceivedEventArgs e)
    {
       string message = e.Message.ToString();
     }

    And in Package.appxmanifest:

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

    Everything as in "https://github.com/dotMorten/NmeaParser/wiki/Using-in-a-Windows-Universal-App-(SerialPort)", just I added "await device.CloseAsync();" and await "device.OpenAsync();".

    The program is working good up to creating an event: "device.MessageReceived += Device_MessageReceived;" A problem is that the event never works. It does not get messages from serial port.

    Does anybody have any idea?




    • Edited by David_FF Monday, November 14, 2016 2:24 PM title tag
    Sunday, November 13, 2016 9:40 AM

Answers

  • You should probably not close the device before hooking up the event handler:

    var selector = SerialDevice.GetDeviceSelector(portName);
        var devices = await DeviceInformation.FindAllAsync(selector);
        if (devices.Any())
        {
            var deviceInfo = devices.First();
            SerialDevice serialPort = await SerialDevice.FromIdAsync(deviceInfo.Id);
            serialPort.BaudRate = 9600;
            serialPort.DataBits = 8;
            serialPort.Parity = SerialParity.None;
            serialPort.StopBits = SerialStopBitCount.One;
    
            var device = new NmeaParser.SerialPortDevice(serialPort);
            //DON'T DO THIS: await device.CloseAsync();
            device.MessageReceived += Device_MessageReceived;
            await device.OpenAsync();
        }
    

    There is a UWP sample app available here:  https://github.com/dotMorten/NmeaParser/blob/master/src/SampleApp.UWP/MainPage.xaml.cs

    If you have any issues with this third-party NuGet package I suggest that you report an issue on GitHub for better help: https://github.com/dotMorten/NmeaParser/issues. These forums are intended for Microsoft's own products and technologies only.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by leozel Thursday, November 17, 2016 1:08 PM
    Sunday, November 13, 2016 12:12 PM

All replies

  • You should probably not close the device before hooking up the event handler:

    var selector = SerialDevice.GetDeviceSelector(portName);
        var devices = await DeviceInformation.FindAllAsync(selector);
        if (devices.Any())
        {
            var deviceInfo = devices.First();
            SerialDevice serialPort = await SerialDevice.FromIdAsync(deviceInfo.Id);
            serialPort.BaudRate = 9600;
            serialPort.DataBits = 8;
            serialPort.Parity = SerialParity.None;
            serialPort.StopBits = SerialStopBitCount.One;
    
            var device = new NmeaParser.SerialPortDevice(serialPort);
            //DON'T DO THIS: await device.CloseAsync();
            device.MessageReceived += Device_MessageReceived;
            await device.OpenAsync();
        }
    

    There is a UWP sample app available here:  https://github.com/dotMorten/NmeaParser/blob/master/src/SampleApp.UWP/MainPage.xaml.cs

    If you have any issues with this third-party NuGet package I suggest that you report an issue on GitHub for better help: https://github.com/dotMorten/NmeaParser/issues. These forums are intended for Microsoft's own products and technologies only.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by leozel Thursday, November 17, 2016 1:08 PM
    Sunday, November 13, 2016 12:12 PM
  • Thanks for tour reply Magnus,

    Unfortunately it still is not working. I reported an issue on GitHub, as you suggested.

    Sunday, November 13, 2016 2:26 PM
  • Hello leozel,

    I checked and found that you have submitted your issue to here:

    https://github.com/dotMorten/NmeaParser/issues/25

    Please don't forget to follow up that case. Personally I also think that your problem is related to the parameter.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 16, 2016 8:57 AM
  • leozel,

    Obviously, you're not following the rules when using some third-party libraries.

    Per https://github.com/dotMorten/NmeaParser/blob/master/src/NmeaParser.Shared/NmeaDevice.cs line 131, the NmeaParser is always expecting an '\n' ended string, make sure you are doing that.

    And in the same file line 150, it's using NmeaParser.Nmea.NmeaMessage.Parse to parse the string, make sure you follow the data format of the parser, checked out in https://github.com/dotMorten/NmeaParser/blob/master/src/NmeaParser.Shared/Nmea/NmeaMessage.cs, which I believe define your string to start with '$', etc...


    • Edited by Jackie_ Thursday, November 17, 2016 10:15 AM
    Thursday, November 17, 2016 10:13 AM