/// <summary> /// Чтение указанного типа параметра /// </summary> /// <param name="paramType"></param> /// <param name="recParams"></param> /// <returns></returns> private bool ReadParams(byte paramType, ref RecordParamsEnergy recParams) { byte[] cmnd = new byte[32]; byte[] answer = new byte[32]; byte[] command = new byte[] { 0x08, 0x16 }; byte answ_size = 0; byte status = 0; ushort temp_word = 0; uint tmpValue = 0; int asign = 1; switch (paramType) { case 0x0: case 0x4: case 0x8: answ_size = RPOWER_SIZE; break; case 0x11: answ_size = RVOLTAGE_SIZE; break; case 0x21: answ_size = RTOK_SIZE; break; case 0x30: answ_size = RKOEF_SIZE; break; case 0x40: answ_size = RFREQUENCY_SIZE; break; case 0x51: answ_size = RANGLE_SIZE; break; default: return false; } command.CopyTo(cmnd, 0); cmnd[2] = paramType; if (!SendCommand(cmnd, ref answer, 3, answ_size, ref status)) return false; switch (answ_size) { case 6: recParams.phase_1 = 0; recParams.phase_2 = 0; recParams.phase_3 = 0; temp_word = BitConverter.ToUInt16(answer, 2); if (temp_word == 0xFFFF) temp_word = 0; recParams.phase_sum = Convert.ToUInt32(temp_word); break; case 12: // фаза 1 temp_word = BitConverter.ToUInt16(answer, 2); if (temp_word == 0xFFFF) temp_word = 0; recParams.phase_1 = Convert.ToSingle(temp_word); // фаза 2 temp_word = BitConverter.ToUInt16(answer, 5); if (temp_word == 0xFFFF) temp_word = 0; recParams.phase_2 = Convert.ToSingle(temp_word); // фаза 3 temp_word = BitConverter.ToUInt16(answer, 8); if (temp_word == 0xFFFF) temp_word = 0; recParams.phase_3 = Convert.ToSingle(temp_word); break; case 15: // по сумме фаз asign = (1 + (-2 * ((answer[1] >> 7) & 1))); tmpValue = Convert.ToUInt32(answer[1] & 0x3F); //int asign = (1 + (-2 * ((answer[1] >> 6) & 1))); tmpValue <<= 16; temp_word = BitConverter.ToUInt16(answer, 2); tmpValue += temp_word; recParams.phase_sum = Convert.ToSingle(tmpValue * asign); // фаза 1 asign = (1 + (-2 * ((answer[4] >> 7) & 1))); tmpValue = Convert.ToUInt32(answer[4] & 0x3F); tmpValue <<= 16; temp_word = BitConverter.ToUInt16(answer, 5); tmpValue += temp_word; recParams.phase_1 = Convert.ToSingle(tmpValue * asign); // фаза 2 asign = (1 + (-2 * ((answer[7] >> 7) & 1))); tmpValue = Convert.ToUInt32(answer[7] & 0x3F); tmpValue <<= 16; temp_word = BitConverter.ToUInt16(answer, 8); tmpValue += temp_word; recParams.phase_2 = Convert.ToSingle(tmpValue * asign); // фаза 3 asign = (1 + (-2 * ((answer[10] >> 7) & 1))); tmpValue = Convert.ToUInt32(answer[10] & 0x3F); tmpValue <<= 16; temp_word = BitConverter.ToUInt16(answer, 11); tmpValue += temp_word; recParams.phase_3 = Convert.ToSingle(tmpValue * asign); break; default: break; } return true; }
/// <summary> /// Чтение параметров качества энергии /// </summary> /// <param name="listRecordsParamEnergy"></param> /// <returns></returns> public void PowerQualityParams(out List<RecordParamsEnergy> listRecordsParamEnergy) { listRecordsParamEnergy = new List<RecordParamsEnergy>(); RecordParamsEnergy recParams = new RecordParamsEnergy(); // читаем напряжение if (this.ReadParams(0x11, ref recParams)) { recParams.phase_1 /= 100f; recParams.phase_2 /= 100f; recParams.phase_3 /= 100f; recParams.type = 1; listRecordsParamEnergy.Add(recParams); } // читаем ток if (this.ReadParams(0x21, ref recParams)) { recParams.phase_1 /= 1000f; recParams.phase_2 /= 1000f; recParams.phase_3 /= 1000f; recParams.type = 2; listRecordsParamEnergy.Add(recParams); } // читаем частоту сети if (this.ReadParams(0x40, ref recParams)) { recParams.phase_sum /= 100f; recParams.type = 3; listRecordsParamEnergy.Add(recParams); } // угол между фазными напряжениями if (this.ReadParams(0x51, ref recParams)) { recParams.phase_1 /= 100f; recParams.phase_2 /= 100f; recParams.phase_3 /= 100f; recParams.type = 4; listRecordsParamEnergy.Add(recParams); } // читаем коэффициенты мощности if (this.ReadParams(0x30, ref recParams)) { recParams.phase_sum /= 1000f; recParams.phase_1 /= 1000f; recParams.phase_2 /= 1000f; recParams.phase_3 /= 1000f; recParams.type = 5; listRecordsParamEnergy.Add(recParams); } // читаем мощность P if (this.ReadParams(0x0, ref recParams)) { recParams.phase_sum /= (100f * 1000f); recParams.phase_1 /= (100f * 1000f); recParams.phase_2 /= (100f * 1000f); recParams.phase_3 /= (100f * 1000f); recParams.type = 6; listRecordsParamEnergy.Add(recParams); } // читаем мощность S if (this.ReadParams(0x8, ref recParams)) { recParams.phase_sum /= (100f * 1000f); recParams.phase_1 /= (100f * 1000f); recParams.phase_2 /= (100f * 1000f); recParams.phase_3 /= (100f * 1000f); recParams.type = 7; listRecordsParamEnergy.Add(recParams); } // читаем мощность Q if (this.ReadParams(0x4, ref recParams)) { recParams.phase_sum /= (100f * 1000f); recParams.phase_1 /= (100f * 1000f); recParams.phase_2 /= (100f * 1000f); recParams.phase_3 /= (100f * 1000f); recParams.type = 8; listRecordsParamEnergy.Add(recParams); } }
/// <summary> /// Чтение указанного типа параметра /// </summary> /// <param name="paramType"></param> /// <param name="recParams"></param> /// <returns></returns> private bool ReadParams(byte paramType, ref RecordParamsEnergy recParams) { byte[] cmnd = new byte[32]; byte[] answer = new byte[32]; byte[] command = new byte[] { 0x08, 0x1B, 0x2}; command.CopyTo(cmnd, 0); cmnd[3] = paramType; //WriteToLog("ReadParams=" + BitConverter.ToString(m_cmd)); if (!SendCommand(cmnd, ref answer, 4, READ_PARAMS_OF_ENERGY)) return false; // по сумме фаз recParams.phase_sum = Math.Round(BitConverter.ToSingle(answer, 1), 3); // фаза 1 recParams.phase_1 = Math.Round(BitConverter.ToSingle(answer, 5), 3); // фаза 2 recParams.phase_2 = Math.Round(BitConverter.ToSingle(answer, 9), 3); // фаза 3 recParams.phase_3 = Math.Round(BitConverter.ToSingle(answer, 13), 3); return true; }