/*
Copyright (c) 2006 QingDao EastSoft.Co.,LTD
All rights reserved
Create date: 2008,12,08
Create by: liuyi
Changed by:
Changed Date:
*/
#include "string.h"
#include "config.h"
#include "driver.h"
#include "user_def.h"
#include "lcd.h"
#include "protocol_hlj.h"
static uint16 last_command = 0;
/* EEPROM数据读取 */
static uint8 e2rom_read(struct dataitem_info *self, struct frame97645 *pframe)
{
read_eeprom(pframe->data + sizeof(self->dataitem), self->address, self->datalen);
return(self->datalen);
}
/* RAM数据读取 */
static uint8 ram_read(struct dataitem_info *self, struct frame97645 *pframe)
{
memcpy(pframe->data + sizeof(self->dataitem), (uint8 *)self->address, self->datalen);
return(self->datalen);
}
/* 当前电量读取 */
static uint8 current_consume_read(struct dataitem_info *self, struct frame97645 *pframe)
{
get_rate_consume(self->address, pframe->data + sizeof(self->dataitem));
return(self->datalen);
}
/* 日,月冻结电量读取 */
static uint8 freeze_consume_read(struct dataitem_info *self, struct frame97645 *pframe)
{
uint16 address = 0;
address = map_e2rom(self->address);
read_eeprom(pframe->data + sizeof(self->dataitem), address, self->datalen);
return(self->datalen);
}
/* 小时冻结电量读取 */
static uint8 hour_freeze_consume_read(struct dataitem_info *self, struct frame97645 *pframe)
{
uint16 address = 0;
address = map_e2rom(self->address);
if (((address - e2rom_addr(hour_freeze_consume)) >> 2) == (MAX_HOUR_FREEZE - 1))
{
read_eeprom(pframe->data + sizeof(self->dataitem), address, 4);
read_eeprom(pframe->data + sizeof(self->dataitem) + 4, e2rom_addr(hour_freeze_consume), 4);
}
else
{
read_eeprom(pframe->data + sizeof(self->dataitem), address, self->datalen);
}
return(self->datalen);
}
/* 重要参数修改标志字读取 */
static uint8 parameter_modified_read(struct dataitem_info *self, struct frame97645 *pframe)
{
read_eeprom(pframe->data + sizeof(self->dataitem), e2rom_addr(parameter_modified_flag), e2rom_len(parameter_modified_flag));
read_eeprom(pframe->data + sizeof(self->dataitem) + e2rom_len(parameter_modified_flag), e2rom_addr(last_set_improtant_parameter), e2rom_len(last_set_improtant_parameter));
return(self->datalen);
}
/* 日期读取 */
static uint8 date_read(struct dataitem_info *self, struct frame97645 *pframe)
{
memcpy(pframe->data + sizeof(self->dataitem), sys_time + 6, 1);
memcpy(pframe->data + sizeof(self->dataitem) + 1, sys_time + _dd, 3);
return(self->datalen);
}
/* 单相表主时段读取 */
#if 0
static uint8 major_periodtable_read(struct dataitem_info *self, struct frame97645 *pframe)
{
uint8 len = 0, tmpbuf[0x20];
memset(tmpbuf, 0x00, sizeof(tmpbuf));
read_eeprom(tmpbuf, self->address, self->datalen);
for (len = 0; len < self->datalen; len += 3)
{
if ((tmpbuf[len] < 1) || (tmpbuf[len] > 4))
{
break;
}
}
if (len == 0)
{
len = 24;
}
memcpy(pframe->data + sizeof(self->dataitem), tmpbuf, len);
return(len);
}
#endif
/* 备用时段及备用时段切换时间读取 */
static uint8 periodtable_switchtime_read(struct dataitem_info *self, struct frame97645 *pframe)
{
read_eeprom(pframe->data + sizeof(self->dataitem), e2rom_addr(minor_period_table), e2rom_len(minor_period_table));
read_eeprom(pframe->data + sizeof(self->dataitem) + e2rom_len(minor_period_table), e2rom_addr(period_table_switch_time), e2rom_len(period_table_switch_time));
return(self->datalen);
}
/* 功率读取 */
static uint8 power_read(struct dataitem_info *self, struct frame97645 *pframe)
{
get_power(pframe->data + sizeof(self->dataitem));
return(self->datalen);
}
/* 获取电表运行状态字 */
uint8 get_running_status(void)
{
union meter_running_status running;
running.running_byte = 0;
running.running_bits.meter_cover_open = system_status.system_bits.meter_cover_open;
running.running_bits.current_reverse = system_status.system_bits.current_reverse;
running.running_bits.batt_status = system_status.system_bits.batt_status;
running.running_bits.overcharge = system_status.system_bits.overcharge;//system_status_bits
return(running.running_byte);
}
/* 电表运行状态字读取 */
static uint8 running_status_read(struct dataitem_info *self, struct frame97645 *pframe)
{
uint8 running_status = 0;
running_status = get_running_status();
memcpy(pframe->data + sizeof(self->dataitem), &running_status, self->datalen);
return(self->datalen);
}
/* 获取电表运行错误字 */
uint8 get_running_error_status(void)
{
union meter_running_error_status running_error;
running_error.running_error_byte = 0;
running_error.running_error_bits.steal_consume_flag = system_status.system_bits.consume_steal_flag;
running_error.running_error_bits.relay_status = system_status.system_bits.relay_status;
running_error.running_error_bits.current_reverse = system_status.system_bits.current_reverse;
running_error.running_error_bits.batt_status = system_status.system_bits.batt_status;
running_error.running_error_bits.exception_flag = system_status.system_bits.exception_flag;
running_error.running_error_bits.clock_status = system_status.system_bits.clock_status;
running_error.running_error_bits.relay_error_flag = system_status.system_bits.relay_error_flag;
return(running_error.running_error_byte);
}
/* 电表运行错误字读取 */
static uint8 running_error_status_read(struct dataitem_info *self, struct frame97645 *pframe)
{
uint8 running_error_status = 0;
running_error_status = get_running_error_status();
memcpy(pframe->data + sizeof(self->dataitem), &running_error_status, self->datalen);
return(self->datalen);
}
/* 以下是打包命令的读取 */
static uint8 block1_read(struct dataitem_info *self, struct frame97645 *pframe)
{
get_rate_consume(CONSUME_BLOCK, pframe->data + sizeof(self->dataitem)); //当前电能示值数据块
*(uint8 *)(pframe->data + sizeof(self->dataitem) + self->datalen - 2) = get_running_error_status();
read_eeprom(pframe->data + sizeof(self->dataitem) + self->datalen - 1, e2rom_addr(plcamr_status), 1);
return(self->datalen);
}
static uint8 block2_read(struct dataitem_info *self, struct frame97645 *pframe)
{
get_rate_consume(CONSUME_BLOCK, pframe->data + sizeof(self->dataitem)); //当前电能示值数据块
*(uint8 *)(pframe->data + sizeof(self->dataitem) + self->datalen - 1) = get_running_error_status();
return(self->datalen);
}
static uint8 block3_read(struct dataitem_info *self, struct frame97645 *pframe)
{
uint16 address = 0;
get_rate_consume(CONSUME_TOTAL, pframe->data + sizeof(self->dataitem)); //当前总电能示值
address = map_e2rom(e2rom_addr(day_freeze_consume[0]) + 0 * 4); //日冻结总电能示值
read_eeprom(pframe->data + sizeof(self->dataitem) + 4, address, 4);
*(uint8 *)(pframe->data + sizeof(self->dataitem) + self->datalen - 2) = get_running_error_status();
read_eeprom(pframe->data + sizeof(self->dataitem) + self->datalen - 1, e2rom_addr(plcamr_status), 1);
return(self->datalen);
}
static uint8 block4_read(struct dataitem_info *self, struct frame97645 *pframe)
{
uint16 address = 0;
address = map_e2rom(e2rom_addr(day_freeze_consume[0]) + 0 * 4); //日冻结总电能示值
read_eeprom(pframe->data + sizeof(self->dataitem), address, 4);
address = map_e2rom(e2rom_addr(day_freeze_consume[0]) + 2 * 4); //日冻结峰,平,谷电能示值
read_eeprom(pframe->data + sizeof(self->dataitem) + 4, address, 12);
*(uint8 *)(pframe->data + sizeof(self->dataitem) + self->datalen - 2) = get_running_error_status();
read_eeprom(pframe->data +