# Can an XBee in API mode receive data through serial?

**URL:** <https://forums.digi.com/t/can-an-xbee-in-api-mode-receive-data-through-serial/16714>\
**Category:** XBee & XBee Gateway\
**Tags:** api-mode, at, xbee\
**Created:** [May 4, 2016, 4:20pm UTC](https://forums.digi.com/t/can-an-xbee-in-api-mode-receive-data-through-serial/16714 "2016-05-04T16:20:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lilyhack](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@lilyhack](https://forums.digi.com/u/lilyhack)\
**Post date:** [May 4, 2016, 4:20pm UTC](https://forums.digi.com/t/can-an-xbee-in-api-mode-receive-data-through-serial/16714/1 "2016-05-04T16:20:34Z")

</div>

Following the python-Xbee library I am trying to communicate between 2 XBees

Using XBee S2C  
Setup:  
Coordinator API=2, running the code in python @ Mac  
Router AT mode, running python code @ Mac

Below data sending code runs in API mode at coordinator

#!/usr/bin/python

#this code runs on the xbee coordinator that is set to API mode 2

import serial  
from xbee import ZigBee  
from xbee.helpers.dispatch import Dispatch  
import time

PORT = ‘/dev/tty.usbserial-A104IC2U’  
BAUD\_RATE = 9600

UNKNOWN = ‘\xff\xfe’  
WHERE = ‘\x00\x13\xA2\x00\x40\xF7\x0A\x50’  
dataString='Hello  
’

# Open serial port

ser = serial.Serial(PORT, BAUD\_RATE)

zb = ZigBee(ser)

#sends data to xbee address  
def sendData(address, datatosend):  
zb.send(‘tx’, dest\_addr\_long = address, dest\_addr = UNKNOWN, data = datatosend)

#test data sending method  
while True:  
try:  
sendData(WHERE, dataString)  
except KeyboardInterrupt:  
break

zb.halt()  
ser.close()

And in the router (AT mode) I run below code

#!/usr/bin/python

import serial

#router port  
PORT = ‘/dev/tty.usbserial-A104IAUX’  
BAUD\_RATE = 9600  
ser = serial.Serial(PORT, BAUD\_RATE)

#myaddress - its the router  
ack=‘0013A20040F70A50’

while True:  
incoming = ser.readline().strip()  
if incoming != ‘0013A20040F70A50’:  
print ‘%s’ % incoming  
ser.write(‘%s’ % ack)

ser.close()

This works fine and I see the output ‘Hello’ on the router side python terminal at Mac. Now I also need to receive data in the coordinator side. So I do this in the coordinator code - after sending, it also reads the serial and prints data

while True:  
try:  
sendData(WHERE, dataString)  
incoming = ser.readline().strip() #added to receive serial data at coordinator  
print ‘%s’ % incoming  
except KeyboardInterrupt:  
break

But as soon as I add those two lines, the communication stops. Is this because coordinator is in API mode and can’t receive in serial? And I have to write the receive code for API mode? Any suggestion?

---

<div class="post-metadata">

**Author:** ![mvut](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/mvut/32/31_2.png) [@mvut](https://forums.digi.com/u/mvut)\
**Post date:** [May 9, 2016, 11:15am UTC](https://forums.digi.com/t/can-an-xbee-in-api-mode-receive-data-through-serial/16714/2 "2016-05-09T11:15:23Z")

</div>

API mode is Serial. What you need to do is to consider putting your read function in a while loop.

---

<div class="post-metadata">

**Author:** ![sams99](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@sams99](https://forums.digi.com/u/sams99)\
**Post date:** [December 29, 2016, 10:12am UTC](https://forums.digi.com/t/can-an-xbee-in-api-mode-receive-data-through-serial/16714/3 "2016-12-29T10:12:52Z")

</div>

sorry… i want to ask…  
how to print data ony without print the acknowledgment between xbee s2 in API mode???  
please help me,

how about the sketch…

thx very much
