在 Windows 10 IOT 上使用 I2C 从传感器读取问题

Problem reading from Sensor Using I2C on Windows 10 IOT

我使用的是 Windows 10 IoT 17763.253 的最新 public 版本,我在读取 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("[=11=]", 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

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

在页面 "Response codes & processing delay" 中,示例显示了从设备请求数据的工作流程。请注意。

If there is no processing delay or the processing delay is too short, the response code will always be 254.

我觉得你可以试试把 delay before 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("[=10=]", 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);
   }
}