C# 在Windows 10 IOT上使用I2C读取传感器时出现问题

C# 在Windows 10 IOT上使用I2C读取传感器时出现问题,c#,raspberry-pi3,i2c,windows-10-iot-core,C#,Raspberry Pi3,I2c,Windows 10 Iot Core,我使用的是最新发布的Windows 10 IoT 17763.253,从i2c二氧化碳传感器读取数据时出现问题 奇怪的是,对于其他传感器来说,这似乎不是一个问题 每隔一段时间,它就会损坏最后两个utf8字符,例如1126显示为11\u0011/2,其中最后1/2是一个utf8字符。很多时候,钻石问号替换字符也出现在那里 有没有办法解决这个问题?我正在使用最新版本的vs2019、Raspberry Pi 3和Windows 17763.253 代码: 使用系统; 利用制度全球化; 使用系统文本;

我使用的是最新发布的Windows 10 IoT 17763.253,从i2c二氧化碳传感器读取数据时出现问题

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

每隔一段时间,它就会损坏最后两个utf8字符,例如1126显示为11\u0011/2,其中最后1/2是一个utf8字符。很多时候,钻石问号替换字符也出现在那里

有没有办法解决这个问题?我正在使用最新版本的vs2019、Raspberry Pi 3和Windows 17763.253

代码:

使用系统;
利用制度全球化;
使用系统文本;
使用System.Text.RegularExpressions;
使用System.Threading.Tasks;
使用Windows.Devices.I2c;
公共字符串GetReading()
{
尝试
{
字节[]i2CReadBuffer=新字节[20];
_设备读取(i2CReadBuffer);
Task.Delay(300.Wait();//MXu
字符串回答_string=“”;
bool got_error=false;
int bufsize=i2CReadBuffer.Length;

对于数据表中的(int i=0;i),编码是ASCII,但不是代码中使用的UTF8。此外,发送命令后是否延迟了300ms?您可以通过以十六进制打印所有响应数据来解决此问题

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

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

我想你可以尝试移动读前延迟的方法

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);
   }
}
公共字符串GetReading()
{
尝试
{
Task.Delay(300.Wait();//MXu
字节[]i2CReadBuffer=新字节[20];
_设备读取(i2CReadBuffer);
字符串回答_string=“”;
bool got_error=false;
int bufsize=i2CReadBuffer.Length;

对于(int i=0;iMichael,感谢您的响应。您能更具体地说明我应该在发送后等待300毫秒的命令吗?是_device.Read()?谢谢。我重新编写了代码,只使用ASCII码并进行了延迟。
_device.Read(i2CReadBuffer);Task.delay(300)。wait()//MXu string answer_string=“”;bool got_error=false;int bufsize=i2CReadBuffer.Length;for(int i=0;iMichael,我不明白你的建议(你的意思)@bluerain,我已经更新了响应。当你打印所有接收到的数据时,数据是否正确?Michael Xu,当我打印接收到的数据时,数据仍然不正确。我开始认为这可能是二氧化碳传感器硬件的问题,并将用Arduino对其进行测试。我还将使用逻辑分析仪验证是否存在错误逻辑上的不一致。非常非常感谢。我怎样才能把你的答案标记为正确?你还有其他的建议来测试它吗?
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);
   }
}