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..

답변 (2개)

bym
bym 2011년 12월 26일

0 개 추천

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

Walter Roberson
Walter Roberson 2011년 12월 26일
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.)

댓글을 달려면 로그인하십시오.

Olof Hansson
Olof Hansson 2011년 12월 26일

0 개 추천

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

Walter Roberson
Walter Roberson 2011년 12월 26일
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Arduino Hardware에 대해 자세히 알아보기

질문:

2011년 12월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by