public Exhaust_Data Getdata_MQ411() { Exhaust_Data exhaust_data = new Exhaust_Data(); Random rd = new Random(); exhaust_data.HC = 0; exhaust_data.CO = 0; exhaust_data.NO = 0; exhaust_data.CO2 = 0; exhaust_data.λ = 0; exhaust_data.HC = rd.Next(15, 30); exhaust_data.CO = rd.Next(5, 10) / 10f; exhaust_data.NO = rd.Next(15, 30); exhaust_data.CO2 = rd.Next(5, 25) / 10f; exhaust_data.λ = rd.Next(94, 113) / 100f; return(exhaust_data); }
/// <summary> /// 获取实时数据 至少耗时500ms /// </summary> /// <returns>Exhaust_Data 如果值全为0则表明通讯失败</returns> public Exhaust_Data GetData() { ReadData(); Exhaust_Data exhaust_data = new Exhaust_Data(); exhaust_data.CO2 = 0; exhaust_data.CO = 0; exhaust_data.HC = 0; exhaust_data.NO = 0; exhaust_data.O2 = 0; exhaust_data.SD = 0; exhaust_data.YW = 0; exhaust_data.HJWD = 0; exhaust_data.ZS = 0; exhaust_data.QLYL = 0; exhaust_data.λ = 0; exhaust_data.HJYL = 0; int i = 0; byte[] Content = new byte[] { }; SendData(Cmd_GetData, Content); Thread.Sleep(500); while (!Read_Flag) //等待仪器返回 { i++; Thread.Sleep(10); if (i == 100) { return(exhaust_data); } } ReadData(); if (Read_Buffer[0] == 0x6 && Read_Buffer[1] == 0x60 && Read_Buffer[2] == 0x1B) { byte[] temp_byte = new byte[2]; temp_byte[0] = Read_Buffer[4]; temp_byte[1] = Read_Buffer[3]; exhaust_data.CO2 = BitConverter.ToInt16(temp_byte, 0) / 100f; //二氧化碳 temp_byte[0] = Read_Buffer[6]; temp_byte[1] = Read_Buffer[5]; exhaust_data.CO = BitConverter.ToInt16(temp_byte, 0) / 100f;; //一氧化碳 temp_byte[0] = Read_Buffer[8]; temp_byte[1] = Read_Buffer[9]; exhaust_data.HC = BitConverter.ToInt16(temp_byte, 0); //碳氢 temp_byte[0] = Read_Buffer[10]; temp_byte[1] = Read_Buffer[9]; exhaust_data.NO = BitConverter.ToInt16(temp_byte, 0); //一氧化氮 temp_byte[0] = Read_Buffer[12]; temp_byte[1] = Read_Buffer[11]; exhaust_data.O2 = BitConverter.ToInt16(temp_byte, 0) / 100f; //氧气 temp_byte[0] = Read_Buffer[14]; temp_byte[1] = Read_Buffer[13]; exhaust_data.SD = BitConverter.ToInt16(temp_byte, 0) / 10f; //湿度 temp_byte[0] = Read_Buffer[16]; temp_byte[1] = Read_Buffer[15]; exhaust_data.YW = BitConverter.ToInt16(temp_byte, 0) / 10f; //油温 temp_byte[0] = Read_Buffer[18]; temp_byte[1] = Read_Buffer[17]; exhaust_data.HJWD = BitConverter.ToInt16(temp_byte, 0) / 10f; //环境温度 temp_byte[0] = Read_Buffer[20]; temp_byte[1] = Read_Buffer[19]; exhaust_data.ZS = BitConverter.ToInt16(temp_byte, 0); //转速 temp_byte[0] = Read_Buffer[22]; temp_byte[1] = Read_Buffer[21]; exhaust_data.QLYL = BitConverter.ToInt16(temp_byte, 0) / 10f; //油路压力 temp_byte[0] = Read_Buffer[24]; temp_byte[1] = Read_Buffer[23]; exhaust_data.λ = BitConverter.ToInt16(temp_byte, 0) / 1000f; //燃空比λ temp_byte[0] = Read_Buffer[26]; temp_byte[1] = Read_Buffer[25]; exhaust_data.HJYL = BitConverter.ToInt16(temp_byte, 0) / 10f; //环境压力 kpa } return(exhaust_data); }