XBee + MATLAB serial communications

Hi all:

I've got an XBEE Pro Series 2 directly hooked up to my computer via USB. I am able to send '+++' and receive an "OK" response when using the Arduino IDE's serial monitor. However, I am unable to do the same in MATLAB.

s=serial('/dev/ttyUSB0');
fopen(s);
fprintf(s, '+++');
% wait three seconds
s.BytesAvailable

s.BytesAvailable = 0 always - i.e., there's nothing on the input buffer for me to read. I expect, of course, the 'OK' signal.

Any ideas what I need to do in MATLAB to make this work? (Settings, etc). I suppose a good starting point is what are the settings the Arduino IDE uses in its serial monitor.

Thanks!

I got it to work using fwrite() instead of fprintf() - I now get the 'OK' signal when I perform an fread().

But now I have a problem with sending commands to the XBee:

fwrite(s,'ATID\r');

I get no response from the module - s.BytesAvailable remains 0.

The Arduino IDE Serial Monitor yields the exact same behavior - I can input '+++' and get an 'OK' just fine, but nothing happens when I type "ATID\r". So it's not necessarily my MATLAB code - there is something going on with the serial communication itself.

However, using X-CTU with WINE (using Linux), I can connect to the XBee just fine and send commands with that serial terminal.

Any idea what is going on? Thanks!

Solved!

I don't know why the method of using fwrite() doesn't work when using a non-'+++' command. I was just playing around with the MATLAB command prompt. Here's what I did, with comments:

s = serial('/dev/ttyUSB0');
s.Terminator = 'CR'; % XBee expects the end of commands to be delineated by a carriage return.

fopen(s);

% Put the XBee in Command Mode
fwrite(s,'+++');
response = fread(s, s.BytesAvailable); % This will be a column vector of hex values - use the char() function to translate to ASCII, and transpose it if desired.

% Send an ATxx command

fprintf(s,'ATID'); % The \r is automagically sent out since we specified s.Terminator = 'CR'.

response = fread(s,s.BytesAvailable); % Result of the ATID command - again a column in hex, postprocess as desired.

I hope this helps anyone else out there who got stuck on this like I did. I am now able to send commands to XBee and receive them. My hope is to create a MATLAB serial terminal for XBee devices to simplify working with them.

EDIT: Hmm, I can't seem to replicate what I just did. I'm not sure why it's not working now. This seems silly. I'll keep posting with what I get.

EDIT2: It looks like the above works - apparently I'm hitting the timeout way too quickly and the XBee is exiting command mode much faster than I expected. I'll play around with it more.

For anyone interested - here is the code I wrote that lets you interface to your XBee device with MATLAB. It takes two inputs - your XBee device (s - assumed to be a serial devices that you've set up already) and a command in ASCII (command).

% XBCOMMAND - Write a command to an XBee device

function xbcommand(s, command)

    fprintf('\n');
    timeoutVal = 3; % Number of seconds to wait for data before killing the program
    
    if (~ regexp(s.Terminator,'CR')) % Reset the terminator to \r if needed.
        try
            fclose(s);
        end
    
        s.Terminator = 'CR';
        fopen(s);
    elseif (regexp(s.Status,'closed')) % if s wasn't open already, open it now.
        fopen(s);
    end
        
    % Write the user-specified command
    if (regexp(command, '\+\+\+'))
        fwrite(s, command);
    else
        fprintf(s, command);
    end
    
    % fetch, process and print result
    timeout = 0;
    while (timeout < timeoutVal) 
        if (s.BytesAvailable > 0) 
            timeout = 0;
            if (s.TransferStatus == 'idle')
                pause(0.01);
                q = fread(s, s.BytesAvailable, 'char');
                q = q';
                fprintf('%s\n', char(q));
                return;
            end
        end
        timeout = timeout + 1;
        pause(1);
    end
    
    error('Command timed out\n');

Hope this reduces someone else's pain and suffering. All I ask is you pay it forward if you use it - contribute to the general body of knowledge somehow!

1 Like