/// <summary> /// /// </summary> /// <param name="channelNumber"></param> /// <returns></returns> public static int[] IICReadRawdata(int channelNumber, double adc2vol) { byte[] IIcWriteBuffer = new byte[256]; byte[] IIcReadBuffer = new byte[256]; ushort crc16 = 0; IIcWriteBuffer[0] = 0xA0; IIcWriteBuffer[1] = (byte)RegList.Reg.REG_DEBUG_MODE; IIcWriteBuffer[2] = 0x01; uint writeLen = 3; uint readLen = 0; IICWriteRead(0, writeLen, MyConvertor.byteArrToUint(IIcWriteBuffer), readLen, MyConvertor.byteArrToUint(IIcReadBuffer)); IIcWriteBuffer[1] = (byte)RegList.Reg.REG_DATA_READY; writeLen = 2; readLen = 1; int iCount = 0; do { Thread.Sleep(10); IICWriteRead(0, writeLen, MyConvertor.byteArrToUint(IIcWriteBuffer), readLen, MyConvertor.byteArrToUint(IIcReadBuffer)); iCount++; } while (IIcReadBuffer[0] == 0 && iCount < 100); // 初始化异常值 int[] rawdata = new int[0]; if (IIcReadBuffer[0] == channelNumber) { rawdata = new int[channelNumber]; for (int i = 0; i < rawdata.Length; i++) { IIcWriteBuffer[1] = (byte)(RegList.Reg.REG_DEBUG_DATA1 + i); writeLen = 2; readLen = 2; IICWriteRead(0, writeLen, MyConvertor.byteArrToUint(IIcWriteBuffer), readLen, MyConvertor.byteArrToUint(IIcReadBuffer)); rawdata[i] = (short)(MyConvertor.byteArrToInt(IIcReadBuffer[1] << 8 | IIcReadBuffer[0]) * adc2vol); } } return(rawdata); }
/// <summary> /// 从IIC读取固件版本 /// </summary> /// <returns></returns> public static int IICReadFWVersion() { byte[] IIcWriteBuffer = new byte[256]; byte[] IIcReadBuffer = new byte[256]; IIcWriteBuffer[0] = 0xA0; IIcWriteBuffer[1] = (byte)RegList.Reg.REG_FW_VERSION; uint writeLen = 2; uint readLen = 2; IICWriteRead(0, writeLen, MyConvertor.byteArrToUint(IIcWriteBuffer), readLen, MyConvertor.byteArrToUint(IIcReadBuffer)); int value = MyConvertor.byteArrToInt(IIcReadBuffer[1] << 8 | IIcReadBuffer[0]); return(value); }
/// <summary> /// 从IIC读取厂家ID /// </summary> /// <returns></returns> public static int IICReadManuFactorID() { byte[] IIcWriteBuffer = new byte[256]; byte[] IIcReadBuffer = new byte[256]; IIcWriteBuffer[0] = 0xA0; IIcWriteBuffer[1] = (byte)RegList.Reg.REG_MANUFACTURER_ID; uint writeLen = 2; uint readLen = 2; IICWriteRead(0, writeLen, MyConvertor.byteArrToUint(IIcWriteBuffer), readLen, MyConvertor.byteArrToUint(IIcReadBuffer)); int value = MyConvertor.byteArrToInt(IIcReadBuffer[1] << 8 | IIcReadBuffer[0]); return(value); }
/// <summary> /// 从IIC中读取AFE信息, 包括Gain, Channel Number和Offset /// </summary> /// <returns></returns> public static object IICReadAFEInfo() { DebugModeBClean(); byte[] IIcWriteBuffer = new byte[256]; byte[] IIcReadBuffer = new byte[256]; IIcWriteBuffer[0] = 0xA0; IIcWriteBuffer[1] = (byte)RegList.Reg.REG_DEBUG_MODEB; IIcWriteBuffer[2] = (byte)RegList.DebugMode.DEBUG_MODE_AFE_INFO; uint writeLen = 3; uint readLen = 0; IICWriteRead(0, writeLen, MyConvertor.byteArrToUint(IIcWriteBuffer), readLen, MyConvertor.byteArrToUint(IIcReadBuffer)); IIcWriteBuffer[1] = (byte)RegList.Reg.REG_DATA_READYB; writeLen = 2; readLen = 1; int iCount = 0; do { Thread.Sleep(10); IICWriteRead(0, writeLen, MyConvertor.byteArrToUint(IIcWriteBuffer), readLen, MyConvertor.byteArrToUint(IIcReadBuffer)); iCount++; } while (IIcReadBuffer[0] == 0 && iCount < 100); // 初始化一个异常值 int gain = -1; int chnlNum = -1; int[] offset = new int[0]; if (IIcReadBuffer[0] != 0) { uint iAFEInfoBytes = IIcReadBuffer[0]; IIcWriteBuffer[1] = (byte)RegList.Reg.REG_DEBUG_DATAB; writeLen = 2; readLen = iAFEInfoBytes; IICWriteRead(0, writeLen, MyConvertor.byteArrToUint(IIcWriteBuffer), readLen, MyConvertor.byteArrToUint(IIcReadBuffer)); if (iAFEInfoBytes == 2 + IIcReadBuffer[1] * 4) { gain = IIcReadBuffer[0]; chnlNum = IIcReadBuffer[1]; int iGetChnlNum = chnlNum; offset = new int[iGetChnlNum]; for (int chnl = 0; chnl < iGetChnlNum; chnl++) { offset[chnl] = (IIcReadBuffer[2 + chnl * 4 + 1]); offset[chnl] <<= 8; offset[chnl] += (IIcReadBuffer[2 + chnl * 4]); offset[chnl] = MyConvertor.byteArrToInt(offset[chnl]); } } else { // AFE, Offset, ChannelNum获取失败 } } else { // DataReady始终为0, AFE等信息获取失败 } DebugModeBClean(); return(new object[] { gain, chnlNum, offset }); }