I would like to find out how to store & load serial deviceinformation.
I have referred to UWP Serial Sample and tested the serial to RS485.
I am using a FTDI USB to UART-RS485 converter on raspberry pi running Windows IoT Core. I do not intend to use the onboard UART.
So I am wondering if I am able to save the SerialPort.id to Windows.Storage.ApplicationDataContainer and load it when the application starts to ensure it connects to the correct SerialPort.
How do I get the
SerialPortreadable name likeUSB-RS485-Cableetc . becasue when i useSerialComsDisplay.Text = listOfDevices[SerialDeviceList.SelectedIndex].Id.ToString();it read theIDwhich are in code.It doesn't seems to load the correct
SerialPortafter i load it.
Please help. Thanks.
Saving the SerialPort to container
private void SaveSerialConfig_Click(object sender, RoutedEventArgs e)
{
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
localSettings.Values["SerialDevice"] = listOfDevices[SerialDeviceList.SelectedIndex].Name;
SerialComsDisplay.Text = listOfDevices[SerialDeviceList.SelectedIndex].Id.ToString();
}
Startup code to initialize the SerialPort when app starts
private async void ListAvailablePorts()
{
try
{
string aqs = SerialDevice.GetDeviceSelector();
var dis = await DeviceInformation.FindAllAsync(aqs);
SerialComsDisplay.Text = "Select a device and connect";
for (int i = 0; i < dis.Count; i++)
{
listOfDevices.Add(dis[i]);
}
DeviceListSource.Source = listOfDevices;
SerialDeviceList.SelectedIndex = 0;
}
catch (Exception ex)
{
SerialComsDisplay.Text = ex.Message;
}
connectCommPort();
}
private async void connectCommPort()
{
var selection = SerialDeviceList.SelectedItems;
DeviceInformation entry;
if (selection.Count <= 0)
{
SerialComsDisplay.Text = "\n Select a device and connect";
return;
}
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
if (localSettings.Values["SerialDevice"] != null)
{
var device = localSettings.Values["SerialDevice"];
SerialComsDisplay.Text = device.ToString();
entry = (DeviceInformation)device;
}
else
{
entry = (DeviceInformation)selection[0];
}
try
{
serialPort = await SerialDevice.FromIdAsync(entry.Id);
if (serialPort == null) return;
// Configure serial settings
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(0);//10 //1000
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(50);//100 //1000
//serialPort.BaudRate = 9600;
serialPort.BaudRate = 4800;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
// Display configured settings
SerialComsDisplay.Text = "Serial port configured successfully: ";
SerialComsDisplay.Text += serialPort.BaudRate + "-";
SerialComsDisplay.Text += serialPort.DataBits + "-";
SerialComsDisplay.Text += serialPort.Parity.ToString() + "-";
SerialComsDisplay.Text += serialPort.StopBits;
// Set the RcvdText field to invoke the TextChanged callback
// The callback launches an async Read task to wait for data
SerialComsDisplay.Text = "Waiting for data...";
// Create cancellation token object to close I/O operations when closing the device
ReadCancellationTokenSource = new CancellationTokenSource();
if (serialPort != null)
{
dataWriteObject = new DataWriter(serialPort.OutputStream);
dataReaderObject = new DataReader(serialPort.InputStream);
}
Listen();
startPollTimer();
}
catch (Exception ex)
{
SerialComsDisplay.Text = ex.Message;
}
}
The property DeviceInformation.Name shows the readable name of the device.Please note that this property should only be used for display purposes only and not for finding a device because the Name can change due to localization or a user assigning a name.
I reviewed your code, found out that you stored the name of DeviceInformation as SerialDevice but not a object of DeviceInformation,
however when you loaded the setting, you parse the string to object.
UPDATE: