【问题标题】:Problem reading from Sensor Using I2C on Windows 10 IOT在 Windows 10 IOT 上使用 I2C 从传感器读取问题
【发布时间】:2019-06-26 12:35:09
【问题描述】:

我正在使用 Windows 10 IoT 17763.253 的最新公开版本,但我在读取 i2c Co2 传感器时遇到问题。

奇怪的是,对于其他传感器来说,这似乎不是问题。

它经常会破坏最后两个 utf8 字符,例如 1126 显示为 11\u0011/2,其中最后 1/2 是单个 UTF8 字符。很多时候,钻石问号替换字符也出现在那里。

关于如何解决它的任何想法?我正在使用最新版本的 vs2019、Raspberry Pi 3 和 Windows 17763.253

代码:

using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;  
using Windows.Devices.I2c;

public string GetReading()
    {
        try
        {
            byte[] i2CReadBuffer = new byte[20];
              _device.Read(i2CReadBuffer);
            Task.Delay(300).Wait(); //MXu
            string answer_string = "";
            bool got_error = false;

            int bufsize = i2CReadBuffer.Length;
            for(int i =0;i<bufsize;i++)
            {
                Debug.WriteLine(i2CReadBuffer[i].ToString("X"));

            }

            Debug.WriteLine("");
            switch (i2CReadBuffer[0]) //first character denotes I2C reception status
            {
                case 1:
                    i2CReadBuffer[0] = 0;
                    answer_string = Encoding.UTF8.GetString(i2CReadBuffer).Replace("\0", string.Empty);
                    // does it match ?L,1  .... if so , makegot_error to true, even though it isn't an error.
                    Regex regex = new Regex(@"\\?L,[0-9]*,?T?");
                    Match match = regex.Match(answer_string);
                    if (match.Success)
                    {
                        got_error = true;
                    }


                    break;

                case 2:
                case 254:
                case 255:
                default:
                    got_error = true;
                    break;
            }

我们的传感器: https://www.atlas-scientific.com/_files/_datasheets/_probe/EZO_CO2_Datasheet.pdf

【问题讨论】:

    标签: c# raspberry-pi3 i2c windows-10-iot-core


    【解决方案1】:

    从数据表中,编码是 ASCII,但不是您代码中使用的 UTF8。另外,你是不是在发送命令后延迟了300ms?您可以通过以十六进制打印所有响应数据来解决此问题。

    在“响应代码和处理延迟”页面中,示例显示了从设备请求数据的工作流程。请注意。

    如果没有处理延迟或者处理延迟太短, 响应代码将始终为 254。

    我认为您可以尝试在 Read 方法之前移动延迟。

    public string GetReading()
    {
        try
        {
            Task.Delay(300).Wait(); //MXu
    
            byte[] i2CReadBuffer = new byte[20];
            _device.Read(i2CReadBuffer);
    
            string answer_string = "";
            bool got_error = false;
    
            int bufsize = i2CReadBuffer.Length;
            for(int i =0;i<bufsize;i++)
            {
                Debug.WriteLine(i2CReadBuffer[i].ToString("X"));
    
            }
    
            Debug.WriteLine("");
            switch (i2CReadBuffer[0]) //first character denotes I2C reception status
            {
                case 1:
                    i2CReadBuffer[0] = 0;
                    answer_string = Encoding.UTF8.GetString(i2CReadBuffer).Replace("\0", string.Empty);
                    // does it match ?L,1  .... if so , makegot_error to true, even though it isn't an error.
                    Regex regex = new Regex(@"\\?L,[0-9]*,?T?");
                    Match match = regex.Match(answer_string);
                    if (match.Success)
                    {
                        got_error = true;
                    }
    
    
                    break;
    
                case 2:
                case 254:
                case 255:
                default:
                    got_error = true;
                    break;
            }
       }
       catch(Exception ex)
       {
            Debug.WriteLine(ex.Message);
       }
    }
    

    【讨论】:

    • 迈克尔,感谢您的回复。您能否更具体地说明我应该在 发送后等待 300 毫秒的哪个命令?是 _device.Read() 吗?谢谢。
    • 我重写了代码以仅使用 ASCII 并延迟。
       _device.Read(i2CReadBuffer); Task.Delay(300).Wait(); //MXu 字符串 answer_string = ""; bool got_error = false; int bufsize = i2CReadBuffer.Length; for(int i =0;i<bufsize debug.writeline></bufsize>
    • 迈克尔,我不明白你的建议(你在说什么)。
    • @bluerain,我已经更新了回复。当您打印所有接收到的数据时,数据是否正确?
    • Michael Xu,我打印收到的数据时,还是不正确。我开始认为这可能是二氧化碳传感器硬件的问题,并将使用 Arduino 对其进行测试。我还将使用逻辑分析器来验证没有逻辑不一致。非常非常感谢你。如何将您的答案标记为正确?您还有其他测试建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    相关资源
    最近更新 更多