#ifndef __MODBUS_H__
#define __MODBUS_H__


/* 调用库 */
#include "usart.h"
#include "sram.h"
#include "crc.h"
#include <string.h>
#include <stdlib.h>

/* 宏定义 */
#define READ_COIL_CODE             0x01
#define WRUTE_MUL_COIL_CODE        0x0f
#define READ_REGUSTER_CODE         0x03
#define READ_EXREGUSTER_CODE       0x30
#define WRITE_COIL_CODE            0x05
#define WRITE_MUL_REGISTER_CODE    0x10
#define WRITE_MUL_EXREGISTER_CODE  0x31
#define GET_HISTORY_CODE           0x55
#define READ_ODD_REGUSTER_CODE     0x33
#define WRITE_ONLY_REGISTER        0x06
#define PLSR_COUNT_CLEAR           0x39

#define SLAVES_ADDRES              0x01


/* 函数声明 */
//基础Modbus
void ErrorBack(uint8_t buffer[], uint16_t longth);
void ProcessModbusFrame(uint8_t buffer[], uint16_t longth);
void ReadCoilProcess (uint8_t buffer[], uint16_t longth);
void WriteCoilProcess(uint8_t buffer[], uint16_t longth);
void WriteMulCoilProcess(uint8_t buffer[], uint16_t longth);
void ReadRegisterProcess(uint8_t buffer[], uint16_t longth);
void WriteOnlyRegister(uint8_t buffer[], uint16_t longth);
void WriteMulRegisterProcess(uint8_t buffer[], uint16_t longth);


//附加Modbus
void SaveHistory(uint8_t buffer[], uint16_t longth);
void LoadHistory(void);
void GetHistory(uint8_t buffer[], uint16_t longth);
void ReadOddRegisterProcess(uint8_t buffer[], uint16_t longth);
void ReadEXRegisterProcess(uint8_t buffer[], uint16_t longth);
void WriteEXRegisterProcess(uint8_t buffer[], uint16_t longth);

//掉电保持
void ModbusLoadSRAM(void);
void ModbusSaveSRAM(void);

//PLSR
void PLSRCountClear(void);

/* 变量定义 */
extern uint8_t History[3][512]; /*存放历史的地方*/
extern uint8_t HistoryLongth[3];/*历史数据大小的计数*/
extern uint8_t HistoryLocation; /*当前历史存放到了第几条*/
extern uint8_t Flash_data[20];  /*存入flash的数据*/
extern uint8_t SaveFlag;

extern uint8_t Register_H[16384];      ///<寄存器的高字节
extern uint8_t Register_L[16384];      ///<寄存器的低字节

#endif