写在最前面

华清远见教育集团
15年专注高端IT培训
做良心教育,做专业教育,做受人尊敬的职业教育

创客学院官网:http://www.makeru.com.cn/
华清远见创客学院嵌入式课程链接:http://www.makeru.com.cn/emb
华清远见创客学院物联网课程链接:http://www.makeru.com.cn/iot

 

正文

 

为了完成通信双方的交互,要有一种通信协议,将通过数据包的形式进行数据传递。一方将数据进行数据的打包发送,另一方接收数据包进行拆包分析;由于整个项目有两个方向的数据流,即硬件层开始的对上进行数据的上报,用户层开始的对下进行命令的发送。在C语言总可以通过结构体将数据封装。

 

(发送方)发出一定格式的数据包  --->  (接收方)按照相同的格式进行拆包  

 

对下下发对硬件控制的数据流(命令)

命令总长一个字节,不用构造结构体。仓库编号 + 设备编号 + 操作掩码 = 命令
例如:
    0x40 + 0x00 + 0x01 = 0x41   风扇一档
    0x40 + 0x30 + 0x08 = 0x78   数码管显示8
    0x40 + 0x30 + 0x0f = 0x7f   关闭数码管

 

 

对上上报传感器采集的数据流(数据)
环境结构体包含:数据头+数据类型+仓库号+温度+湿度+三轴加速+光度+电量+ADC
(24个字节)

struct sendEnv
{
    uint8_t head[3];//标识位st:
    uint8_t type;       //数据类型
    uint8_t snum;       //仓库编号
    uint8_t temp[2]; //温度
    uint8_t hum[2];  //湿度
    uint8_t x;      //三轴信息
    uint8_t     y;             
    uint8_t     z;             
    uint32_t ill;     //光照
    uint32_t bet;     //电池电量
    uint32_t adc;  //电位器信息
};

RFID商品识别结构体包含:数据头、数据类型、仓库号、I/O、商品编号、商品数量
(8个字节)
struct sendGoods
{
    uint8_t head[3]; //标识位st:
    uint8_t type;        //数据类型
    uint8_t snum;        //仓库编号
    uint8_t io;        //进出仓库
    uint8_t goodsno;  //商品编号
    uint8_t goodsnum; //商品数量
};


typedef struct storage_info
{
    char index[2];
    unsigned char storage_status;     // Storage No.
    unsigned char led_status;          // Device status.
    unsigned char buzzer_status;  // Device status.
    unsigned char key_status;      // Device status.
    signed char x;                 // A value detected by Sensor MPU6050.
    signed char y;                 // A value detected by Sensor MPU6050.
    signed char z;                 // A value detected by Sensor MPU6050.
    float temperature;                 // The current temperature of curtain reposity.
    float temperatureMIN;             // The lowest limit to temperature for curtain reposity.
    float temperatureMAX;             // The highest limit to temperature for curtain reposity.
    float humidity;             // The current humidity of curtain reposity.
    float humidityMIN;             // The lowest limit to humidity for curtain reposity.
    float humidityMAX;             // The highest limit to humidity for curtain reposity.
    float illumination;             // The strength of lightnss.
    float illuminationMIN;                 // The lowest limit to strength of lightnss for curtain reposity.
    float illuminationMAX;                 // The highest limit to strength of lightnss for curtain reposity.
    struct storage_goods_info goods_info[GOODS_NUM];     // Goods info for all types of goods.
} __attribute__((packed)) REPO_ENV;


这里使用 __attribute__ 是为了 将来要定义的结构体变量中的成员进行内存对齐。如果要使用编译器默认的对齐规则的话,会造成内存的浪费,故而这里使用内存对齐的方法。