如何从温度计读取数值?

问题描述

我在 USB 串行端口 (COM5) 中连接了温度计,我想从设备获取温度并将其显示在 winform 应用程序中。 我得到了一种像“??\u001a\u0001\u0001g\r”这样的值,我不知道如何将这个值转换为十进制值。

这是我正在使用的代码

    private void Form1_Load(object sender,EventArgs e)
    {
        // all of the options for a serial device
        // can be sent through the constructor of the SerialPort class
        // PortName = "COM1",Baud Rate = 19200,Parity = None,// Data Bits = 8,Stop Bits = One,Handshake = None
        _serialPort = new SerialPort("COM5",9600,Parity.None,8,StopBits.One);
        _serialPort.Handshake = Handshake.None;
        _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;
       // _serialPort.Encoding = Encoding.Unicode;
        _serialPort.open();
    }

    void sp_DataReceived(object sender,SerialDataReceivedEventArgs e)
    {
        Thread.Sleep(500);
        string data = _serialPort.ReadLine();
        string unescaped = Regex.Unescape(data);
        this.BeginInvoke(new SetTextDeleg(si_DataReceived),new object[] { data });
    }

    private void si_DataReceived(string data)
    {
        //textBox2.Text = ConvertUnicodetoAscii(data);
        //byte[] utf8Bytes = Encoding.UTF8.GetBytes(data);
        //textBox1.Text = new string(utf8Bytes.Select(b => (char)b).ToArray()); //Encoding.Unicode.GetString(utf8Bytes);
        textBox1.Text = data.Trim();
    }

你能给我一个关于我应该采取的方式的提示或建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)