Select to view content in your preferred language

Help With FromSerialPort

386
2
07-25-2022 11:09 AM
pshearon
Emerging Contributor

.NET 6 Target 10.0.22000.0 or less  x64
Esri.ArcGISRuntime.WPF (100.14.1)
Esri.ArcGISRuntime.Toolkit (100.14.1)
System.IO.Ports (6.0.0)

Problem: No response after calling FromSerialPort with proper devices.

The blue-tooth devices connect properly, but I can't get any serial GPS to connect. I do not have this problem in .NET 4.8 using it's constructs (Port name).

Using this sample: https://github.com/Esri/arcgis-runtime-demos-dotnet/tree/main/src/ExternalNmeaGPS

Which also exhibits the same behavior. 

Regards,
Patrick Shearon

0 Kudos
2 Replies
dotMorten_esri
Esri Notable Contributor

Are you setting the correct baud rate for your device?

0 Kudos
pshearon
Emerging Contributor

Yes, 4800. 

Here is how I have it working but not using the FromSerialPort. I have to use FromStreamCreator to get it to work.

 

internal static async void Connect(Device device)
        {
            try
            {
                Disconnect();
                gpsFile = AppDomain.CurrentDomain.BaseDirectory + @"\Offline\" + Statics.App.User.Username + "_" + DateTime.Now.ToString("MM-dd-yyyy_HH-mm-ss") + ".gpsdata";
                //SERIAL PORTS
                if (device.IsSerialPort)
                {
                    string portName = "";
                    using (var ser = await Windows.Devices.SerialCommunication.SerialDevice.FromIdAsync(device.DeviceInfo!.Id))
                    {
                        portName = ser.PortName;
                    }
                    var port = new System.IO.Ports.SerialPort(portName, 4800);
                    currentGPS = NmeaLocationDataSource.FromStreamCreator(
                        onStart: (ds) =>
                        {
                            try
                            {
                                port.Open();
                                return Task.FromResult(port.BaseStream);
                            }
                            catch (Exception ex)
                            {
                                return null;
                            }
                        },
                        onStop: (ds) =>
                        {
                            try
                            {
                                port.Close();
                                return Task.CompletedTask;
                            }
                            catch (Exception ex)
                            {
                                return Task.CompletedTask;
                            }
                        }
                    );
                    pmc.MyMapView.LocationDisplay.DataSource = currentGPS;
                    pmc.MyMapView.LocationDisplay.IsEnabled = true;
                }
                else
                {
                    //BLUETOOTH
                    var btdevice = await Windows.Devices.Bluetooth.BluetoothDevice.FromIdAsync(device.DeviceInfo!.Id);
                    if (btdevice is not null)
                    {
                        currentGPS = NmeaLocationDataSource.FromBluetooth((await btdevice.GetRfcommServicesForIdAsync(RfcommServiceId.SerialPort)).Services.First());
                        pmc.MyMapView.LocationDisplay.DataSource = currentGPS;
                        pmc.MyMapView.LocationDisplay.IsEnabled = true;
                    }

                }
                currentGPS.LocationChanged += Gps_LocationChanged;
                SaveSettings(device);
                Statics.Alert("Connecting to: " + device.Name);
            }
            catch (Exception ex)
            {
                Statics.Alert(ex.Message);
            }
        }

 

 

0 Kudos