LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

about fmt command

Solved!
Go to solution

我使用fmt命令时,程序如下

char a[10];

float data;

fmt (%data,"%f<%s",a);

 

 其中a中是从串口采集到的数据,结果编译通过了,运行时在%data这里停止了,说是变量类型和fmt转换的形式不符合,不知道为什么,请解答,谢谢

0 Kudos
Message 1 of 8
(5,683 Views)

fmt command

char a[10];

float da;

fmt (%da,"%f<%s",a);

 

the date from com1 are put in a,when i run the program , the error is
attempt to read beyond the end of the string

 

i donnot know how can i do about it

 

 

 

0 Kudos
Message 2 of 8
(5,680 Views)

Since data from your external instruments are put in a string, you must use Scan instead of Fmt to extract values from the string, as in the following example:

 

char a[10];
float da;

strcpy (a, "123.45");
Scan (a,"%s>%f[b4]", &da);

 

after the conversion da = 123.4499969



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 3 of 8
(5,669 Views)

thank u

 

my emample is

 

char a[10];

double da;

fmt(da,"%f<%s",a);

 

and the data only use 9 bytes in a, if i define : char a[9], the error" attmept to read beyond the end of the string"appear again

 there is another problem

 

float a;
int b=32;
a=b*2/160;

 

the value of b is 32, and the value of a is 0.0000

why? 

 

 

 

 

 


0 Kudos
Message 4 of 8
(5,664 Views)

Because the variable b, and all associated values in the expression (ie 2 and 160) are integers, the compiler performs calculations using integer mode. You can force it to use floating point mode quite easily:

 

a = (float) b * 2 / 160;

 

or:

 

a = b * 2.0 / 160; 

 

JR

0 Kudos
Message 5 of 8
(5,661 Views)
Solution
Accepted by topic author greeysid

thanks

 

 

0 Kudos
Message 6 of 8
(5,654 Views)

greeysid wrote:

thank u

 

my emample is

 

char a[10];

double da;

fmt(da,"%f<%s",a);

 

and the data only use 9 bytes in a, if i define : char a[9], the error" attmept to read beyond the end of the string"appear again


Greeysid,

In your case it is preferable to use Scan instead of Fmt: the first instuction scans a string extracting values from it (which is your problem), while the second one is used to format items into a string which is the opposite problem. Even though you could use Fmt to "format" a string representation of a number to a double variable (but the corresponding code should be Fmt (&da, "%f<%s", a);  ) it is preferable to use Scan that permits you more control over the conversion.

 

Regarding stringh lenght, remember that all strings need to be nul-terminated , that is you must allocate one additional byte with respect to the content of the string to permit storing of terminating NULL byte. There are some additional hints regarding this that you can read in the online help, looking for "Scanning strings that are not null-terminated" in the Formatting and I/O library reference.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 7 of 8
(5,630 Views)

//In your case it is preferable to use Scan instead of Fmt: the first instuction scans a string extracting values from it (which is your problem), while the second one is used to format items into a string which is the opposite problem. Even though you could use Fmt to "format" a string representation of a number to a double variable (but the corresponding code should be Fmt (&da, "%f<%s", a);  ) it is preferable to use Scan that permits you more control over the conversion.

 

Regarding stringh lenght, remember that all strings need to be nul-terminated , that is you must allocate one additional byte with respect to the content of the string to permit storing of terminating NULL byte. There are some additional hints regarding this that you can read in the online help, looking for "Scanning strings that are not null-terminated" in the Formatting and I/O library reference.//

 

 

ok i will try to use Scan command

 

thank u 

0 Kudos
Message 8 of 8
(5,623 Views)