Send API frame through MATLAB serial


I have a transmitting xBee and a receiving one (both S6B series). I want to send an API frame to my xBee transmitter using MATLAB serial port. The frame is a simple Tx64 request sent to the receiving module. Both xBee settings and frame should be ok because I can successfully send the request using XCTU. The basic code I’m using is:

s = serial(‘/dev/tty…’,‘BaudRate’,57600);
set(s,‘DataBits’ , 8 );
set(s,‘StopBits’ , 1 );
set(s,‘Terminator’ , ‘’ )


APIframe = ‘7E length 00 frameID IPaddress 00 message checksum’;

fwrite(s, APIframe)

I believe the problem is the format I use for the frame, because I don’t have any errors from Matlab and RSS led on the receiving Xbee doesn’t turn on. It looks like the transmitting Xbee does not recognize the message as an API frame. I tried to convert it as decimal but still nothing.

Thank you for your help,


After several trials I found the correct form for the outgoing message. As supposed, the error was in the format of the string which as to be converted to decimal. If anyone else is interested here is my solution:

APIframe = [‘7E’;…RestOfFrame…;‘C0’];
APIframe = hex2dec(APIframe);
fwrite = (serial,APIframe,‘uint8’);

Have fun!


Hy!It’s bit late for a question but i’ll try anyway…
I am facing the same problem. i am reading an API frame in Matlab but does’t seem to work. I have tried your solution and same result. could you pls provide more matlab code so i can try understand how you solved it?
Thank you very much!

Hi Pietro,

Just a quick doubt about the APIframe message.
Does the characters of the message must be sent with separate by spaces, as you did?
‘7E length 00 frameID IPaddress 00 message checksum’

Or the can be sent together?

Sorry for the late replay, I temporarily lost access to my code. The way I’m constructing the frame with Matlab is by filling a vector element by element with desired hex values expressed as char, for example:

frame(1,:slight_smile: = ‘7E’; % Frame Class Name for reading only
frame(2,:slight_smile: = ‘00’; % length 1
frame(3,:slight_smile: = num2str(frmLgt); % length 2
frame(4:5,:slight_smile: = [‘00’;‘00’]; % type & ID
frame(6:12,:slight_smile: = [‘00’;‘00’;‘00’;‘00’;‘C0’;‘A8’;‘01’];
frame(13,:slight_smile: = address;
frame(14,:slight_smile: = ‘00’;

frame = hex2dec(frame);
fwrite(ssCOM, frame, ‘uint8’)

Hope this help, let me know if you need more info.

I replied to a comment to my first post, hope you get some insight from it. Let me know.

Hi Pietro,

Thanks a lot for your collaboration :slight_smile:
I followed this path and it works pretty good.

The current issue I’m facing now is how to read on MATLAb the messages sent over by the remote XBEE.

I tried to use fread() and fscanf() functions in MATLAB but I still haven’t had a successful outcome.

Do you have any hints?

Best regards,