Problem with fprintf
5 次查看(过去 30 天)
显示 更早的评论
I am trying to write some data to my serial port using the 'fprintf' command. The command works fine when i run it in my command window but when i try to run the same command in my script file it is unable to write data to the serial port. Please suggest the possible reasons and solutions.
No sir. This question is different. this is my code. It runs well in command window but in my gui it shows error...
global s
s = serial('COM8','BaudRate', 9600,'Parity', 'none','DataBits',8, 'StopBits',1);
s.Terminator = 'CR/LF';
s.BytesAvailableFcnMode = 'terminator';
fopen(s);
pause(0.5);
fprintf(s,'WelkinSat');
pause(0.5);
fprintf('CanSat is Now Ready for Communication--Go WelkinSat \n');
fprintf('Sample data from the main Program... \n');
a=fgetl(s);
disp(a);
回答(1 个)
per isakson
2012-6-3
There are two "different" fprintf. Note "serial" and "object, obj" in the documentation
Doc says:
fprintf (serial)
Write text to device
Syntax
fprintf(obj,'cmd')
fprintf(obj,'format','cmd')
fprintf(obj,'cmd','mode')
fprintf(obj,'format','cmd','mode')
Description
fprintf(obj,'cmd') writes the string cmd to the device connected to the serial port object, obj. The default format is %s\n. The write operation is synchronous and blocks the command-line until execution completes.
--- more ---
The object handle shall always be the first argument to fprintf. Doc has this example.
Specify an array of formats and commands:
s = serial('COM1');
fopen(s)
fprintf(s,['ch:%d scale:%d'],[1 20e-3],'sync');
Your statements
fprintf('CanSat is Now Ready for Communication--Go WelkinSat \n');
fprintf('Sample data from the main Program... \n');
should print to the command window according to the documentation.
3 个评论
Walter Roberson
2012-6-3
fgetl is implemented for serial.
http://www.mathworks.com/help/techdoc/ref/serial.fgetl.html
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!