Lab 06 Xbee API Communications 實驗
What is API mode An application programming interface (API) is simply a set of standard interfaces created to allow one software program to interact with another. APIs let one computer application request services from another application in a standard manner. The most important thing to note is that APIs are specifically engineered to enable computers to talk efficiently to other computers. They are not generally designed for direct human interaction. The interactions are made easy for humans, but they are not as robust, explicit, and efficient for computers. Computers prefer to deal with numbers, and do best when the organization of these numbers provides an unambiguous and highly structured method for transfer. 2
XBee API Protocol The goal of API-mode communications is to transmit highly structured data quickly, predictably, and reliably. We will begin by taking a look at the structures shared by all API data frames and work our way into the specifics for each frame type. The tiny microcontroller inside the Series 2 XBee radio doesn t have enough room to hold all the instructions for both AT mode (transparent/command modes) and API mode. Therefore, different firmware must be loaded into the xbee device. 3
API Frame Types Inside the general frame structure there are substructures that cover all the different kinds of data that you might want to send to and receive from your local XBee radio. The frame type byte tells us what type of API frame we are looking at. Knowing the frame type is crucial to knowing what information is coming next. For example, if the frame type is 0x08, that indicates it is an AT command frame. So by reading the first four bytes we will know: Where the frame begins (start byte) How long the frame is going to be (length bytes) What kind of frame we re looking at (frame type) 4
API Frame Types Every frame type is assigned a number, following table lists the basic ones, including all the frame types. 5
AT Commands AT-type commands can be sent via API frames to configure your local radio. They can query the settings on the local radio or set parameters. These are all the same commands you typed in transparent/command mode. AT command frames begin with our old buddy the start byte: 0x7E (see next slide). First two bytes that indicate the frame length. There s also a checksum at the end. The data that makes the AT command frame unique goes in the Frame-specific section, starting right after the length byte. 6
API format for AT commands 7
ZigBee Receive Packet Here s another API frame that gives us far more than we could get from simple trans-parent/command mode interactions. When a transmission is received in transparent mode, it comes with no indication of who the sender was. On a simple pair network that s fine because there s only one possible sender. But on a larger network, it s usually of considerable interest to know not only what was received but where it came from. So in addition to the usual preamble bytes, including the frame type of 0x90 to indicate a ZigBee Receive Packet and a frame ID that was sent by the transmitter, we get to see the 64-bit and 16-bit source addresses along with a receive options indicator, and of course the payload data itself, followed by a checksum. 8
ZigBee Receive Packet 9
ZigBee Receive Packet 10
API Mode Parameter Range, 有 0-2 的數值可以更改,0 = Disabled, 1 = API Enabled, 2 = API w/escaped 發現你的另外一顆 xbee 傳過來的訊息不是正常的字串, 這是因為 XBee 已經進入 API 傳輸模式 11
Hex Mode 看到右邊的奇怪符號, 打開 Show Hex 按鈕顯示 16 進制模式 12
API Type ASCII 碼, 順序是 7E 00 06 81 00 1F 31 00 61 CD 參考 datasheet 的 API 格式說明, 對照我們得到的是封包內容去解讀 13
Packets Format 7E 是開頭, 然後接著的 00 06 代表後面會有 6 個 bytes 的封包內容, 然後 81 是 cmdid, 然後剩下 00 1F 31 00 61 CD 這些我們要先去看 cmdid = 81 的格式是什麼,RX packet 16-bit address (page 54 0x90) 14
checksum 00 1F 就是 source Address, 也就是說傳給我們那一顆 Xbee 的 MY, MSB 是高位元 LSB 是低位元, 然後 31 是 RSSI 是傳輸信號強度,00 是保留用 0 = 沒作用, 1 = 廣播模式, 2 PAN 廣播, 最後的 61 才是我們的字元 a (16bit) 那最後 CD 的意思是什麼呢?? 也就是 Checksum,Checksum 的算法很簡單, 就是扣除開頭的 7E 以及長度還有 checksum 全部加起來的總和取 LSB 的 byte 去減掉 0xFF 看是否是等於 CD, 也就是說 : 7E 00 06 81 00 1F 31 00 61 CD 取 81 00 1F 31 00 61 加起來 0xFF - (81+0+1F+31+0+61) = 0xFF - 0x132 = 0xFF - 0x32 = 0xCD 15
checksum 把所有的 byte 加起來之後不包括 7E 跟長度應該 LSB 等於 0xFF, 也就是說 : 7E 00 06 81 00 1F 31 00 61 CD = 81 + 1F + 31 + 61 + CD = 0x1FF 16