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
2011 年 12 月 26 日
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
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
2011 年 12 月 26 日
0 投票
1 件のコメント
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 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!