简体   繁体   中英

Problem reading from Sensor Using I2C on Windows 10 IOT

I'm using the latest public release of Windows 10 IoT 17763.253, and I am having a problem reading from an i2c Co2 sensor.

It doesn't seem to be an issue for other sensors, strangely.

Every so often, it mangles the last two utf8 characters eg 1126 appears as 11\/2 where the last 1/2 is a single UTF8 character. Many times, the Diamond Question Mark Replacement character also appears there.

Any ideas on how to fix it? I'm using the latest build of vs2019, Raspberry Pi 3 and Windows 17763.253

Code:

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;
            }

Our Sensor: https://www.atlas-scientific.com/_files/_datasheets/_probe/EZO_CO2_Datasheet.pdf

From the datasheet, the encoding is ASCII,but not UTF8 which was used in your code. In addition, did you delay 300ms after sending the command? You can troubleshoot this issue with printing all the response data in hex.

In page "Response codes & processing delay", the example shows the work flow for requesting data from device. Please note that.

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

在此处输入图片说明

I think you can try to move the delay before Read method.

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);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM