xbee - serial port problem in UBUNTU

I’m trying to write a bash script that automatically detects xbee radios attached to a PC running Ubuntu via a usb adaptor

The radio shows up as /dev/ttyUSB[0…n].

Everything works fine with X-CTU under Wine and minicom.

Under minicom :

+++OK

AT

OK

From a terminal :

$ echo -n ‘+++’ > /dev/ttyUSB0 ; RESPONSE=dd if=/dev/ttyUSB0 count=2 ; echo $RESPONSE

0+2 records in

0+1 records out

2 bytes (2 B) copied, 0.977698 s, 0.0 kB/s

OK

$ echo ‘AT’ > /dev/ttyUSB0 ; RESPONSE=dd if=/dev/ttyUSB0 count=2 ; echo $RESPONSE

[HANGS]

Any ideas how to communicate with an xbee using a bash script?

If you love python try pyserial, you cand do everything you want with it, as well as programming your xbee or communicate with it !

little code example:

#! /usr/bin/python

from xbee import XBee
import serial

"""
serial_example.py
By Paul Malmsten, 2010

Demonstrates reading the low-order address bits from an XBee Series 1
device over a serial port (USB) in API-mode.
"""

def main():
    """
    Sends an API AT command to read the lower-order address bits from 
    an XBee Series 1 and looks for a response
    
    switch the XBEE to API mode by ATAP1:
    +++
    ATAP1
    ATWR
    """
    try:
        
        # Open serial port
        ser = serial.Serial('/dev/ttyACM0', 9600)
        
        # Create XBee Series 1 object
        xbee = XBee(ser)
        
            # Send AT packet
        xbee.send('at', frame_id='A', command='ID')
        
        # Wait for response
        response = xbee.wait_read_frame()
        print response
        
        # Send AT packet
        xbee.send('at', frame_id='A', command='CH')
        # Wait for response
        response = xbee.wait_read_frame()
        print response
        
        # Send AT packet
        xbee.send('at', frame_id='A', command='BD')
        # Wait for response
        response = xbee.wait_read_frame()
        print response        

    except KeyboardInterrupt:
        pass
    finally:
        ser.close()
    
if __name__ == '__main__':
    main()

Thanks,

I don’t know Python. When I cut and paste the example code, I get the following :

from: can't read /var/mail/xbee
./xbee_0.py: line 12: 

Are there any examples in C, C++, bash, or perl?

I have a coordinator and router connected and communicating using C/C++, but can’t get the AT commands working. When I send ‘+++’ the response OK comes back, but after that nothing in response to ATID, etc.\n serial_example.py\n By Paul Malmsten, 2010\n\n Demonstrates reading the low-order address bits from an XBee Series 1\n device over a serial port (USB) in API-mode.\n ‘: command not found
./xbee_0.py: line 14: syntax error near unexpected token (' ./xbee_0.py: line 14: def main():’


Are there any examples in C, C++, bash, or perl?

I have a coordinator and router connected and communicating using C/C++, but can't get the AT commands working. When I send '+++' the response OK comes back, but after that nothing in response to ATID, etc.

In the Python script you need to set the correct port name and Baud rate that the XBee is connected to. The example code uses: ser = serial.Serial(‘/dev/ttyACM0’, 9600)

But you had used: /dev/ttyUSB0

Change the Python code to match the port you are using.

Python is really pretty nice and very powerful. It is also very cross platform comparable where the same code works on Windows and Linux.

On my test machine the xbee’s are connected to /dev/ttyUSB0 and /dev/ttyUSB1. In the deployed system, part of the problem is to find which xbee is attached to which port. There are other things attached to /dev/ttyUSB* and they can be attached in any order. My idea was to check each port for serial number and/or ATID to find the ports (and networks) for each xbee. My plan was to use a bash script to find each network and launch the proper applications.