Uwp serial devices

大兔子大兔子 提交于 2019-12-11 07:31:08

问题


I'm creating a UWP APP to receive the data from waspmote board, I was used WindForm to develop it, it can work, but because UWP doesn't support using System.IO.Port so I use using Windows.Devices.SerialCommunication; I look through the papers, but I still have no idea how can I start, how can I convert the windform code to UWP code?

the windform code as below

 public Form1()
    {
        InitializeComponent();
        getAvailablePorts();
    }
    void getAvailablePorts()
    {
        // Get a list of existing ports
        String[] ports = SerialPort.GetPortNames();
        String[] baudrates = new String[] { "9600", "115200" };
        // Insert the lists into the combo boxes
        comboBox1.Items.AddRange(ports);
        comboBox2.Items.AddRange(baudrates);
        // Select the default setting if at least a com port is found.
        if (ports.Count() != 0)
        {
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 1;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (comboBox1.Text == "" || comboBox2.Text == "")
            {
                textBox1.Text = "Please select port settings";
            }
            else
            {
                serialPort1.PortName = comboBox1.Text;
                serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
                serialPort1.DataReceived += new
                SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.Open();
                progressBar1.Value = 100;
                button1.Enabled = false;
                button2.Enabled = true;
            }
        }
        catch (UnauthorizedAccessException)
        {
            textBox1.Text = "UnauthorizedAccess";
        }
    }

    private void serialPort1_DataReceived(object sender,
    SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string value = sp.ReadExisting();
        processValue(value);
    }
    //Delegate to update received data in the textbox
    private delegate void processValueDelegate(string value);
    private void processValue(string value)
    {
        if (this.InvokeRequired)
        {
            this.BeginInvoke(new processValueDelegate(processValue),
            new object[] { value });
        }
        else
        {
            // ... update GUI in here ...
            textBox1.AppendText(value);
            DataExtraction(value);
        }
    }
    string UnreadBuffer = "";
    private void DataExtraction(string RxString)
    {
        UnreadBuffer += RxString;
        // Search for Data Header "<=>"
        int start = UnreadBuffer.IndexOf("<=>");
        int delimiter = UnreadBuffer.IndexOf("#");
        if (start >= 0)
        {
            string row = UnreadBuffer.Substring(start);
            if (delimiter >= 0)
            {
                string[] ExtractedDataFields = row.Split('#');
                if (ExtractedDataFields.Length >= 7)
                {
                    textBox2.Text = string.Format("{0:HH:mm:ss tt}", DateTime.Now);
                    textBox3.Text = ExtractedDataFields[1];
                    textBox4.Text = ExtractedDataFields[2];
                    textBox5.Text = ExtractedDataFields[3];
                    textBox6.Text = ExtractedDataFields[5];
                    UnreadBuffer = "";
                }
            }
        }
    }

*And I have anohther question: Since UWP doesn't have serialport in Toolbox, what's the substitute in UWP?

来源:https://stackoverflow.com/questions/47798265/uwp-serial-devices

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!