How can I close the serial port of xbee cellular

Hi there, I’m trying to do a project which need the xbee reading data from another MCU. Basically is sending command using xbee and receive the feedback from the MCU. The feedback returned from the MCU start with ‘\x01’
Here is the code I’m using:

sys.stdout.write(L_E)
l = 0
while True:
l +=1
if sys.stdin.read() != ‘\x01’:
time.sleep_ms(1)
else:
time.sleep_ms(2)
break
link1 = sys.stdin.read(2)
time.sleep_ms(10)
link2 = sys.stdin.read(5)
est_link = [link1, link2]
sys.stdout.close()

#----------------read p--------------
time.sleep_ms(50)
sys.stdout.write(command_p)

a = 0
while True:
a +=1
if sys.stdin.read() != ‘\x01’:
time.sleep_ms(1)
else:
time.sleep_ms(13)
break
p_data = sys.stdin.read(13)
time.sleep_ms(10)
p_crc = sys.stdin.read(5)
p = [p_data,p_crc]

the code works fine until the second while loop. I can get the ‘est_link’ correctly but the code seems stopped when after entering the second while loop. I use ‘l’ and ‘a’ to count the loops number, and I got “l = 80”, “a = 1”. And the item “p_data”,“p_crc”,“p” are all not defined after running the code.
From the response of the MCU, it seems like the xbee sent some unknown message to MCU and cause an interrupt of MCU. I can’t figure out where those message came from since I only sent two commands through the UART.
I’m wondering is there any method allow me to close the serial port after sending the command or can anyone tell me where is wrong about my code? Thanks.

Johnny,

The XBee is basically nothing more than a cable in this case. So in order to close the COM port, you would have it close it on your processor and not the radio.

Same goes for the data. Unless you are in API mode, the radio is not going to provide anything out of its UART unless the data was provided from the receiving side of the socket.

Digi Support