UWP C# 存储和加载串行设备信息

UWP C# Store and Load Serial Device Information

我想了解如何存储和加载 serial deviceinformation。 我已经提到 UWP Serial Sample 并测试了串口到 RS485。

我在 raspberry pi 运行 Windows IoT Core 上使用 FTDI USB 到 UART-RS485 转换器。我不打算使用板载 UART。 所以我想知道我是否能够将 SerialPort.id 保存到 Windows.Storage.ApplicationDataContainer 并在应用程序启动时加载它以确保它连接到正确的 SerialPort.

  1. 如何获得 SerialPort 可读名称,如 USB-RS485-Cable 等。因为当我使用 SerialComsDisplay.Text = listOfDevices[SerialDeviceList.SelectedIndex].Id.ToString(); 它 阅读代码中的 ID

  2. 我加载后似乎没有加载正确的SerialPort

请帮忙。 谢谢。

SerialPort 保存到 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();
    }

应用程序启动时初始化 SerialPort 的启动代码

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;
        }
    }

1.How do I get the SerialPort readable name like USB-RS485-Cable etc . becasue when i use SerialComsDisplay.Text = listOfDevices[SerialDeviceList.SelectedIndex].Id.ToString(); it read the ID which are in code.

属性 DeviceInformation.Name 显示 device.Please 的可读名称 请注意,此 属性 应仅用于显示目的而不是用于查找设备,因为由于本地化或用户分配名称,名称可能会更改。

2.It doesn't seems to load the correct SerialPort after i load it.

我查看了您的代码,发现您将 DeviceInformation 的名称存储为 SerialDevice 而不是 DeviceInformation 的对象,

localSettings.Values["SerialDevice"] = listOfDevices[SerialDeviceList.SelectedIndex].Name;

但是当您加载设置时,您将字符串解析为对象。

var device = localSettings.Values["SerialDevice"];
SerialComsDisplay.Text = device.ToString();
entry = (DeviceInformation)device;

更新:

private async void connectCommPort()
{
    var selection = SerialDeviceList.SelectedItems;
    string deviceId;

    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 deviceId = localSettings.Values["SerialDevice"];
        SerialComsDisplay.Text = deviceId;
    }
    else
    {
        deviceId = ((DeviceInformation)selection[0]).Id;
    }

    try
    {
        serialPort = await SerialDevice.FromIdAsync(deviceId );
        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;
    }
}