116 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			116 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| 
								 | 
							
								/****************************************************************************
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
							 | 
						||
| 
								 | 
							
								be copied by any method or incorporated into another program without
							 | 
						||
| 
								 | 
							
								the express written consent of Aerospace C.Power. This Information or any portion
							 | 
						||
| 
								 | 
							
								thereof remains the property of Aerospace C.Power. The Information contained herein
							 | 
						||
| 
								 | 
							
								is believed to be accurate and Aerospace C.Power assumes no responsibility or
							 | 
						||
| 
								 | 
							
								liability for its use in any way and conveys no license or title under
							 | 
						||
| 
								 | 
							
								any patent or copyright and makes no representation or warranty that this
							 | 
						||
| 
								 | 
							
								Information is free from patent or copyright infringement.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								****************************************************************************/
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifndef IOT_PROTO_MODBUS_H
							 | 
						||
| 
								 | 
							
								#define IOT_PROTO_MODBUS_H
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "iot_proto_common.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#if PLC_SUPPORT_STA_ROLE
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								extern "C" {
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma pack(push)  // save the pack status
							 | 
						||
| 
								 | 
							
								#pragma pack(1)     // 1 byte align
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define HALF_NO                               0
							 | 
						||
| 
								 | 
							
								#define HALF_YES                              1
							 | 
						||
| 
								 | 
							
								#define OVERFLOW                              2
							 | 
						||
| 
								 | 
							
								#define LEFT_8BIT                             256
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_01_READ_COIL_STATUS        0x01
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_02_READ_INPUT_STATUS       0x02
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_03_READ_HOLD_REG           0x03
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_04_READ_INPUT_REG          0x04
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_05_ENFORCE_COIL            0x05
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_06_PRESET_REG              0x06
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_0F_ENFORCE_MULTIPLE_COIL   0x0F
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_10_PRESET_MULTIPLE_REG     0x10
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_LEN_NO_DATA                6
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_01_04_ACK_LEN              5
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_0F_10_ACK_LEN              9
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_01_04_ACK_LEN_NO_CRC       3
							 | 
						||
| 
								 | 
							
								#define MODBUS_CMD_0F_10_ACK_LEN_NO_CRC       7
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define MODBUS_MIN_LEN                        5
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define MATCH                                 1
							 | 
						||
| 
								 | 
							
								#define NO_MATCH                              0
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* value of resv set to 1 means modbus frame */
							 | 
						||
| 
								 | 
							
								#define MODBUS_RESV0_RESV                     0x01
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef struct _modbus_head_01_10 {
							 | 
						||
| 
								 | 
							
								    uint8_t addr;
							 | 
						||
| 
								 | 
							
								    uint8_t cmd;
							 | 
						||
| 
								 | 
							
								    /* these bytes are useful, but not be used in our fuction */
							 | 
						||
| 
								 | 
							
								    uint8_t no_use[4];
							 | 
						||
| 
								 | 
							
								    uint8_t crc_l;
							 | 
						||
| 
								 | 
							
								    uint8_t crc_h;
							 | 
						||
| 
								 | 
							
								} modbus_head_01_to_10;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef struct _modbus_head_01_04_with_len {
							 | 
						||
| 
								 | 
							
								    uint8_t addr;
							 | 
						||
| 
								 | 
							
								    uint8_t cmd;
							 | 
						||
| 
								 | 
							
								    uint8_t len;
							 | 
						||
| 
								 | 
							
								    uint8_t data[0];
							 | 
						||
| 
								 | 
							
								} modbus_head_01_04_with_len;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef struct _modbus_head_0f_10_with_len {
							 | 
						||
| 
								 | 
							
								    uint8_t addr;
							 | 
						||
| 
								 | 
							
								    uint8_t cmd;
							 | 
						||
| 
								 | 
							
								    uint8_t addr_l;
							 | 
						||
| 
								 | 
							
								    uint8_t addr_h;
							 | 
						||
| 
								 | 
							
								    uint8_t reg_num_l;
							 | 
						||
| 
								 | 
							
								    uint8_t reg_num_h;
							 | 
						||
| 
								 | 
							
								    uint8_t data_len;
							 | 
						||
| 
								 | 
							
								    uint8_t data[0];
							 | 
						||
| 
								 | 
							
								} modbus_head_0f_10_with_len;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief iot_proto_modbus_format_check() - To pickup modbus frames from a
							 | 
						||
| 
								 | 
							
								                                            buffer
							 | 
						||
| 
								 | 
							
								 * @param recv_data:   point to the buffer data
							 | 
						||
| 
								 | 
							
								 * @param pdata:       point to the array which will reseive the ge frames
							 | 
						||
| 
								 | 
							
								 * @param ret_size:    point to the data number of a frames
							 | 
						||
| 
								 | 
							
								 * @ge_len_out:        point to the ge length if it exist
							 | 
						||
| 
								 | 
							
								 * @param retval:      how many frames
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint8_t iot_proto_modbus_format_check(mcu_data_handle_t *recv_data,
							 | 
						||
| 
								 | 
							
								    uint8_t *pdata, uint16_t *ret_size, uint16_t *ge_len);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief iot_proto_modbus_frm_format_check()  check the data is modbus frame
							 | 
						||
| 
								 | 
							
								 *                                             or not.
							 | 
						||
| 
								 | 
							
								 * @param data:                  point to the data
							 | 
						||
| 
								 | 
							
								 * @param data:                  len of the data
							 | 
						||
| 
								 | 
							
								 * @param retval:                0:modbus frame,others:not modbus frame
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint8_t iot_proto_modbus_frm_format_check(uint8_t *data, uint16_t len);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma pack(pop)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif /* PLC_SUPPORT_STA_ROLE */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif /* end IOT_PROTO_MODBUS_H */
							 |