ZigBee协议栈串口应用.ppt

上传人:sh****n 文档编号:6581212 上传时间:2020-02-29 格式:PPT 页数:32 大小:1.38MB
返回 下载 相关 举报
ZigBee协议栈串口应用.ppt_第1页
第1页 / 共32页
ZigBee协议栈串口应用.ppt_第2页
第2页 / 共32页
ZigBee协议栈串口应用.ppt_第3页
第3页 / 共32页
点击查看更多>>
资源描述
ZigBee串口应用 一 串口收发基础实验二 ZigBee协议栈串口应用扩展实验 实验课题 串口数据收发基础实验实验目的 利用串口收发数据试验步骤 ZigBee协议栈中串口通信的配置使用一个结构体来实现 该结构体为hal UARTCfg t 不必关心该结构体的具体定义形式 只需要对其功能有个了解 该结构体将串口初始化的参数集合在一起 只需要初始化各个参数即可最后使用HalUARTOpen 函数对串口进行初始化 该函数将halUARTCfg t类型的结构体变量作为相关参数回调函数 通过指针 函数地址 调用的函数 如果把函数的地址作为参数传递给另一个函数 当通过这个指针调用它所指向的函数时 称为函数的回调 一 串口收发基础实验 使用串口的基本步骤 初始化串口 包括设置波特率 中断等 向发送缓冲区发送数据或者从接收缓冲区读取数据 串口工作原理剖析 在ZigBee协议栈中 halUARTCfg t结构体是如何定义的 串口是如何初始化的 发送给串口的数据时如何接受的 串口是如何向PC机发送数据的 这些问题涉及如下三个函数 ZigBee协议栈中提供的与串口操作有关的三个函数为 可在zmain c和hal uart c中查看相关函数 uint8HalUARTOpen uint8halUARTCfg t config uint16HalUARTRead uint8port uint8 buf uint16len Uint16HalUARTWrite uint8prot uint buf uint16len Coordinator c include OSAL h include AF h include ZDApp h include ZDObject h include ZDProfile h include include Coordinator h include DebugTrace h if defined WIN32 include OnBoard h endif include hal lcd h include hal led h include hal key h include hal uart h 包含头文件 constcId tGenericApp ClusterList GENERICAPP MAX CLUSTERS GENERICAPP CLUSTERID constSimpleDescriptionFormat tGenericApp SimpleDesc GENERICAPP ENDPOINT GENERICAPP PROFID GENERICAPP DEVICEID GENERICAPP DEVICE VERSION GENERICAPP FLAGS GENERICAPP MAX CLUSTERS cId t GenericApp ClusterList 0 cId t NULL endPointDesc tGenericApp epDesc byteGenericApp TaskID byteGenericApp TransID typedefstruct byteendPoint byte task id SimpleDescriptionFormat t simpleDesc afNetworkLatencyReq tlatencyReq endPointDesc t voidGenericApp MessageMSGCB afIncomingMSGPacket t pckt voidGenericApp SendTheMessage void UINT16GenericApp ProcessEvent bytetask id UINT16events staticvoidrxCB uint8port uint8event 任务初始化函数voidGenericApp Init bytetask id halUARTCfg tuartConfig GenericApp TaskID task id GenericApp TransID 0 GenericApp epDesc endPoint GENERICAPP ENDPOINT GenericApp epDesc task id 串口初始化 事件处理函数UINT16GenericApp ProcessEvent bytetask id UINT16events return0 本实验没有进行事件处理 staticvoidrxCB uint8port uint8event unsignedcharuartbuf 128 HalUARTRead 0 uartbuf 16 调用函数 从串口读取数据并将其放在uartbuf数组中if osal memcmp uartbuf 16 使用osal memcmp 判断接收到的数据是否是 返回TRUE 执行HalUARTWrite 函数 HalUARTWrite 0 uartbuf 16 调用函数将接收到的字符输出到串口 ZigBee协议栈使用条件编译 在GenericApp Coordinator工程上右键单击 options C C Compiler在Definedsymbols 下拉框中添加HAL UART TRUE 点击OK即可 二 ZigBee协议栈串口应用扩展实验 实验课题 ZigBee协议栈串口应用扩展实验实验目的 利用串口收发数据实验原理 协调器建立ZigBee无线网络 终端节点自动加入该网络中 然后终端节点周期性地向协调器发送字符串 EndDevice 协调器收到该字符串后 通过串口将其输出到用户PC机 Coordinator c include OSAL h include AF h include ZDApp h include ZDObject h include ZDProfile h include include Coordinator h include DebugTrace h if defined WIN32 include OnBoard h endif include hal lcd h include hal led h include hal key h include hal uart h constcId tGenericApp ClusterList GENERICAPP MAX CLUSTERS GENERICAPP CLUSTERID constSimpleDescriptionFormat tGenericApp SimpleDesc GENERICAPP ENDPOINT GENERICAPP PROFID GENERICAPP DEVICEID GENERICAPP DEVICE VERSION GENERICAPP FLAGS GENERICAPP MAX CLUSTERS cId t GenericApp ClusterList 0 cId t NULL endPointDesc tGenericApp epDesc byteGenericApp TaskID byteGenericApp TransID unsignedcharuartbuf 128 将这一行注释掉 typedefstruct byteendPoint byte task id SimpleDescriptionFormat t simpleDesc afNetworkLatencyReq tlatencyReq endPointDesc t voidGenericApp MessageMSGCB afIncomingMSGPacket t pckt voidGenericApp SendTheMessage void staticvoidrxCB uint8port uint8event 将这一行注释掉 任务初始化函数voidGenericApp Init bytetask id halUARTCfg tuartConfig GenericApp TaskID task id GenericApp TransID 0 GenericApp epDesc endPoint GENERICAPP ENDPOINT GenericApp epDesc task id 串口初始化 事件处理函数UINT16GenericApp ProcessEvent bytetask id UINT16events afIncomingMSGPacket t MSGpkt if events osal msg deallocate uint8 MSGpkt MSGpkt afIncomingMSGPacket t osal msg receive GenericApp TaskID return events SYS EVENT MSG return0 当协调器收到终端节点发送来的数据后 首先使用osal msg receive 函数 从消息队列接收到消息 然后调用GenericApp MessageMSGCB 函数中将接收到的数据通过串口发送给PC机 voidGenericApp MessageMSGCB afIncomingMSGPacket t pkt unsignedcharbuffer 10 switch pkt clusterId caseGENERICAPP CLUSTERID osal memcpy buffer pkt cmd Data 10 HalUARTWrite 0 buffer 10 break 使用osal memcpy 函数 将接收到的数据拷贝到buffer数组中 然后就可以将该数据通过串口发送给PC机 EndDevice cOsal start timerEx 函数原型如下 调用uint8osal start timerEx uint8taskID uint16event id uint16timeout value 周期性发送 任务 事件 时间 include OSAL h include AF h include ZDApp h include ZDObject h include ZDProfile h include include Coordinator h 使用该头文件的宏定义 include DebugTrace h if defined WIN32 include OnBoard h endif include hal lcd h include hal led h include hal key h include hal uart h defineSEND DATA EVENT0 x01constcId tGenericApp ClusterList GENERICAPP MAX CLUSTERS GENERICAPP CLUSTERID 简单设备描述符结构体常量constSimpleDescriptionFormat tGenericApp SimpleDesc GENERICAPP ENDPOINT GENERICAPP PROFID GENERICAPP DEVICEID GENERICAPP DEVICE VERSION GENERICAPP FLAGS 0 cId t NULL GENERICAPP MAX CLUSTERS cId t GenericApp ClusterList 初始化端口描述符endPointDesc tGenericApp epDesc 简单节点描述符byteGenericApp TaskID 任务优先级byteGenericApp TransID 数据发送序列号devStates tGenericApp NwkState 节点状态保存变量枚举型记录设备状态 定义结构体 typedefstruct byteendPoint byte task id SimpleDescriptionFormat t simpleDesc afNetworkLatencyReq tlatencyReq endPointDesc t 声明函数voidGenericApp MessageMSGCB afIncomingMSGPacket t pckt voidGenericApp SendTheMessage void int8readTemp void 定义任务初始化函数voidGenericApp Init bytetask id GenericApp TaskID task id GenericApp TransID 0 累加数据发送序列号 可用于计算丢包率GenericApp NwkState DEV INIT 设备状态初始化为DEV INIT表示该节点没有连接到ZigBee网络GenericApp epDesc endPoint GENERICAPP ENDPOINT GenericApp epDesc task id 节点描述符注册 以下是件处理函数UINT16GenericApp ProcessEvent bytetask id UINT16events afIncomingMSGPacket t MSGpkt if eventsif GenericApp NwkState DEV END DEVICE 对节点设备类型进行判断是否是终端节点DEV END DEVICE osal set event GenericApp TaskID SEND DATA EVENT break default break 如果事件SEND DATA EVENT发生 则events if events 定义数据发送函数voidGenericApp SendTheMessage void unsignedchartheMessageData 10 EndDevice 定义数组存放要发送的数据afAddrType tmy DstAddr my DstAddr addrMode afAddrMode t Addr16Bit 发送地址模式为单播my DstAddr endPoint GENERICAPP ENDPOINT 初始化目的端口号my DstAddr addr shortAddr 0 x0000 在ZigBee网络中 协调器的网络地址是固定的 向协调器发送数据时可以直接指定协调器的网络地址 调用数据请求发送函数AF DataRequest 讲述结束 谢谢
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!