Python client to control XBee RS232 Adapter and sending AT commands

Hi All,

I am relatively new to the Digi Drop In Network.

I am trying to write python client which obtains regular readings from the device connected to the XBee RS-232 Adapter. Or even sending some AT commands (in ASCII) to the device over the RS232.
I want to run the client on the ConnectPort X4 gateway and communicate using this gateway (and client application on the gateway) and API. However, I find difficulties to send AT commands to the XBee node and get any information from the device connected to this node.

Could anybody point me into some source examples or documentation?

Thanks in advance, Chris

Since you were asking about running python on a CP-X4 gateway, I moved your post from the ZNet 2.5 forum to this one about python scripts.

The Digi Python mediawiki has code examples and demo apps to do a number of things. You can find that here:

Also of relevance would be the ZNet 2.5 Product manual:

So there’s a couple things to get you started.

Before I posted my topic I was looking on these two places but didn’t come up with something fruitful.
Still not sure how can I connect to the node using gateway. What I was using are two commands: ddo_set_param and ddo_get_param. But don’t know how can I switch to the AT commands mode using Zigbee module.

You cannot use AT commands to the X4’s XBee, as it is forced to always be in API-mode. Remember, your program does not own the mesh - it must share it with other gateway functions.

You will need to use the UDP Socket-like function to send you serial messages to remote devices. See this Wiki page:

These socket-like functions manage the API-mode messages for you.

Lynn, thanks very much for your response. I finally managed to talk to the device connected to XBee RS232. I used the socket-like functions, and it work perfectly fine.

The only problem I was facing was proper configuration of XBee RS232, mainly the baud rate, parity, data bits etc. The only problem I found was that I couldn’t configure it through the Web UI, I had to literally go to python code and do it using xbee module. The reason for that is that the RS232 is in sleeping mode, don’t know yet how to turn off the sleeping mode.

Anyway, thanks very much for your help.

Message was edited by: chris.r

I think you want to avoid the term “API” unless you mean the Digi API or you just confuse people.

So assume the remote serial device has a simple protocol with “request-A” and “response-A”, and perhaps a few other responses. If they are actual “AT” commands, that is irrelevant as the CPX4 will just send as data and the fact ATxx is involved is transparent.

You would load the standard “Route AT” firmware in the remote XBee RS-232, and make sure you set the baud rate (etc) correctly. There is a new Wiki page on this here:

At this point, if you send the string “Hello” or “ATDT” or “x23d\r” into that UDP/ZB Socket with the correct MAC extended address, then the remote XBee would just send that string out the serial port. Very simple and easy (I do this all the time with Modbus protocol).

Since the remote serial device cannot “command” the XBee, it just sends a serial response. What happens inside the XBee depends on which hardware it is.

Worst case you need to MANUALLY have preloaded the CPX4’s 8-byte MAC as the DH/DL settings in the remote XBee - this causes the XBee to always send any serial data back to the MAC (which is your CPX4) and you would see the data returned in the recvfrom()

In the Zigbee XBee, it naturally returns unknown serial traffic to the “coordinator” or MAC 00:00:00:00:00:00:00:00. But in summary, I would suggest these steps:

  1. Follow the advice on the above wiki page - confirm the RS-232 adapter shows up correctly in the XBee List of the CPX4.
  2. confirm the serial settings are correct (today the XBee won’t support 2-stop bits or 7 data bits, but 8-data bits and parity can be set.)
  3. set the CPX4’s MAC address as the DH/DL in the Advanced Settings Page for the XBee RS-232 Adapter (just to be safe).

At this point the Python use of UDP/ZB Socket should work. If not, try putting a PC running Hyperterminal out there, see if you can talk to yourself via CPX4+Python -> Xbee -> RS-232 -> Hyperterminal.

Appologies for using term ‘API’, and thanks for pointing this out.

Before I get your response, I managed to configure XBee RS-232 following the wiki page you sent. However, as per my previous msg, I wasn’t able to configure the XBee using Web UI, although I can see CPX4, XBee RS-232 and Xbee Analog I/O. So I had to configure it using python and ddo_set_param().
I also tested the hyperterminal before you suggested :wink:
Thanks again for your help and advices.

I undestand now that to use WEB UI for configuring Serial I need to use ‘Route AT-mode’ firmware. Thanks.

Message was edited by: chris.r

I am glad the problem was solved by “easier” instead of “more complex/harder”.

There is nothing wrong with using set_ddo/get_ddo.

In fact, all of my own Python code starts with a list of settings like [ (‘BD’, 3), (‘D7’, 1) ], then during startup I have a simple loop to do a GET_DDO. If the parameter is NOT set as I wish, then I add it to a second list of things to change.

Then if I am lucky, the second “to_set” list is empty when I am done, otherwise I must do the set_ddo, plus save the setting and reset the XBee.

This is the robust way to do this because it allows you to add a new device without preconfiguring it.