/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-04-23     lijian       the first version
 */
#ifndef APPLICATIONS_MODBUS_MODBUS_RTU_H_
#define APPLICATIONS_MODBUS_MODBUS_RTU_H_


#include <rtthread.h>
#include <drv_common.h>
#include <rtdevice.h>

#include <rtthread.h>
#include <rtdevice.h>
#include <stdint.h>


#include "rs485.h"

//int rs485_init(void);
//int rs485_send(uint8_t *buf, int len);
//int rs485_receive(uint8_t *buf, int bufsz, int timeout, int bytes_timeout);


#include "agile_modbus.h"
#include "agile_modbus_rtu.h"
#include <rs485.h>

typedef struct
{
    rt_uint16_t modbus_addr;//从机地址
    rt_uint16_t modbus_reg_addr;//寄存器地址
    rt_uint16_t modbus_reg_num;//寄存器数量
    rt_uint8_t modbus_func;//功能码
    rt_uint8_t modbus_rec; //接收成功
    rt_uint8_t log; //log
    rt_uint16_t hold_register[10];
} Modbus_RTU_t;                    //

//设置modbus从机地址
void set_Modbus_Addr(rt_uint16_t modbus_addr);

//获取Modbus从机地址
rt_uint16_t get_Modbus_Addr();

//设置modbus寄存器地址
void set_Modbus_Reg_Addr(rt_uint16_t modbus_reg_addr);

//获取Modbus读取寄存器起始地址
rt_uint16_t get_Modbus_Reg_Addr();

/**
 * @brief 设置Modbus读寄存器数量
 * @return rt_uint16
 */
void set_Modbus_Reg_Num(rt_uint16_t modbus_reg_num);

/**
 * @brief 获取Modbus读寄存器数量
 * @return rt_uint16
 */
rt_uint16_t get_Modbus_Reg_Num();

void set_modbus_log(rt_uint8_t log);

//modbus初始化
void Modbus_init();

//获取modbus是否接收成功
rt_uint16_t get_modbus_rec();

#endif /* APPLICATIONS_MODBUS_MODBUS_RTU_H_ */
