64 lines
1.5 KiB
C
64 lines
1.5 KiB
C
#ifndef _COMMUNICATION_PROTOCOL_H_
|
|
#define _COMMUNICATION_PROTOCOL_H_
|
|
|
|
#include "user_lib.h"
|
|
|
|
#define COM_ERROR_CODE_SIZE 7 // 通信错误返回帧长度
|
|
#define COM_AI_DATA_SIZE 11 // 模拟量数据返回帧长度
|
|
#define FRAME_HEAD 0xAA // 帧头
|
|
#define FRAME_TAIL 0x3C // 帧尾
|
|
#define DEVICE_NUM 0x00 // 设备号
|
|
#define READ_ANALOG_CMD 0x00 // 读模拟量命令
|
|
#define WRITE_ANALOG_CMD 0x01 // 写模拟量命令
|
|
#define READ_DIGITAL_CMD 0x02 // 读数字量命令
|
|
#define WRITE_DIGITAL_CMD 0x03 // 写数字量命令
|
|
#define SEND_STATE_CMD 0x04 // 主动上发状态命令
|
|
|
|
typedef enum
|
|
{
|
|
COM_OK = 0,
|
|
FRAMING_ERROR,
|
|
CHECK_ERROR,
|
|
COMMAND_ERROR,
|
|
DEVICE_ERROR,
|
|
} communication_error_e;
|
|
#pragma pack(1)
|
|
typedef struct
|
|
{
|
|
uint8_t start_addr;
|
|
uint8_t num;
|
|
uint8_t data[16];
|
|
} communication_do_t;
|
|
|
|
typedef struct
|
|
{
|
|
uint8_t start_addr;
|
|
uint8_t num;
|
|
} communication_di_t;
|
|
|
|
typedef struct
|
|
{
|
|
uint8_t channel;
|
|
float32_u data;
|
|
} communication_ao_t;
|
|
|
|
typedef struct
|
|
{
|
|
uint8_t channel;
|
|
} communication_ai_t;
|
|
|
|
typedef union
|
|
{
|
|
uint8_t data[50];
|
|
communication_do_t do_data;
|
|
communication_di_t di_data;
|
|
communication_ao_t ao_data;
|
|
communication_ai_t ai_data;
|
|
} communication_data_u;
|
|
#pragma pack()
|
|
|
|
void communication_exception(uint8_t *tx_data, const uint8_t *const rx_data, communication_error_e error_code);
|
|
void communication_get_ai(communication_ai_t *ai_data, uint8_t *tx_data, const uint8_t *const rx_data);
|
|
void communication_set_ao(communication_ao_t *ao_data);
|
|
#endif
|