www.pudn.com > src.rar > lib.h, change:2013-12-17,size:10570b


#ifndef __LIB_H__ 
#define __LIB_H__ 
 
#include <pthread.h> 
#include <sys/time.h> 
#include "list.h" 
#include "iec_103.h" 
 
#define DEV_ISA_347G		"ISA_347G" 
#define DEV_ISA_351G		"ISA_351G" 
#define DEV_ISA_381G		"ISA_381G" 
 
 
 
#define FLAG_RESET_CU		50		/* 复位通信单元标志 */ 
#define FLAG_RESET_FCB		100		/* 复位帧计数位标志 */ 
#define FLAG_POLLING		200		/* 启动总查询的标志 */ 
#define FLAG_READ_MEASURE	300		/* 读遥测标志 */ 
#define FLAG_READ_SET		400		/* 读定值标志 */ 
#define FLAG_READ_SFC		500		/* 读压板标志 */ 
#define FLAG_READ_LIST		600		/* 读描述标志 */ 
#define FLAG_READ_DI		700		/* 读遥信标志 */ 
#define	FLAG_POLLING_END	800		/* 总查询终止*/ 
#define FLAG_POWER_ON		900		/* 启动/重新启动 */ 
#define FLAG_YK_CHK             850             /*遥控选择*/ 
#define FLAG_YK_DO              950             /*遥控执行*/ 
#define FLAG_YK_UNDO            1000            /*遥控撤消*/ 
 
/* 各组的起始点号 */ 
#define EVT_BEGIN	20		/* 事件起始码 */ 
#define ALA_BEGIN	79		/* 告警起始码 */ 
#define DIC_BEGIN	175		/* 遥信起始码 */ 
#define MEA_BEGIN	207		/* 遥测起始码 */ 
#define CTR_BEGIN     230        /*  遥控起始码 */ 
 
//帧格式类型 
#define FRAME_T_VOL	0x68	/* 可变帧长 */ 
#define FRAME_T_CON	0x10	/* 固定帧长 */ 
 
#define TIME_REG_BASE_ADDR              	0x0000 
#define DEV_INFO_BASE_ADDR             		0x0100 
#define ANALOG_REG_BASE_ADDR         	0x0200 
#define ANA_EXT_REG_BASE_ADDR       	0x0300 
#define CONTROL_REG_BASE_ADDR               0x0400 
#define SWITCH_REG_BASE_ADDR                 0x0c00 
#define SWITCH_PARAM_REG_BASE_ADDR    0x0600 
 
#define TIME_REG_NUM                           8 
#define DEV_INFO_NUM                           8 
#define ANALOG_REG_NUM                      24 
#define ANA_EXT_REG_NUM                    8 
#define CONTROL_REG_NUM                    17 
#define SWITCH_REG_NUM                      32 
#define SWITCH_PARAM_REG_NUM         32 
 
#define PRM_OFFSET 6 
#define FCB_OFFSET 5 
#define FCV_OFFSET 4 
#define ACD_OFFSET 5 
#define DFC_OFFSET 4 
 
#define MASTER_FUNNO_RESET_CU      		0 
#define MASTER_FUNNO_RESET_FCB		7 
#define MASTER_FUNNO_CALL_DATA1         10 
#define MASTER_FUNNO_CALL_DATA2         11 
#define SLAVE_FUNNO_CONFIRM          		0 
#define SLAVE_FUNNO_BUSY               		1 
#define SLAVE_FUNNO_NO_DATA          		9 
#define SLAVE_FUNNO_LINK_STATE      		11 
 
#define PRM_VAL(code) (code >> PRM_OFFSET) & 1 
#define FCB_VAL(code) (code >> FCB_OFFSET) & 1 
#define FCV_VAL(code) (code >> FCV_OFFSET) & 1 
#define ACD_VAL(code) (code >> ACD_OFFSET) & 1 
#define DFC_VAL(code) (code >> DFC_OFFSET) & 1 
 
#define FUNNO_VAL(code) code&(0x0f) 
 
#define STABLE_FRAME_CTRL_OFFSET               1 
#define STABLE_FRAME_ADDR_OFFSET			2 
 
#define VARIABLE_FRAME_CTRL_OFFSET		4 
#define VARIABLE_FRAME_ADDR_OFFSET		5 
#define VARIABLE_FRAME_TYPE_OFFSET		6 
#define VARIABLE_FRAME_VSQ_OFFSET			7 
#define VARIABLE_FRAME_COT_OFFSET		8 
#define VARIABLE_FRAME_ASADDR_OFFSET		9 
#define VARIABLE_FRAME_ELEMENT_OFFSET	10 
 
#define MODBUS_ERR_ILLEGAL_FC			01 
#define MODBUS_ERR_ILLEGAL_ADDR		02 
#define MODBUS_ERR_ILLEGAL_VALU		03 
#define MODBUS_ERR_DEV_FAULT			04 
 
typedef unsigned char 		uint8; 
typedef unsigned short		uint16; 
typedef unsigned int		uint32; 
 
void Ack_Changeable(const uint8* rbuf,uint8 byDevAddr,uint8 uart_port); 
void Ack_Staid(const uint8* rbuf,uint8 byDevAddr,uint8 uart_port); 
 
void ASDU05(const uint8 *rbuf,uint8 byDevAddr,uint8 uart_port); 
void ASDU44(const uint8 *rbuf,uint8 byDevAddr,uint8 uart_port); 
void ASDU01(const uint8 *rbuf,uint8 byDevAddr,uint8 uart_port); 
void ASDU06(const uint8 *rbuf,uint8 byDevAddr,uint8 uart_port); 
void ASDU07(const uint8* rbuf,uint8 byDevAddr,uint8 uart_port); 
void ASDU08(const uint8 *rbuf,uint8 byDevAddr,uint8 uart_port); 
void ASDU64(const uint8* rbuf,uint8 byDevAddr,uint8 uart_port); 
void ASDU20(const uint8 *rbuf,uint8 byDevAddr,uint8 uart_port); 
void ASDU88(const uint8 *rbuf,uint8 byDevAddr,uint8 uart_port); 
void ASDU21(const uint8* rbuf,uint8 byDevAddr,uint8 uart_port); 
uint8 Addr_to_ID(uint8 byDevAddr); 
uint8 ID_to_Addr(uint8 dev_id); 
uint8 Get_Fun(uint8 dev_id); 
uint8 Get_Device_Number(); 
uint8 Makecrc(uint8 *str,uint8 nSize); 
 
 
/* 设备类型定义 */ 
#define D_PSL_600	0xb2 
#define D_PSL_640	0xc8 
#define D_PSR_652	0x01 
#define D_PSP_640	0xc9 
#define D_PSC_640	0xca 
 
#define PORT_DEFAULT			              "/dev/tts0" 
#define BAUD_DEFAULT			                   9600 
#define DATA_BITS_DEFAULT		                      8 
#define PARITY_DEFAULT			                      0 
#define STOP_BITS_DEFAULT			               1 
#define RECV_DELAY_DEFAULT      				100 
 
#define READ_COIL_STATUS_FUN               01 
#define READ_INPUT_STATUS_FUN            02 
#define READ_HOLDING_REGISTER_FUN    03 
#define READ_INPUT_REGISTER_FUN         04 
 
#define SET_SINGLE_COIL_FUN                  05 
#define SET_SINGLE_REGISTER_FUN          06 
#define SET_MULTI_COIL_FUN                    15 
#define SET_MULTI_REGISTER_FUN            16 
 
#define MODBUS_ERR_ILLEGAL_FC			01 
#define MODBUS_ERR_ILLEGAL_ADDR		02 
#define MODBUS_ERR_ILLEGAL_VALU		03 
#define MODBUS_ERR_DEV_FAULT			04 
 
typedef struct 
{ 
	struct list_head list; 
	char name[20]; 
	uint8 addr; 
	uint8 *last_frame; 
	uint8 scn; 
	uint8 fcb; 
	uint8 state; 
	uint16 mod_addr; 
	pthread_mutex_t *lock; 
	struct PtlData103 ptl103; 
	int fd; 
}DEVICE; 
 
typedef struct dbl_lnk 
{ 
	struct dbl_lnk *prev; 
	struct dbl_lnk *next; 
}DBL_LNK; 
 
 
typedef struct 
{ 
	DBL_LNK l; 
	int yc_addr; 
	int yx_addr; 
	int funno; 
}PCL_MODADDR; 
 
typedef struct 
{ 
	DBL_LNK l; 
	char *pre; 
	int str; 
	int end; 
	int interval; 
	float factor; 
	char *data_type; 
	//ENTRY_MAP *map; 
}PCL_SADDR; 
 
typedef struct 
{ 
	DBL_LNK l; 
	PCL_SADDR *saddrHead; 
	int addr; 
	int num; 
}PCL_REG_ADDR; 
 
typedef struct 
{ 
	DBL_LNK l; 
	PCL_REG_ADDR *reg_addrHead; 
	int funno; 
}PCL_FUN; 
 
typedef struct 
{ 
	DBL_LNK l; 
	PCL_MODADDR *modaddrHead; 
	int devaddr; 
	char *devname; 
	int yc_addr; 
	int yx_addr; 
	DEVICE *dev; 
	struct timeval tm; 
}PCL_DEV; 
 
typedef struct 
{ 
	DBL_LNK l; 
	PCL_DEV *devHead; 
	int portno; 
	int baud; 
	int data_bits; 
	int parity; 
	int stop_bits; 
	int com_fd; 
	int poll_interval; 
	int frame_timeout; 
	int query_timeout; 
	int synctm_timeout; 
	int modbus_srv; 
}PCL_PORT; 
 
typedef struct 
{ 
	PCL_PORT *portHead; 
}PCL_INFO; 
 
typedef struct 
{ 
	uint8 di_num; 
	uint16 *di_value; 
}T_DI; 
 
typedef struct 
{ 
	uint8 ms_num; 
	uint16 *ms_value; 
}T_MS; 
 
typedef struct 
{ 
	uint16 msec; 
	uint8 minute; 
	uint8 hour; 
	uint8 week; 
	uint8 day; 
	uint8 month; 
	uint16 year; 
}T_DATE; 
 
typedef struct  
{ 
	uint8 dev_id; 
	uint8 service; 
	uint8 dev_type; 
	uint8 nul; 
	uint8 dev_data[6]; 
 
}T_IED; 
 
typedef struct 
{ 
	uint8 addr; 
	uint8 value; 
}shift_info; 
 
 
 
 
T_IED g_ied_list[] = 
{ 
	{1,  0, 200,	0,	{172,20,10,1,1,1}}, 
}; 
 
uint8 g_ied_num =sizeof(g_ied_list)/sizeof(T_IED); 
 
 
static const char *ini_section[] = 
{ 
	"com_setup" 
}; 
 
static const char *ini_key_com[] = 
{ 
	"port_name", 
	"baud_rate", 
	"data_bits", 
	"parity", 
	"stop_bits", 
	"recv_delay" 
}; 
 
 
char week[7][10] = 
{ 
	"Monday", 
	"Tuesday", 
	"Wednesday", 
	"Thursday", 
	"Friday", 
	"Saturday", 
	"Sunday" 
}; 
 
static unsigned char auchCRCHi[] = { 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 
} ; 
 
static char auchCRCLo[] = { 
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 
0x08, 0xC8, 0xD8, 0x18, 0x19, 0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, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 
0x43, 0x83, 0x41, 0x81, 0x80, 0x40 
} ; 
 
static const char *ini_file_name = "./ini/uart_cfg.ini"; 
 
#endif