Thread Subject: Rs232 hardware communication, fprintf

Subject: Rs232 hardware communication, fprintf

From: zhang mian

Date: 3 Nov, 2009 05:28:03

Message: 1 of 2

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?

Subject: Rs232 hardware communication, fprintf

From: Steven Lord

Date: 3 Nov, 2009 14:41:38

Message: 2 of 2


"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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
realtime control zhang mian 3 Nov, 2009 00:29:07
fprintf zhang mian 3 Nov, 2009 00:29:07
rs232 zhang mian 3 Nov, 2009 00:29:06
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com