#pragma once #include #include "data/Data.h" /* * 数据帧格式定义,工厂模式实现 */ #define FRAME_DIRECT_REQ (0x00) // 请求 #define FRAME_DIRECT_RSP (0x01) // 响应 #define FRAME_CMD_START (0x00) #define FRAME_CMD_SET_TIME (0x01) // 设置时间 #define FRAME_CMD_GET_TIME (0x02) // 获取时间 #define FRAME_CMD_SET_SERIAL_NUMBER (0x03) // 设置出厂编号 #define FRAME_CMD_GET_SERIAL_NUMBER (0x04) // 获取出厂编号 #define FRAME_CMD_SET_DEVICE_NAME (0x05) // 设置设备名称 #define FRAME_CMD_GET_DEVICE_NAME (0x06) // 获取设备名称 #define FRAME_CMD_SET_DEVICE_NUMBER (0x07) // 设置设备编号 #define FRAME_CMD_GET_DEVICE_NUMBER (0x08) // 获取设备编号 #define FRAME_CMD_SET_LOCK_STATUS (0x09) // 设置锁定状态 #define FRAME_CMD_SET_PASSWORD (0x0A) // 设置密码 #define FRAME_CMD_SET_USAGE_COUNT (0x0B) // 设置使用次数 #define FRAME_CMD_GET_RECORD (0x0C) // 获取文件记录 #define FRAME_CMD_CLS_RECORD (0x0D) // 清除文件记录 #define FRAME_CMD_SET_PARAM_FALLBACK (0x0E) // 设置回退参数 #define FRAME_CMD_SET_OPERATOR (0x0F) // 设置操作人名 #define FRAME_CMD_GET_OPERATOR (0x10) // 获取操作人名 #define FRAME_CMD_SET_PARAM_TORQUE (0x11) // 设置扭矩模式参数 #define FRAME_CMD_SET_PARAM_BOLT (0x12) // 设置螺栓模式参数 #define FRAME_CMD_SET_PARAM_ANGLE (0x13) // 设置角度模式参数 #define FRAME_CMD_SET_PARAM_GEAR (0x14) // 设置挡位预设参数 #define FRAME_CMD_SET_PARAM_SENSOR (0x15) // 设置传感器参数 #define FRAME_CMD_MAX (0x16) #define FRAME_TYPE(DIRECT, CMD) (((unsigned char)(DIRECT) << 7 & 0x80) | ((unsigned char)(CMD) & 0x7F)) #define FRAME_TYPE_SET_TIME_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_TIME) #define FRAME_TYPE_SET_TIME_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_TIME) #define FRAME_TYPE_GET_TIME_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_GET_TIME) #define FRAME_TYPE_GET_TIME_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_GET_TIME) #define FRAME_TYPE_SET_SERIAL_NUMBER_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_SERIAL_NUMBER) #define FRAME_TYPE_SET_SERIAL_NUMBER_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_SERIAL_NUMBER) #define FRAME_TYPE_GET_SERIAL_NUMBER_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_GET_SERIAL_NUMBER) #define FRAME_TYPE_GET_SERIAL_NUMBER_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_GET_SERIAL_NUMBER) #define FRAME_TYPE_SET_DEVICE_NAME_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_DEVICE_NAME) #define FRAME_TYPE_SET_DEVICE_NAME_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_DEVICE_NAME) #define FRAME_TYPE_GET_DEVICE_NAME_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_GET_DEVICE_NAME) #define FRAME_TYPE_GET_DEVICE_NAME_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_GET_DEVICE_NAME) #define FRAME_TYPE_SET_DEVICE_NUMBER_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_DEVICE_NUMBER) #define FRAME_TYPE_SET_DEVICE_NUMBER_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_DEVICE_NUMBER) #define FRAME_TYPE_GET_DEVICE_NUMBER_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_GET_DEVICE_NUMBER) #define FRAME_TYPE_GET_DEVICE_NUMBER_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_GET_DEVICE_NUMBER) #define FRAME_TYPE_SET_LOCK_STATUS_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_LOCK_STATUS) #define FRAME_TYPE_SET_LOCK_STATUS_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_LOCK_STATUS) #define FRAME_TYPE_SET_PASSWORD_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_PASSWORD) #define FRAME_TYPE_SET_PASSWORD_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_PASSWORD) #define FRAME_TYPE_SET_USAGE_COUNT_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_USAGE_COUNT) #define FRAME_TYPE_SET_USAGE_COUNT_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_USAGE_COUNT) #define FRAME_TYPE_GET_RECORD_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_GET_RECORD) #define FRAME_TYPE_GET_RECORD_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_GET_RECORD) #define FRAME_TYPE_CLS_RECORD_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_CLS_RECORD) #define FRAME_TYPE_CLS_RECORD_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_CLS_RECORD) #define FRAME_TYPE_SET_PARAM_FALLBACK_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_PARAM_FALLBACK) #define FRAME_TYPE_SET_PARAM_FALLBACK_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_PARAM_FALLBACK) #define FRAME_TYPE_SET_OPERATOR_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_OPERATOR) #define FRAME_TYPE_SET_OPERATOR_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_OPERATOR) #define FRAME_TYPE_SET_PARAM_TORQUE_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_PARAM_TORQUE) #define FRAME_TYPE_SET_PARAM_TORQUE_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_PARAM_TORQUE) #define FRAME_TYPE_SET_PARAM_BOLT_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_PARAM_BOLT) #define FRAME_TYPE_SET_PARAM_BOLT_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_PARAM_BOLT) #define FRAME_TYPE_SET_PARAM_ANGLE_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_PARAM_ANGLE) #define FRAME_TYPE_SET_PARAM_ANGLE_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_PARAM_ANGLE) #define FRAME_TYPE_SET_PARAM_GEAR_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_PARAM_GEAR) #define FRAME_TYPE_SET_PARAM_GEAR_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_PARAM_GEAR) #define FRAME_TYPE_SET_PARAM_SENSOR_REQ FRAME_TYPE(FRAME_DIRECT_REQ, FRAME_CMD_SET_PARAM_SENSOR) #define FRAME_TYPE_SET_PARAM_SENSOR_RSP FRAME_TYPE(FRAME_DIRECT_RSP, FRAME_CMD_SET_PARAM_SENSOR) const unsigned short FRAME_HEAD = 0x5AA5; const unsigned short MAX_DATA_LEN = 10240; static bool isCmdValid(unsigned char cmd) { return cmd > FRAME_CMD_START && cmd < FRAME_CMD_MAX; } static std::string getCmdName(int cmd) { std::string cmdName; switch(cmd) { case FRAME_CMD_SET_TIME : cmdName = "设置时间"; break; case FRAME_CMD_GET_TIME : cmdName = "获取时间"; break; case FRAME_CMD_SET_SERIAL_NUMBER : cmdName = "设置出厂编号"; break; case FRAME_CMD_GET_SERIAL_NUMBER : cmdName = "获取出厂编号"; break; case FRAME_CMD_SET_DEVICE_NAME : cmdName = "设置设备名称"; break; case FRAME_CMD_GET_DEVICE_NAME : cmdName = "获取设备名称"; break; case FRAME_CMD_SET_DEVICE_NUMBER : cmdName = "设置设备编号"; break; case FRAME_CMD_GET_DEVICE_NUMBER : cmdName = "获取设备编号"; break; case FRAME_CMD_SET_LOCK_STATUS : cmdName = "设置锁定状态"; break; case FRAME_CMD_SET_PASSWORD : cmdName = "设置密码"; break; case FRAME_CMD_SET_USAGE_COUNT : cmdName = "设置使用次数"; break; case FRAME_CMD_GET_RECORD : cmdName = "获取文件记录"; break; case FRAME_CMD_CLS_RECORD : cmdName = "清除文件记录"; break; case FRAME_CMD_SET_PARAM_FALLBACK:cmdName = "设置回退参数"; break; case FRAME_CMD_SET_OPERATOR : cmdName = "设置操作人名"; break; case FRAME_CMD_GET_OPERATOR : cmdName = "获取操作人名"; break; case FRAME_CMD_SET_PARAM_TORQUE : cmdName = "设置扭矩模式参数"; break; case FRAME_CMD_SET_PARAM_BOLT : cmdName = "设置螺栓模式参数"; break; case FRAME_CMD_SET_PARAM_ANGLE : cmdName = "设置角度模式参数"; break; case FRAME_CMD_SET_PARAM_GEAR : cmdName = "设置挡位预设参数"; break; case FRAME_CMD_SET_PARAM_SENSOR : cmdName = "设置传感器参数"; break; default : cmdName = "未知的命令"; break; } return cmdName; } class Frame { public: Frame(); Frame(int cmd, int direct); virtual ~Frame(); void updateDataType(); void updateLen(); void setHead(unsigned short head) { m_head = head; } void setAddr(unsigned short addr) { m_addr = addr; } void setCmd(unsigned char cmd) { m_cmd = cmd; } void setLen(unsigned short len) { m_len = len; } void setCrc(unsigned short crc) { m_crc = crc; } //void setHasData(bool hasData) { m_hasData = hasData; } void setDataType(int dataType) { m_dataType = dataType; } void setData(Data *data); void setDirect(int direct) { m_direct = direct; } unsigned short getHead() { return m_head; } unsigned short getAddr() { return m_addr; } unsigned char getCmd() { return m_cmd; } unsigned short getLen() { return m_len; } unsigned short getCrc() { return m_crc; } Data *getData() { return m_data; } //bool getHasData() { return m_hasData; } int getDataType() { return m_dataType; } int getDirect() { return m_direct; } // 帧整体转换为字符串 std::string toStr(); // 帧整体转换为Byte字符串 -- 格式固定,所以在基类中实现 std::string toByteStr(); // 帧整体转换为十六进制字符串 -- 格式固定,所以在基类中实现 std::string toHexStr(); // 从Byte字符串解析 bool fromByteStr(std::string str); private: void createData(); void destroyData(); private: unsigned short m_head; // 协议头 unsigned short m_addr; // 地址 unsigned char m_cmd; // 命令 unsigned short m_len; // 长度 unsigned short m_crc; // 数据区各不相同,在具体的类中实现 //bool m_hasData; // 是否存在数据 int m_dataType; // 数据区类型 Data *m_data; // 数据 int m_direct; // 数据发送方向,0下发:PC->开发板,1上发:开发板->PC };