在Windows 10 IOT上使用I2C从Sensor读取问题

时间:2019-02-01 22:18:04

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

我正在使用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

1 个答案:

答案 0 :(得分:2)

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

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

  

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

enter image description here

我认为您可以尝试将延迟时间移到Read方法之前。

public class DatabaseHelper2 extends SQLiteOpenHelper {

    private final static String DBNAME = "buyerdb";
    private final static int DBVERSION = 1;

    public final static String TBL_COMMENT = "comment";
    public final static String COL_COMMENT_ID = BaseColumns._ID;
    public final static String COL_COMMENT_NAME = "name";
    public final static String COL_COMMENT_COMMENT = "comment";
    public final static String COl_COMMENT_TIMESTAMP = "timestamp";

    private String crt_tbl_comment = "CREATE TABLE IF NOT EXISTS " + TBL_COMMENT + "(" +
            COL_COMMENT_ID + " INTEGER PRIMARY KEY, " +
            COL_COMMENT_NAME + " TEXT," +
            COL_COMMENT_COMMENT + " TEXT, " +
            COl_COMMENT_TIMESTAMP + " TEXT DEFAULT CURRENT_TIMESTAMP" +
            ")";

    SQLiteDatabase mDB;

    public DatabaseHelper2(Context context) {
        super(context, DBNAME, null, DBVERSION);
        mDB = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(crt_tbl_comment);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long addData(String name, String comment) {
        ContentValues cv = new ContentValues();
        cv.put(COL_COMMENT_NAME,name);
        cv.put(COL_COMMENT_COMMENT,comment);
        return mDB.insert(TBL_COMMENT,null,cv);
    }

    public Cursor getLatestComment() {
        return mDB.query(TBL_COMMENT,null,null,null,null,null,COl_COMMENT_TIMESTAMP + " DESC","1");
    }

    public Cursor getAllButLatestComment() {
        String whereclause = COL_COMMENT_ID + " < (SELECT max(" +
                COL_COMMENT_ID +
                ") FROM " + TBL_COMMENT +
                ")";
        return mDB.query(TBL_COMMENT,null,whereclause,null,null,null,COl_COMMENT_TIMESTAMP + " DESC");
    }
}