Beispiel #1
0
        public bool ReadCurrentValues(ref Values values)
        {
            values = new Values();
            values.listRV = new List<RecordValue>();
            byte[] command = { 0x27 };
            byte[] answer = new byte[23];
            uint total = 0;

            /*
            CMD = 27h
            ADDR(4)-CMD(1)-CRC(2) ( формат ответа ADDR-CMD-count*4-CRC)
            Count = 4 bytes = 0…99999999h
                                            4-х байтовое значение
                                            потребленной эл.энергии
                                            в десятках Вт.ч
                                            Старшие разряды вперёд.
                                            Возвращает сразу по всем тарифам.
            */

            if (!SendCommand(command, ref answer, 1, 23))
                return false;

            for (int t = 0; t < m_listTypesForRead.Count; t++)
            {
                RecordValue recordValue;
                recordValue.type = m_listTypesForRead[t];
                recordValue.fine_state = true;
                recordValue.value = 0;

                for (int i = 0; i < 4; i++)
                {
                    if (answer[5 + i + (m_listTypesForRead[t] - 1) * 4] == 0xFF)
                    {
                        recordValue.fine_state = false;
                        break;
                    }
                }

                if (recordValue.fine_state)
                {
                    total = Convert.ToUInt32(CommonMeters.DEC2HEX(answer[5 + (m_listTypesForRead[t] - 1) * 4])) * 1000000 +
                            Convert.ToUInt32(CommonMeters.DEC2HEX(answer[6 + (m_listTypesForRead[t] - 1) * 4])) * 10000 +
                            Convert.ToUInt32(CommonMeters.DEC2HEX(answer[7 + (m_listTypesForRead[t] - 1) * 4])) * 100 +
                            Convert.ToUInt32(CommonMeters.DEC2HEX(answer[8 + (m_listTypesForRead[t] - 1) * 4]));

                    recordValue.value = Convert.ToSingle(total) / 100;// т.к. передаётся в десятках ватт
                }

                values.listRV.Add(recordValue);
            }

            return true;
        }
Beispiel #2
0
 public bool ReadDailyValues(byte day, byte month, ushort year, ref Values values)
 {
     return false;
 }
Beispiel #3
0
        public bool ReadMonthlyValues(DateTime dt, ushort param, ushort tarif, ref float recordValue)
        {
            List<byte> types = new List<byte>();
            types.Add((byte)(param & 0x000000FF));
            ClearTypesForRead();
            SetTypesForRead(types);
            Values vals = new Values();
            if (ReadMonthlyValues((byte)dt.Month, (ushort)dt.Year, ref vals))
            {
                if (vals.listRV.Count == 1)
                {
                    RecordValue rv = vals.listRV[vals.listRV.Count - 1];
                    recordValue = (float)rv.value;
                    return true;
                }
            }

            WriteToLog("ReadMonthlyValues: невозможно считать текущее значение для параметра " + param.ToString());
            return false;
        }
Beispiel #4
0
        private bool ReadArchive(TypeDataPulsar type, DateTime date,  ref Values values)
        {
            values.listRV = new List<RecordValue>();
            m_length_cmd = 0;
            Byte[] in_buffer = new Byte[255];

            ushort rnd = 0x50b9;
            byte func = 0;

            byte out_packet_length = 0;
            int bytes_to_read = -1;

            func = 6;
            out_packet_length = 28;

            // адрес
            byte[] adr = new byte[4];
            Int2BCD((int)m_address, adr);

            m_cmd = new byte[out_packet_length];

            for (int t = 0; t < m_listTypesForRead.Count; t++)
            {
                m_length_cmd = 0;

                // битовая маска каналов
                uint channel_mask = 0;
                uint tmp = 1;
                tmp = tmp << (m_listTypesForRead[t] - 1);
                channel_mask |= tmp;

                byte[] parameter = BitConverter.GetBytes(channel_mask);
                byte[] random = BitConverter.GetBytes(rnd);

                // формируем команду
                // адрес
                for (int i = 0; i < adr.Length; i++)
                {
                    m_cmd[m_length_cmd++] = adr[i];
                }
                // номер функции
                m_cmd[m_length_cmd++] = func;
                // общая длина пакета
                m_cmd[m_length_cmd++] = out_packet_length;
                // параметры
                for (int i = 0; i < parameter.Length; i++)
                {
                    m_cmd[m_length_cmd++] = parameter[i];
                }

                bytes_to_read = 10 + 2 + 2 + 6 + 4 * 1;

                // тип архива
                m_cmd[m_length_cmd++] = Convert.ToByte(type);
                m_cmd[m_length_cmd++] = 0;
                // дата - начало
                m_cmd[m_length_cmd++] = Convert.ToByte(date.Year - 2000);
                m_cmd[m_length_cmd++] = Convert.ToByte(date.Month);
                m_cmd[m_length_cmd++] = Convert.ToByte(date.Day);
                m_cmd[m_length_cmd++] = 0;
                m_cmd[m_length_cmd++] = 0;
                m_cmd[m_length_cmd++] = 0;
                // дата - конец
                m_cmd[m_length_cmd++] = Convert.ToByte(date.Year - 2000);
                m_cmd[m_length_cmd++] = Convert.ToByte(date.Month);
                m_cmd[m_length_cmd++] = Convert.ToByte(date.Day);
                m_cmd[m_length_cmd++] = 0;
                m_cmd[m_length_cmd++] = 0;
                m_cmd[m_length_cmd++] = 0;

                // ID
                for (int i = 0; i < random.Length; i++)
                {
                    m_cmd[m_length_cmd++] = random[i];
                }

                // CRC16
                byte[] crc16 = CRC16(m_cmd, m_length_cmd);
                for (int i = 0; i < crc16.Length; i++)
                {
                    m_cmd[m_length_cmd++] = crc16[i];
                }

                if (m_vport.WriteReadData(FindPacketSignature, m_cmd, ref in_buffer, m_length_cmd, bytes_to_read) > 0)
                {
                    bool find_header = true;

                    // длина пакета
                    byte packet_length = 0;

                    // проверка заголовка пакета
                    for (int i = 0; i < 5; i++)
                    {
                        if (m_cmd[i] != in_buffer[i])
                        {
                            find_header = false;
                        }
                    }

                    if (find_header)
                    {
                        packet_length = in_buffer[5];

                        // проверка CRC
                        crc16 = CRC16(in_buffer, packet_length - 2);

                        if (in_buffer[packet_length - 2] == crc16[0] && in_buffer[packet_length - 1] == crc16[1])
                        {
                            // проверка ID
                            if (m_cmd[out_packet_length - 4] == in_buffer[packet_length - 4] && m_cmd[out_packet_length - 3] == in_buffer[packet_length - 3])
                            {
                                RecordValue recordValue;
                                recordValue.type = m_listTypesForRead[t];
                                recordValue.fine_state = true;
                                recordValue.value = 0;

                                int data_elements_count = (packet_length - 10 - 4 - 6) / 4;

                                //WriteToLog("type archive=" + type.ToString() + "; data_elements_count=" + data_elements_count.ToString() + "; Date=" + date.ToShortDateString());

                                if (data_elements_count > 0)
                                {
                                    int iyear = in_buffer[6 + 4] + 2000;
                                    int imon = in_buffer[6 + 4 + 1];
                                    int iday = in_buffer[6 + 4 + 2];

                                    //WriteToLog("dt_begin=" + date.ToString() + "; idate=" + iday.ToString() + "." + imon.ToString() + "." + iyear.ToString());

                                    if (date.Year == iyear && date.Month == imon && date.Day == iday)
                                    {
                                        data_elements_count = 1;

                                        for (int d = 0; d < data_elements_count; d++)
                                        {
                                            byte[] temp_buff = new byte[4];
                                            for (int b = 0; b < 4; b++)
                                            {
                                                temp_buff[b] = in_buffer[6 + 4 + 6 + d * 4 + b];
                                            }

                                            if ((temp_buff[0] == 0xF0 || temp_buff[0] == 0xF1) && temp_buff[1] == 0xFF && temp_buff[2] == 0xFF && temp_buff[3] == 0xFF)
                                            {
                                                recordValue.fine_state = true;
                                                recordValue.value = 0;
                                            }
                                            else
                                            {
                                                recordValue.fine_state = true;
                                                recordValue.value = Math.Round(BitConverter.ToSingle(temp_buff, 0), 4);
                                            }

                                            //WriteToLog("value=" + recordValue.value.ToString());

                                            values.listRV.Add(recordValue);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return true;
        }
Beispiel #5
0
 public bool ReadDailyValues(byte day, byte month, ushort year, ref Values values)
 {
     return ReadArchive(TypeDataPulsar.Daily, new DateTime(year, month, day), ref values);
 }
Beispiel #6
0
 public bool ReadMonthlyValues(byte month, ushort year, ref Values values)
 {
     return ReadArchive(TypeDataPulsar.Monthly, new DateTime(year, month, 1), ref values);
 }
Beispiel #7
0
        public bool ReadCurrentValues(ref Values values)
        {
            m_length_cmd = 0;
            Byte[] in_buffer = new Byte[255];

            ushort rnd = 0x50b9;
            byte func = 0;

            uint channel_mask = 0;
            byte out_packet_length = 0;
            int bytes_to_read = -1;

            func = 1;
            out_packet_length = 14;

            // битовая маска каналов
            for (int i = 0; i < m_listTypesForRead.Count; i++)
            {
                uint tmp = 1;
                tmp = tmp << (m_listTypesForRead[i] - 1);
                channel_mask |= tmp;
            }

            // адрес
            byte[] adr = new byte[4];
            Int2BCD((int)m_address, adr);

            byte[] parameter = BitConverter.GetBytes(channel_mask);
            byte[] random = BitConverter.GetBytes(rnd);

            m_cmd = new byte[out_packet_length];

            // формируем команду
            // адрес
            for (int i = 0; i < adr.Length; i++)
            {
                m_cmd[m_length_cmd++] = adr[i];
            }
            // номер функции
            m_cmd[m_length_cmd++] = func;
            // общая длина пакета
            m_cmd[m_length_cmd++] = out_packet_length;
            // параметры
            for (int i = 0; i < parameter.Length; i++)
            {
                m_cmd[m_length_cmd++] = parameter[i];
            }

            // ID
            for (int i = 0; i < random.Length; i++)
            {
                m_cmd[m_length_cmd++] = random[i];
            }

            // CRC16
            byte[] crc16 = CRC16(m_cmd, m_length_cmd);
            for (int i = 0; i < crc16.Length; i++)
            {
                m_cmd[m_length_cmd++] = crc16[i];
            }

            // байт для чтения
            bytes_to_read = 10 + 8 * m_listTypesForRead.Count;

            //WriteToLog("m_length_cmd=" + m_length_cmd.ToString());

            //WriteToLog("bytes_to_read=" + bytes_to_read.ToString());

            if (m_vport.WriteReadData(FindPacketSignature, m_cmd, ref in_buffer, m_length_cmd, bytes_to_read) > 0)
            {
                //WriteToLog("WriteReadData");
                bool find_header = true;

                // длина пакета
                byte packet_length = 0;

                // проверка заголовка пакета
                for (int i = 0; i < 5; i++)
                {
                    if (m_cmd[i] != in_buffer[i])
                    {
                        find_header = false;
                    }
                }

                if (find_header)
                {
                    //WriteToLog("find_header");
                    packet_length = in_buffer[5];

                    // проверка CRC
                    crc16 = CRC16(in_buffer, packet_length - 2);

                    if (in_buffer[packet_length - 2] == crc16[0] && in_buffer[packet_length - 1] == crc16[1])
                    {
                        // проверка ID
                        if (m_cmd[out_packet_length - 4] == in_buffer[packet_length - 4] && m_cmd[out_packet_length - 3] == in_buffer[packet_length - 3])
                        {
                            for (int i = 0; i < m_listTypesForRead.Count; i++)
                            {
                                RecordValue recordValue;
                                recordValue.type = m_listTypesForRead[i];
                                recordValue.fine_state = true;
                                recordValue.value = 0;
                                recordValue.value = Math.Round(BitConverter.ToDouble(in_buffer, 6 + i * 8), 4);
                                recordValue.fine_state = true;
                                 values.listRV = new List<RecordValue>();
                                values.listRV.Add(recordValue);
                            }

                            return true;
                        }
                    }
                }
            }

            return false;
        }
Beispiel #8
0
        public bool ReadCurrentValues(ref Values values)
        {
            byte[] value_sizes = new byte[] {4, 4, 4, 2, 1, 4 };
            byte[] lengths = new byte[] { 52, 64, 16, 24, 12, 48 };
            byte[] begin_address = new byte[] { 0x38, 0x6C, 0xAC, 0xC8, 0xE0, 0x08 };
            byte[] answer_bytes;
            byte number_value = 1;

            try
            {
                for (int i = 0; i < lengths.Length & i < begin_address.Length & i < value_sizes.Length; i++)
                {
                    answer_bytes = new byte[READVALUES_CMD_ANSWER_SIZE + lengths[i]];

                    if (ReadValuesFromAddress(TypesCommands.tc2K, lengths[i], begin_address, answer_bytes, i))
                    {
                        List<double> temp_values = new List<double>();

                        switch (value_sizes[i])
                        {
                            case 1:
                                for (int n = 0; n < lengths[i] / value_sizes[i]; n++)
                                {
                                    byte value = answer_bytes[m_header_length + n * value_sizes[i]];
                                        temp_values.Add((double)value / 100f);
                                }
                                break;
                            case 2:
                                for (int n = 0; n < lengths[i] / value_sizes[i]; n++)
                                {
                                    byte[] temp_buff = CommonMeters.InverseBytesOrder(answer_bytes, Convert.ToUInt32(m_header_length + n * value_sizes[i] + 1), value_sizes[i]);
                                    ushort integer_part = BitConverter.ToUInt16(temp_buff, 0);
                                    temp_values.Add((double)integer_part / 100f);
                                }
                                break;
                            case 4:
                                for (int n = 0; n < lengths[i] / value_sizes[i]; n++)
                                {
                                    byte[] temp_buff = CommonMeters.InverseBytesOrder(answer_bytes, Convert.ToUInt32(m_header_length + n * value_sizes[i] + 3), value_sizes[i]);
                                    temp_values.Add((double)(begin_address[i] == 0x08 ? BitConverter.ToSingle(temp_buff, 0) : BitConverter.ToUInt32(temp_buff, 0)));
                                }
                                break;
                        }

                        if (begin_address[i] == 0x08)
                        {
                            for (byte k = 0; k < temp_values.Count; k++)
                            {
                                int index = values.listRV.FindIndex(x => x.type == (k + 1));
                                if (index >= 0)
                                {
                                   // WriteToLog(temp_values[k].ToString());
                                    RecordValue rv = values.listRV[index];
                                    rv.value += temp_values[k];

                                    values.listRV[index] = rv;
                                }
                            }
                        }
                        else
                        {
                            for (int j = 0; j < temp_values.Count; j++)
                            {
                               // WriteToLog(temp_values[j].ToString());
                                if (m_listTypesForRead.Contains(number_value))
                                {
                                    RecordValue rv;
                                    rv.fine_state = true;
                                    rv.type = number_value;
                                    rv.value = temp_values[j];

                                    values.listRV.Add(rv);
                                }
                                number_value++;
                            }
                        }
                    }
                    else
                    {
                        number_value += (byte)(lengths[i] / value_sizes[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteToLog("ReadCurrentValues: " + ex.Message);
            }

            return (values.listRV.Count == m_listTypesForRead.Count) ? true : false;
        }
Beispiel #9
0
        private bool ReadDataFromFlash512KByAddress(byte[] address_buffer, ref Values values)
        {
            byte[] value_sizes = new byte[] { 4, 4, 4, 2, 1, 4 };
            byte[] lengths = new byte[] { 52, 64, 16, 24, 12, 48 };
            byte[] begin_address = new byte[] { 0x38, 0x6C, 0xAC, 0xC8, 0xE0, 0x08 };
            byte number_value = 1;

            for (int i = 0; i < begin_address.Length; i++)
            {
                address_buffer[3] = begin_address[i];

                byte[] answer_bytes = new byte[READVALUES_CMD_ANSWER_SIZE + lengths[i]];

                if (ReadValuesFromAddress(TypesCommands.tc512K, lengths[i], address_buffer, answer_bytes))
                {
                    List<double> temp_values = new List<double>();

                    switch (value_sizes[i])
                    {
                        case 1:
                            for (int n = 0; n < lengths[i] / value_sizes[i]; n++)
                            {
                                byte value = answer_bytes[m_header_length + n * value_sizes[i]];

                                temp_values.Add((double)value / 100f);
                            }
                            break;
                        case 2:
                            for (int n = 0; n < lengths[i] / value_sizes[i]; n++)
                            {
                                byte[] temp_buff = CommonMeters.InverseBytesOrder(answer_bytes, Convert.ToUInt32(m_header_length + n * value_sizes[i] + 1), value_sizes[i]);
                                ushort integer_part = BitConverter.ToUInt16(temp_buff, 0);
                                temp_values.Add((double)integer_part / 100f);
                            }
                            break;
                        case 4:
                            for (int n = 0; n < lengths[i] / value_sizes[i]; n++)
                            {
                                byte[] temp_buff = CommonMeters.InverseBytesOrder(answer_bytes, Convert.ToUInt32(m_header_length + n * value_sizes[i] + 3), value_sizes[i]);
                                temp_values.Add((double)(begin_address[i] == 0x08 ? BitConverter.ToSingle(temp_buff, 0) : BitConverter.ToUInt32(temp_buff, 0)));
                            }
                            break;
                    }

                    if (begin_address[i] == 0x08)
                    {
                        for (byte k = 0; k < temp_values.Count; k++)
                        {
                            int index = values.listRV.FindIndex(x => x.type == (k + 1));
                            if (index >= 0)
                            {
                                RecordValue rv = values.listRV[index];
                                rv.value += temp_values[k];

                                values.listRV[index] = rv;

                                WriteToLog("temp_values type=" + k.ToString() + "; value = " + temp_values[k].ToString());
                            }
                        }
                    }
                    else
                    {
                        for (int j = 0; j < temp_values.Count; j++)
                        {
                            if (m_listTypesForRead.Contains(number_value))
                            {
                                RecordValue rv;
                                rv.fine_state = true;
                                rv.type = number_value;
                                rv.value = temp_values[j];

                                values.listRV.Add(rv);

                                WriteToLog("value type=" + rv.type.ToString() + "; value = " + rv.value.ToString());
                            }

                            number_value++;
                        }
                    }
                }
                else
                {
                    values.listRV.Clear();
                    break;
                }
            }

            WriteToLog("values.listRV.Count=" + values.listRV.Count.ToString() + "; m_listTypesForRead.Count=" + m_listTypesForRead.Count.ToString());

            return (values.listRV.Count == m_listTypesForRead.Count) ? true : false;
        }
Beispiel #10
0
        /// <summary>
        /// Чтение архивных данных
        /// </summary>
        /// <param name="day"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        private bool ReadFromFlash512K(byte day, byte month, ushort year, ref Values values)
        {
            try
            {
                DateTime date_target = new DateTime(year, month, day, 0, 0, 0);

                if (Math.Round(DateTime.Now.Subtract(date_target).TotalDays, 0) >= 0)
                {
                    byte[] address_buffer = new byte[4];
                    byte[] answer_bytes = new byte[READVALUES_CMD_ANSWER_SIZE + 4];

                    int ihour = 0;
                    int iday = 0;
                    int imonth = 0;
                    int iyear = 0;

                    Date date;
                    date.day = day;
                    date.month = month;
                    date.year = year;

                    WriteToLog("dict_address_by_date count=" + dict_address_by_date.Count.ToString());

                    // если требуемая дата, ранее была обнаружена, то читаем по обнаруженному адресу
                    if (dict_address_by_date.ContainsKey(date))
                    {
                        address_buffer = AddressToBytes(dict_address_by_date[date]);

                        if (ReadValuesFromAddress(TypesCommands.tc512K, 4, address_buffer, answer_bytes))
                        {
                            ihour = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 0]);
                            iday = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 1]);
                            imonth = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 2]);
                            iyear = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 3]) + 2000;

                            // проверка на совпадение прочитанной даты с искомой
                            if (iday == day && imonth == month && iyear == year)
                            {
                                if (ReadDataFromFlash512KByAddress(address_buffer, ref values))
                                {
                                    WriteToLog("Readed data from early finded address : " + new DateTime(iyear, imonth, iday).ToShortDateString());
                                }
                            }
                        }
                    }
                    // в противном случае - ищем запись по архиву
                    else
                    {
                        // чтение данных из первого адреса в архиве, если ранее не было прочитано
                        if (!already_read_first_address)
                        {
                            address_buffer[0] = 0;
                            address_buffer[1] = 6;
                            address_buffer[2] = 0;
                            address_buffer[3] = 0;

                            if (ReadValuesFromAddress(TypesCommands.tc512K, 4, address_buffer, answer_bytes))
                            {
                                already_read_first_address = true;

                                ihour = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 0]);
                                iday = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 1]);
                                imonth = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 2]);
                                iyear = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 3]) + 2000;

                                date_first_readed = new DateTime(iyear, imonth, iday);

                                date.day = iday;
                                date.month = imonth;
                                date.year = iyear;

                                if (!dict_address_by_date.ContainsKey(date))
                                {
                                    dict_address_by_date.Add(date, BytesToAddress(address_buffer));
                                }

                                // проверка на совпадение прочитанной даты с искомой
                                if (iday == day && imonth == month && iyear == year)
                                {
                                    if (ReadDataFromFlash512KByAddress(address_buffer, ref values))
                                    {
                                        WriteToLog("Read from first address. Date=" + new DateTime(iyear, imonth, iday).ToShortDateString() + "; Values.Count = " + values.listRV.Count.ToString());
                                    }
                                }
                            }
                        }
                        // поиск записи с нужной датой в архиве ПУ
                        if (already_read_first_address)
                        {
                            TimeSpan diff_date = date_target - date_first_readed;
                            int iexpected_number = (int)diff_date.TotalDays < m_max_day_records ? (int)diff_date.TotalDays : (m_max_day_records - 1);

                            iexpected_number = iexpected_number < 0 ? m_max_day_records - iexpected_number : iexpected_number;

                            WriteToLog("Difference days of target and first records=" + iexpected_number.ToString());

                            byte[] address_expected_bytes = BitConverter.GetBytes(Convert.ToInt32(iexpected_number * m_sys_int_size));

                            // Чтение искомой записи по предполагаемому адресу
                            address_buffer[0] = 0;
                            address_buffer[1] = Convert.ToByte(6 + address_expected_bytes[2]);
                            address_buffer[2] = address_expected_bytes[1];
                            address_buffer[3] = address_expected_bytes[0];

                            if (ReadValuesFromAddress(TypesCommands.tc512K, 4, address_buffer, answer_bytes))
                            {
                                ihour = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 0]);
                                iday = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 1]);
                                imonth = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 2]);
                                iyear = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 3]) + 2000;

                                date.day = iday;
                                date.month = imonth;
                                date.year = iyear;

                                if (!dict_address_by_date.ContainsKey(date))
                                {
                                    dict_address_by_date.Add(date, BytesToAddress(address_buffer));
                                }

                                // проверка на совпадение прочитанной даты с искомой
                                if (iday == day && imonth == month && iyear == year)
                                {
                                    if (ReadDataFromFlash512KByAddress(address_buffer, ref values))
                                    {
                                        WriteToLog("Read expected. Date=" + new DateTime(iyear, imonth, iday).ToShortDateString() + "; Values.Count = " + values.listRV.Count.ToString());
                                    }
                                }
                                else // последовательное чтение всех записей для поиска записи с нужной датой
                                {
                                    bool finded = false;
                                    bool all_records_check = true;

                                    int iforward = 0;
                                    int ibackward = 0;

                                    for (int n = 0; n < m_max_day_records; n++)
                                    {
                                        int number_record_for_read = 0;

                                        if (n % 2 == 0)
                                        {
                                            number_record_for_read = iexpected_number + ++iforward;
                                            //if (number_record_for_read < m_max_day_records)
                                            //{
                                            //    WriteToLog("iforward=" + iforward.ToString());
                                            //}
                                        }
                                        else
                                        {
                                            number_record_for_read = iexpected_number + --ibackward;
                                            //if (number_record_for_read > 0)
                                            //{
                                            //    WriteToLog("ibackward=" + ibackward.ToString());
                                            //}
                                        }

                                        address_expected_bytes = BitConverter.GetBytes(number_record_for_read * m_sys_int_size);

                                        // искомый адрес
                                        address_buffer[0] = 0;
                                        address_buffer[1] = Convert.ToByte(6 + address_expected_bytes[2]);
                                        address_buffer[2] = address_expected_bytes[1];
                                        address_buffer[3] = address_expected_bytes[0];

                                        if (!dict_address_by_date.ContainsValue(BytesToAddress(address_buffer)))
                                        {
                                            //WriteToLog("Address=" + address_buffer[0].ToString("x") + "|" + address_buffer[1].ToString("x") + "|" + address_buffer[2].ToString("x") + "|" + address_buffer[3].ToString("x"));
                                            if (ReadValuesFromAddress(TypesCommands.tc512K, 4, address_buffer, answer_bytes))
                                            {
                                                ihour = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 0]);
                                                iday = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 1]);
                                                imonth = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 2]);
                                                iyear = (int)Meters.CommonMeters.BCDToByte(answer_bytes[m_header_length + 3]) + 2000;

                                                date.day = iday;
                                                date.month = imonth;
                                                date.year = iyear;

                                                //WriteToLog("ReadValuesFromAddress Date=" + iday.ToString() + "." + imonth.ToString() + "." + iyear.ToString());

                                                if (!dict_address_by_date.ContainsKey(date))
                                                {
                                                    dict_address_by_date.Add(date, BytesToAddress(address_buffer));
                                                }

                                                if (iday == day && imonth == month && iyear == year)
                                                {
                                                    finded = true;

                                                    if (ReadDataFromFlash512KByAddress(address_buffer, ref values))
                                                    {
                                                        WriteToLog("Find required. Date=" + new DateTime(iyear, imonth, iday).ToShortDateString() + "; Values.Count = " + values.listRV.Count.ToString());
                                                    }

                                                    break;
                                                }
                                            }
                                            else
                                            {
                                                all_records_check = false;
                                            }
                                        }
                                    }

                                    // при не нахождении записи
                                    if (finded == false && all_records_check == true)
                                    {
                                        for (byte k = 0; k < m_listTypesForRead.Count; k++)
                                        {
                                            RecordValue rv;
                                            rv.fine_state = false;
                                            rv.type = m_listTypesForRead[k];
                                            rv.value = 0;

                                            values.listRV.Add(rv);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                WriteToLog("ReadFromFlash512K: " + ex.Message);
            }

            return (values.listRV.Count == m_listTypesForRead.Count) ? true : false;
        }
Beispiel #11
0
        public bool ReadMonthlyValues(DateTime dt, ushort param, ushort tarif, ref float recordValue)
        {
            List<byte> sellectedType = new List<byte>(1);
            sellectedType.Add((byte)param);
            SetTypesForRead(sellectedType);

            Values vals = new Values();
            vals.listRV = new List<RecordValue>();

            ReadMonthlyValues((byte)dt.Month, (ushort)dt.Year, ref vals);

            if (vals.listRV.Count == 1)
            {
                recordValue = (float)vals.listRV[0].value;
                WriteToLog("Получено МЕСЯЧНОЕ значение:" + recordValue.ToString());
                return true;
            }
            else
            {
                return false;
            }
        }
Beispiel #12
0
 public bool ReadMonthlyValues(byte month, ushort year, ref Values values)
 {
     return ReadFromFlash512K(1, month, Convert.ToUInt16(year), ref values);
 }
Beispiel #13
0
        public bool ReadCurrentValues(ushort param, ushort tarif, ref float recordValue)
        {
            List<byte> sellectedType = new List<byte>(1);
            sellectedType.Add((byte)param);
            SetTypesForRead(sellectedType);

            Values vals = new Values();
            vals.listRV = new List<RecordValue>();
            ReadCurrentValues(ref vals);

            if (vals.listRV.Count == 1)
            {
                recordValue = (float)vals.listRV[0].value;
                WriteToLog("Получено ТЕКУЩЕЕ значение:" + recordValue.ToString());
                return true;
            }
            else
            {
                return false;
            }
        }
Beispiel #14
0
        public bool ReadMonthlyValues(byte month, ushort year, ref Values values)
        {
            values = new Values();
            values.listRV = new List<RecordValue>();
            if ((m_dt.Year < year) || (m_dt.Year == year && m_dt.Month < month))
            {
                WriteToLog("ReadMonthlyValues datetime=" + m_dt.ToString() + "; month=" + month.ToString() + "; year=" + year.ToString());
                return false;
            }

            byte[] command = { 0x32, (byte)(month - 1) };
            byte[] answer = new byte[23];
            ulong total = 0;

            /*
            CMD = 32h
            ADDR-CMD-ii3-CRC ( формат ответа ADDR-CMD-count*4-CRC)
            ii3   = 1 byte  = 0…Bh          Младшая тетрада - месяц  0h…Bh
                                            (0 -январь … Bh - декабрь)
            Count = 4 bytes = 0…99999999h
                                            4-х байтовое значение
                                            потребленной эл.энергии
                                            в десятках Вт.ч
                                            Старшие разряды вперёд.
            */

            if (!SendCommand(command, ref answer, 2, 23))
                return false;

            for (int t = 0; t < m_listTypesForRead.Count; t++)
            {
                RecordValue recordValue;
                recordValue.type = m_listTypesForRead[t];
                recordValue.fine_state = true;
                recordValue.value = 0;

                for (int i = 0; i < 4; i++)
                {
                    if (answer[5 + i + (m_listTypesForRead[t] - 1) * 4] == 0xFF)
                    {
                        recordValue.fine_state = false;
                        break;
                    }
                }

                if (recordValue.fine_state)
                {
                    total = Convert.ToUInt32(CommonMeters.DEC2HEX(answer[5 + (m_listTypesForRead[t] - 1) * 4])) * 1000000 +
                            Convert.ToUInt32(CommonMeters.DEC2HEX(answer[6 + (m_listTypesForRead[t] - 1) * 4])) * 10000 +
                            Convert.ToUInt32(CommonMeters.DEC2HEX(answer[7 + (m_listTypesForRead[t] - 1) * 4])) * 100 +
                            Convert.ToUInt32(CommonMeters.DEC2HEX(answer[8 + (m_listTypesForRead[t] - 1) * 4]));

                    recordValue.value = Convert.ToSingle(total) / 100;// т.к. передаётся в десятках ватт
                }

                values.listRV.Add(recordValue);
            }

            return true;
        }
Beispiel #15
0
        public bool ReadCurrentValues(ushort param, ushort tarif, ref float recordValue)
        {
            List<byte> types = new List<byte>();
            types.Add((byte)(param & 0x000000FF));
            ClearTypesForRead();
            SetTypesForRead(types);
            Values vals = new Values();
            if (ReadCurrentValues(ref vals))
            {
                if (vals.listRV.Count == 1)
                {
                    RecordValue rv = vals.listRV[vals.listRV.Count - 1];
                    recordValue = (float)rv.value;
                    return true;
                }
            }
            WriteToLog("ReadCurrentValues: невозможно считать текущее значение для параметра " + param.ToString());
            return false;

            /*
            SetTypesForRead(GetTypesForCategory(CommonCategory.Current));
            Values vals = new Values();
            vals.listRV = new List<RecordValue>();
            if (ReadCurrentValues(ref vals))
            {

                return true;
            }

            return false;*/
        }
Beispiel #16
0
        public bool ReadMonthlyValues(DateTime dt, ushort param, ushort tarif, ref float recordValue)
        {
            Values tmpValues = new Values();
            if (!ReadMonthlyValues((byte)dt.Month, (ushort)dt.Year, ref tmpValues))
                return false;

            List<RecordValue> listRv = tmpValues.listRV;
            if (listRv == null || listRv.Count == 0) return false;
            if (listRv.Count > 4) return false;

            int listRvCount = listRv.Count;

            switch (tarif)
            {
                case 1:
                    {
                        if (listRv.Count == 0) return false;
                        recordValue = (float)listRv[0].value;
                        break;
                    }
                case 2:
                    {
                        if (listRv.Count < 2) return false;
                        recordValue = (float)listRv[1].value;
                        break;
                    }
                case 3:
                    {
                        if (listRv.Count < 3) return false;
                        recordValue = (float)listRv[2].value;
                        break;
                    }
                case 4:
                    {
                        if (listRv.Count < 4) return false;
                        recordValue = (float)listRv[3].value;
                        break;
                    }
                case 0:
                    {
                        for (int i = 0; i < listRv.Count; i++)
                        {
                            recordValue += (float)listRv[i].value;
                        }
                        break;
                    }
                default: return false;
            }

            return true;
        }
Beispiel #17
0
        /*
        public bool ReadCurrentValues(ref Values values)
        {
            byte[] cmnd = new byte[32];
            byte[] answer = new byte[RCURRENT_ANSW_SIZE];

            bool[] already_read_tarif = new bool[4] { false, false, false, false };

            for (int t = 0; t < m_types_for_read_expense.Count; t++)
            {
                byte tarif = 0;
                switch (m_types_for_read_expense[t])
                {
                    case (byte)TypesValues.Tarif1AP:
                    case (byte)TypesValues.Tarif1AM:
                    case (byte)TypesValues.Tarif1RP:
                    case (byte)TypesValues.Tarif1RM:
                        tarif = 1;
                        break;
                    case (byte)TypesValues.Tarif2AP:
                    case (byte)TypesValues.Tarif2AM:
                    case (byte)TypesValues.Tarif2RP:
                    case (byte)TypesValues.Tarif2RM:
                        tarif = 2;
                        break;
                    case (byte)TypesValues.Tarif3AP:
                    case (byte)TypesValues.Tarif3AM:
                    case (byte)TypesValues.Tarif3RP:
                    case (byte)TypesValues.Tarif3RM:
                        tarif = 3;
                        break;
                    case (byte)TypesValues.Tarif4AP:
                    case (byte)TypesValues.Tarif4AM:
                    case (byte)TypesValues.Tarif4RP:
                    case (byte)TypesValues.Tarif4RM:
                        tarif = 4;
                        break;
                    default:
                        continue;
                }

                if (already_read_tarif[tarif - 1] == true)
                    continue;

                already_read_tarif[tarif - 1] = true;

                byte[] command = new byte[] { 0x05, 0x0 };

                command.CopyTo(cmnd, 0);
                cmnd[2] = tarif;

                if (!SendCommand(cmnd, ref answer, 3, RCURRENT_ANSW_SIZE))
                    continue;

                for (byte i = 0; i < m_energy; i++)
                {
                    if (m_types_for_read_expense.Contains(Convert.ToByte(tarif + i)))
                    {
                        RecordValue recordValue;
                        recordValue.fine_state = false;
                        recordValue.value = 0;
                        recordValue.type = Convert.ToByte(tarif + i);

                        byte[] buff = new byte[4];
                        buff[0] = answer[1 + 3 + i * 4];
                        buff[1] = answer[1 + 2 + i * 4];
                        buff[2] = answer[1 + 1 + i * 4];
                        buff[3] = answer[1 + 0 + i * 4];

                        if (buff[0] != 0xFF & buff[1] != 0xFF & buff[2] != 0xFF & buff[3] != 0xFF)
                        {
                            recordValue.fine_state = true;
                            recordValue.value = Convert.ToSingle(BitConverter.ToUInt32(buff, 0)) / (2f * (float)m_gear_ratio);//1000f;//Вт -> кВт
                        }

                        values.listRV.Add(recordValue);
                    }
                }
            }

           // PowerQualityParams(ref values);

            return true;
        }
         *
         * */
        public bool ReadMonthlyValues(byte month, ushort year, ref Values values)
        {
            if ((m_dt.Year < year) || (m_dt.Year == year && m_dt.Month < month))
            {
                return false;
            }

            byte[] cmnd = new byte[32];
            byte[] answer = new byte[RMONTHLY_ANSW_SIZE_BASE + m_energy * 4];

            bool[] already_read_tarif = new bool[4] { false, false, false, false };

            for (int t = 0; t < m_types_for_read_expense.Count; t++)
            {
                byte tarif = 0;
                switch (m_types_for_read_expense[t])
                {
                    case (byte)TypesValues.Tarif1AP:
                    case (byte)TypesValues.Tarif1AM:
                    case (byte)TypesValues.Tarif1RP:
                    case (byte)TypesValues.Tarif1RM:
                        tarif = 1;
                        break;
                    case (byte)TypesValues.Tarif2AP:
                    case (byte)TypesValues.Tarif2AM:
                    case (byte)TypesValues.Tarif2RP:
                    case (byte)TypesValues.Tarif2RM:
                        tarif = 2;
                        break;
                    case (byte)TypesValues.Tarif3AP:
                    case (byte)TypesValues.Tarif3AM:
                    case (byte)TypesValues.Tarif3RP:
                    case (byte)TypesValues.Tarif3RM:
                        tarif = 3;
                        break;
                    case (byte)TypesValues.Tarif4AP:
                    case (byte)TypesValues.Tarif4AM:
                    case (byte)TypesValues.Tarif4RP:
                    case (byte)TypesValues.Tarif4RM:
                        tarif = 4;
                        break;
                    default:
                        continue;
                }

                if (already_read_tarif[tarif - 1] == true)
                    continue;

                already_read_tarif[tarif - 1] = true;

                byte[] command = new byte[] { 0x0A, 0x83 };

                command.CopyTo(cmnd, 0);
                cmnd[2] = month;
                cmnd[3] = tarif;
                cmnd[4] = 0xF;//A+;A-;R+;R-
                cmnd[5] = 0;

                if (!SendCommand(cmnd, ref answer, 6, RMONTHLY_ANSW_SIZE_BASE + m_energy * 4))
                    continue;

                for (byte i = 0; i < m_energy; i++)
                {
                    if (m_types_for_read_expense.Contains(Convert.ToByte(tarif + i))) // + вид энергии: 0-A+;1-A-;2-R+;3-R-
                    {
                        RecordValue recordValue;
                        recordValue.fine_state = false;
                        recordValue.value = 0;
                        recordValue.type = Convert.ToByte(tarif + i);

                        byte[] buff = new byte[4];
                        buff[0] = answer[1 + 3 + i * 4];
                        buff[1] = answer[1 + 2 + i * 4];
                        buff[2] = answer[1 + 1 + i * 4];
                        buff[3] = answer[1 + 0 + i * 4];

                        if (buff[0] != 0xFF & buff[1] != 0xFF & buff[2] != 0xFF & buff[3] != 0xFF)
                        {
                            recordValue.fine_state = true;
                            recordValue.value = Convert.ToSingle(BitConverter.ToUInt32(buff, 0)) / (2f * (float)m_gear_ratio);//1000f;//Вт -> кВт
                        }

                        values.listRV.Add(recordValue);
                    }
                }
            }

            return true;
        }