XBee API Code for BS2
StephenMoore
Posts: 188
I am interested in any API code that has been written for the Stamp. I have written my own API comm program and would appreciate comments!
It will talk to any Xbee connected to a serial terminal on a PC that is talking in API mode (subset of full API definition) and I have a C source code terminal program if any body is interested.
I am sure there are lot's better ways to do this and I would like to improve my programs.
It will talk to any Xbee connected to a serial terminal on a PC that is talking in API mode (subset of full API definition) and I have a C source code terminal program if any body is interested.
I am sure there are lot's better ways to do this and I would like to improve my programs.
' {$STAMP BS2px, XbeeTX.bpx, XbeeRX.bpx} ' {$PBASIC 2.5} ' -----[ Program Description ]--------------------------------------------- ' ' This program is a Stamp terminal for a Xbee ZB radio with SIP adapter ' written by S.G. Moore 6/5/2011 ' '============================================================================== '----------------------- XBee definitions ------------------------- 20011-05-27 '============================================================================== ' -----[ XBee I/O Definitions ]------------------------------------------------- XBeeRX PIN 0 XBeeTX PIN 1 XBeeRTS PIN 2 ' -----[ Xbee Constants ]------------------------------------------------------- DemoAPI CON $0 TxAPI CON $1 RxAPI CON $2 argBuff CON 120 TxOK CON $1 RxOK CON $2 NoRx CON $3 TxBad CON $4 RxBad CON $5 Baud CON 396 ' 9600, 8, N ' -----[ Xbee Variables ]------------------------------------------------------- char VAR Byte idx VAR Byte ' -----[ Initialization ]-------------------------------------------------- CONFIGPIN DIRECTION, %0000000000000110 HIGH XbeeTX GET argBuff, char SELECT char CASE TxOK GOTO transmitComplete CASE RxOK GOTO receiveComplete CASE NoRx DEBUG "." RUN RxAPI CASE TxBad DEBUG CR, "Bad TX.", CR CASE RxBad DEBUG CR, "Bad RX.", CR ENDSELECT ' -----[ Program Code ]---------------------------------------------------- Main: DO DEBUG "Input string.", CR char = 0 idx = 0 DO WHILE char <> $0D DEBUGIN char PUT idx, char idx = idx + 1 LOOP PUT (idx-1), 0 RUN TxAPI transmitComplete: RUN RxAPI receiveComplete: DEBUG CR, "Received string: " idx = 0 GET idx, char IF char = "T" THEN transmitComplete DO WHILE char <> 0 DEBUG char idx = idx + 1 GET idx, char LOOP DEBUG CR LOOP END 'XbeeTx.bpx ' {$STAMP BS2px} ' {$PBASIC 2.5} '============================================================================== '----------------------- XBee definitions ------------------------- 20011-05-27 '============================================================================== ' -----[ XBee I/O Definitions ]------------------------------------------------- PIR1 PIN 0 PIR2 PIN 1 XBeeRX PIN 2 XBeeTX PIN 3 ' -----------------[ Program Messaging Constants ]------------------------------------- myApp CON $0 ' These values are slot ID's TxAPI CON $1 RxAPI CON $2 argBuff CON 120 ' Scratch Pad RAM for passing Xbee messages notInit CON $0 TxOK CON $1 ' These values are passed through argBuff RxOK CON $2 NoRx CON $3 TxBad CON $4 RxBad CON $5 chkTX CON $6 initDone CON $7 Baud CON 396 ' 9600, 8, N '------------------------------------------------------------------------------- '------------------------[ Xbee TX API Header definition ]---------------------- '------------------------------------------------------------------------------- 'TX Data start, lenMSB, lenLSB, type, id, addr7, addr6, addr5, addr4, addr3, addr2, addr1, addr0, net1, net0, radius, options ' note: this address is for my Xstick on my PC, change if necessary TX DATA $7E, $00, $00, $10, $01, $00, $13, $A2, $00, $40, $66, $C4, $F9, $FF, $FE, $00, $00 ' -----[ Xbee Variables ]------------------------------------------------------- idx VAR Byte char VAR Byte mlen VAR Byte csum VAR Byte msgAddr VAR Word '============================== Message Definitions =========================== msg1 DATA "Hello.",0 msg2 DATA "Zone 1 detect.",0 msg3 DATA "Zone 1 is clear.",0 ' ------------------------------[ Program Code ]---------------------------- main: GET argBuff, char SELECT char CASE 0 ' argBuff is set to 0 if myApp has assembled it's own message GOTO messageSetup CASE 1 ' otherwise a pre-recorded message is selected msgAddr = msg1 CASE 2 msgAddr = msg2 CASE 3 msgAddr = msg3 ENDSELECT '-------------------------------[ Message Setup ] --------------------------- idx = 0 ' selects a pre-recorded message from EEPROm DO WHILE char <> 0 READ msgAddr + idx, char PUT idx, char idx = idx + 1 LOOP messageSetup: FOR idx = 0 TO 16 ' assemble API TX packet starting at scratchpad RAM address 50 READ (TX + idx), char ' read message header from EEPROM PUT (50+idx), char ' store Xbee API message starting at RAM 50 NEXT mlen = 0 GET mlen, char ' get first character of message string DO WHILE char <> 0 PUT (67 + mlen), char ' move the message to the TxAPI buffer mlen = mlen + 1 ' count message length in characters GET mlen, char ' get next character of message sring LOOP PUT 52, (mlen + 14) csum = $00 FOR idx = 53 TO (66 + mlen) ' compute check sum (between LSB message length and CSUM) GET idx, char csum = csum + char ' checksum = $FF - sum(between LSB message length and CSUM) NEXT csum = $FF - csum PUT (67 + mlen), csum FOR idx = 50 TO (67 + mlen) GET idx, char SEROUT XbeeTX, Baud, [char] ' transmit the message to the Xbee UART NEXT PUT argBuff, ChkTX RUN RxAPI END 'XbeeRx.bpx ' {$STAMP BS2px} ' {$PBASIC 2.5} '============================================================================== '----------------------- XBee definitions ------------------------- 20011-05-27 '============================================================================== ' -----[ XBee I/O Definitions ]------------------------------------------------- PIR1 PIN 0 PIR2 PIN 1 XBeeRX PIN 2 XBeeTX PIN 3 ' -----------------[ Program Messaging Constants ]------------------------------------- myApp CON $0 ' These values are slot ID's TxAPI CON $1 RxAPI CON $2 argBuff CON 120 ' Scratch Pad RAM for passing Xbee messages notInit CON $0 TxOK CON $1 ' These values are passed through argBuff RxOK CON $2 NoRx CON $3 TxBad CON $4 RxBad CON $5 chkTX CON $6 initDone CON $7 Baud CON 396 ' 9600, 8, N ' -----[ Xbee Variables ]------------------------------------------------------- idx VAR Byte char VAR Byte start VAR Byte lenMSB VAR Byte lenLSB VAR Byte type VAR Byte id VAR Byte add7 VAR Byte add6 VAR Byte add5 VAR Byte add4 VAR Byte add3 VAR Byte add2 VAR Byte add1 VAR Byte add0 VAR Byte net1 VAR Byte net0 VAR Byte retry VAR Byte TXstat VAR Byte disc VAR Byte option VAR Byte csum VAR Byte calc VAR Byte ' -----[ Program Code ]---------------------------------------------------- main: GET argBuff, char IF char = ChkTX THEN TXstatus ' read a message of Frame Type $90 SERIN XbeeRX, Baud, 1000, noMessage, [start, lenMSB,lenLSB, type, add7,add6, add5, add4, add3, add2, add1,add0, net1, net0, option] SERIN XbeeRX, Baud, [SPSTR lenLSB-12, csum] PUT (lenLSB - 12), 0 ' DEBUG CR, "Message start: ", HEX2 start, CR ' DEBUG "Message size MSB: ", HEX2 lenMSB, CR ' DEBUG "Message size LSB: ", HEX2 lenLSB, CR ' DEBUG "Frame type: ", HEX2 type, CR ' DEBUG "Source 64bit ID: ", HEX2 add7, HEX2 add6, HEX2 add5, HEX2 add4, " ", HEX2 add3, HEX2 add2, HEX2 add1, HEX2 add0, CR ' DEBUG "Source net ID: ", HEX2 net1, " ", HEX2 net0, CR ' DEBUG "Packet option: ", HEX2 option, CR ' verify checksum on incoming message calc = type+add7+add6+add5+add4+add3+add2+add1+add0+net1+net0+option idx = 0 DO GET idx, char calc = calc + char idx = idx + 1 LOOP WHILE char <> 0 calc = $FF - calc IF calc = csum THEN PUT argBuff, RxOK ' return to slot 0 with RxOK success code ELSE DEBUG CR, "Check sum error.", CR PUT argBuff, RxBad ENDIF RUN myApp TXstatus: ' read a message of Frame Type $8B SERIN XbeeRX, Baud, [start, lenMSB, lenLSB, type,id, net1, net0, retry, TXstat, disc, csum] IF TXstat <> $00 THEN DEBUG "Transmission error code: ", TXstat, CR PUT argBuff, TxBad ELSE ' DEBUG "Transmission OK.", CR PUT argBuff, TxOK ENDIF RUN myApp noMessage: PUT argBuff, NoRx ' signal program running in slot 0 that RX timeout occurred RUN myApp END
Comments
'
Your code is pretty slick....CRC to boot.
'
I would like to see your C pc interface.
'
I'm working with the XBee-WiFi units(XB24-WF) now.They are a little harder to use than the RovingNetworks RN-171/RN-174 but they seem to be more popular.
'
Great work on the BS2 code
Transmit
Receive
Hi and thanks for looking at my progs. My PC interface is written actually for National Instruments LabWindows but here are the Tx and Rx methods. If you would like the .exe or the full up code you are welcome to it. I am planning on converting it to Microsoft Visual Studio Express since you can get that for free from Microsoft.
The hard part is the Rx method since it has so many possible results.
SM
Which Xbee are you using?
I've got the Series 1 XBee PRO 60mW and XBee PRO 1mW modules.