Problem with fprintf

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);

댓글 수: 8

Ryan
Ryan 2012년 6월 3일
Perhaps sharing a bit of your code will help clarify the issue/causes?
Oleg Komarov
Oleg Komarov 2012년 6월 3일
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
Walter Roberson
Walter Roberson 2012년 6월 3일
I suspect this question is really about sharing variables between functions, which Pradip asked about in a further post http://www.mathworks.com/matlabcentral/answers/40164-calling-variables
skyimager
skyimager 2012년 6월 3일
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);
skyimager
skyimager 2012년 6월 3일
Communication should start after the code "WelkinSat" is written to the device.....but the program is unable to print it....
Walter Roberson
Walter Roberson 2012년 6월 3일
What error does it show?
Have you considered the possibility that a pause(0.5) might not be long enough?
skyimager
skyimager 2012년 6월 3일
It says....Warning...Terminator Reached before it was called for. I get this error when i try to read from serial port before writing the code WelkinSat to it. That means it is not able to write to the Serial port.
skyimager
skyimager 2012년 6월 3일
I first gave the command without pause only but it was nt wrkng. So i thgt pause mite wrk bt no results.

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

답변 (1개)

per isakson
per isakson 2012년 6월 3일

0 개 추천

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

per isakson
per isakson 2012년 6월 3일
I cannot find anything in the documentation that suggest that fgetl(s) can read from a serial port. I think you need to read the documentation more carefully. I added a few lines to my answer above.
Walter Roberson
Walter Roberson 2012년 6월 3일
fgetl is implemented for serial.
http://www.mathworks.com/help/techdoc/ref/serial.fgetl.html
per isakson
per isakson 2012년 6월 3일
My mistake

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

카테고리

도움말 센터File Exchange에서 Enterprise Deployment with MATLAB Production Server에 대해 자세히 알아보기

질문:

2012년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by