04-13-2022 11:12 AM
Hi, here's my problem.
I'm trying to send a string (MyCMd) to a virtual serial com port , on CVI, I have:
int a,b;
double c;
a=80000000;
b=79993715;
c=1;
OpenComConfig (PORT,COM, 115200,0,8,1,512,-1);
SetCTSMode (PORT,0);
sprintf(MyCmd, "SWEEP 1 %d %d %f 0\r", a ,b, c);
stringsize = StringLength (MyCmd);
ComWrt (Port, MyCmd, stringsize);
Delay(0.1);
CloseCom (PORT);
MyCmd is sent to a PC wich displays the output in ASCII, usually I get this:
53 57 45 45 50 20 31 20 38 30 30 30 30 30 30 30 20 37 39 39 39 33 37 31 35 20 31 2E 30 30 30 30 30 30 20 30 0D
Which corresponds to: "SWEEP 1 80000000 79993715 1.000000 0"
But if I change the format of c, for example %f->%.4f, I get:
53 57 45 45 50 20 31 20 38 30 30 30 30 30 30 30 20 37 39 39 39 33 37 31 35 20 31 2E 30 30 30 30 20 30 0D 00 0D
As you can see, the "lack" of 0's is filled by unwanted ascii. If c is an integer, the string is filled with 7 unwanted ascii values (the dot and the 6 zeros). I run an equivalent script on python and Hyperterminal and I don't have to face this issue.
How can I send a string without those ascii values ?
Solved! Go to Solution.
04-14-2022 02:27 AM
If c is an integer, the string is filled with 7 unwanted ascii values (the dot and the 6 zeros).
Nothing to do with serial port. It's just printf expected behavior. %.4f means integer part plus a dot plus 4 digits. If those are 0, so be it. If you want a shorter display whenever possible (for instance if c is an integer), try %.4g it will give you 1 in that case and 123.4567 in other cases and exponents in others.