Serial communication issues

Hi!
I'm trying to create an .m-file that can both send transmit and receive data from an Arduino Uno. The Arduino is set to echo any incoming signal.
When I'm using the mttool I get the correct values, in eg if I send 1 the received data is 1. But the things is when I try to do this with an m-file I get:
Warning: A timeout occurred before the Terminator was reached.
This is the code I'm using:
% Find a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', '/dev/tty.usbmodemfd131', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('/dev/tty.usbmodemfd131','BaudRate', 9600, 'StopBits', 1);
else
fclose(obj1);
obj1 = obj1(1)
end
% Connect to instrument object, obj1.
fopen(obj1);
% Communicating with instrument object, obj1.
data1 = query(obj1, '1');
fclose(obj1);
I'm using an macbook btw..

Answers (2)

bym
bym on 26 Dec 2011
It's possible that mttool (whatever that is) is adding a termination character to the communication packet that Matlab is not. You might try spying on the port to see the packet structure to determine if that is the case. Or you could try:
data1 = query(obj1,'1\n');

1 Comment

I agree, termination issues would be the first place to look. The default terminator for serial objects might not be the same as what the Arduino needs (my recollection of previous Arduino threads is that this *is* an issue.)

Sign in to comment.

Olof Hansson
Olof Hansson on 26 Dec 2011
tmtool not mttool, my bad.
Thanks for the suggestion. Unfortunately it did not solve my issues.
So the serial communication works. From the session log tab one could snap the code for the very same sequence.
Thanks for your help guys!

1 Comment

Please get(obj1,'Terminator) to check. The %s\n is not sufficient to establish that Linefeed is being used: see http://www.mathworks.com/help/techdoc/matlab_external/terminator.html

Sign in to comment.

Categories

Asked:

on 26 Dec 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!