
#ifndef __MODBUS_RTU_H__
#define __MODBUS_RTU_H__

#include "msystem.h"

#include "com_ttySx.h"


#define  MODBUS_FC_READ_COILS                     0x01
#define  MODBUS_FC_READ_DISCRETE_INPUTS           0x02
#define  MODBUS_FC_READ_HOLDING_REGISTERS         0x03
#define  MODBUS_FC_READ_INPUT_REGISTERS           0x04
#define  MODBUS_FC_WRITE_SINGLE_COIL              0x05
#define  MODBUS_FC_WRITE_SINGLE_REGISTER          0x06
#define  MODBUS_FC_READ_EXCEPTION_STATUS          0x07
#define  MODBUS_FC_DIAGNOSTICS                    0x08

#define  MODBUS_FC_GET_COMM_EVENT_COUNTER         0x0B
#define  MODBUS_FC_GET_COMM_EVENT_LOG             0x0C

#define  MODBUS_FC_WRITE_MULTIPLE_COILS           0x0F
#define  MODBUS_FC_WRITE_MULTIPLE_REGISTERS       0x10
#define  MODBUS_FC_REPORT_SLAVE_ID                0x11

#define  MODBUS_FC_READ_FILE_RECORD               0x14
#define  MODBUS_FC_WRITE_FILE_RECORD              0x15
#define  MODBUS_FC_MASK_WRITE_REGISTER            0x16
#define  MODBUS_FC_READ_WRITE_REGISTERS           0x17
#define  MODBUS_FC_READ_FIFO_QUEUE                0x18

int modbus_RTU_write_GYDCG_register(com_ttySx_t* tty, uint8_t slave, uint16_t addr, uint16_t value);
uint16_t modbus_crc16(uint8_t *buffer, int length);
int modbus_RTU_read_coils(com_ttySx_t* tty, uint8_t slave, uint16_t addr, uint16_t nb, uint8_t* value);
int modbus_RTU_read_discrete_inputs(com_ttySx_t* tty, uint8_t slave, uint16_t addr, uint16_t nb, uint8_t* value);
int modbus_RTU_read_holding_registers(com_ttySx_t* tty, uint8_t slave, uint16_t addr, uint16_t nb, uint16_t* value);
int modbus_RTU_read_input_registers(com_ttySx_t* tty, uint8_t slave, uint16_t addr, uint16_t nb, uint16_t* value);
int modbus_RTU_write_single_coil(com_ttySx_t* tty, uint8_t slave, uint16_t addr, uint16_t value);
int modbus_RTU_write_single_register(com_ttySx_t* tty, uint8_t slave, uint16_t addr, uint16_t value);
int modbus_RTU_read_exception_status(com_ttySx_t* tty, uint8_t slave, uint8_t* value);
int modbus_RTU_write_multiple_coils(com_ttySx_t* tty, uint8_t slave, uint16_t addr, uint16_t nb, uint8_t* value);
int modbus_RTU_write_multiple_registers(com_ttySx_t* tty, uint8_t slave, uint16_t addr, uint16_t nb, uint16_t* value);
int modbus_RTU_report_slave_id(com_ttySx_t* tty, uint8_t slave, uint8_t* slave_id);
int modbus_RTU_mask_write_register(com_ttySx_t* tty, uint8_t slave, uint16_t addr, uint16_t and_mask, uint16_t or_mask);
int modbus_RTU_read_write_registers(com_ttySx_t* tty, uint8_t slave, uint16_t read_addr, uint16_t read_nb, uint16_t write_addr, uint16_t write_nb, uint16_t* value);

#endif//__MODBUS_RTU_H__
