Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Rs232 hardware communication,  fprintf
Date: Tue, 3 Nov 2009 09:41:38 -0500
Organization: The MathWorks, Inc.
Lines: 53
Message-ID: <hcpfdb$8h8$1@fred.mathworks.com>
References: <hcof12$gn8$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1257259243 8744 172.31.44.65 (3 Nov 2009 14:40:43 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 3 Nov 2009 14:40:43 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
X-RFC2646: Format=Flowed; Original
Xref: news.mathworks.com comp.soft-sys.matlab:582056



"zhang mian" <zhangmian0126@126.com> wrote in message 
news:hcof12$gn8$1@fred.mathworks.com...
> Hi,
> I am doing a hardware real time control through rs232 in M-file. The 
> problem is when i put fprintf(serobj,'rat 1'),the motor will run at a 
> speed 1ml/hr but if the speed is calculated by my program and represented 
> by variable 'u1', i put it like fprintf(serobj,'rat u1')  the hardware 
> will not response to it. Even it shows u1=12.25 or sth else, i put the 
> fprintf(serobj,'rat u1'), the motor still run at a default speed 1ml/hr. I 
> change it to
> fprintf(serobj,'rat 6'), the speed of the motor will change to 6ml/hr from 
> 1ml/hr.
>
> The hardware technician said he was not familiar with Matlab but if the 
> program is written in C, the function should be fprintf(serobj,'rat 
> %f',u1). But it seems the Matlab doesn't accept this format. It only can 
> be fprintf(serobj,format,variable), in this format the whole variable will 
> become same format, number or character ect. Now i need the 'rat' in 
> character format but the u1 in a float-point format, what can i do?

From the documentation for the serial object's FPRINTF method, the first 
syntax you gave in the second paragraph is the correct syntax.

u1 = 12.25;
fprintf(serobj, 'rat %f', u1)

Can your motor accept noninteger values for the 'parameter' of the rat 
function?  Try:

u1 = 6;
fprintf(serobj, 'rat %f', u1)

Does it expect the parameter after rat to be in a particular format, say an 
integer (as per the C %d specifier?)

u1 = 6;
fprintf(serobj, 'rat %d', u1)

Does it have an upper limit on its speed that is lower than 12.25?

u1 = 1.5;
fprintf(serobj, 'rat %f', u1)

Does the motor have the capability to return status/diagnostic information 
if you give it something it doesn't expect?

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ