Moving decimal places FormatString


BeeHay

Recommended Posts

Can DAQFactory use toPrecision() and/or toFixed() to move decimal places in a number being output say into a text document or to a receiving pc?

i can move the decimal as far as i want to going to the right, but moving to the left is giving me troubles...

also, how can i take a received signal over my serial connection and display it in a varial value component?

would it be device.serialin.read?

the same connection you helped me out with is in this post ..

may have to create the protocol?

Any info will be useful!

Thanks!

Brandon

Link to comment
Share on other sites

DAQFactory's function for formatting numbers is called Format(). It uses the same specifiers as the C printf() function which is very well documented on the web and elsewhere. The specifiers are also described in the help with some examples. Post if you have detailed examples of what you want.

As for serial, you most definitely can do this, though not in Express as Express only works with LabJacks. In the other versions of DAQFactory you can do this. How you do it depends on the protocol. What exactly is your device outputting?

Link to comment
Share on other sites

DAQFactory's function for formatting numbers is called Format(). It uses the same specifiers as the C printf() function which is very well documented on the web and elsewhere. The specifiers are also described in the help with some examples. Post if you have detailed examples of what you want.

thanks! i was wondering which to look for, thought it was Format.String or toPrecision

an example would be my output is being read as 400 when it should be 4.00...

and my format is this

device.serout.write("0140" + format("%04.1f",

but i really believe it is the receiving pc..i think the scaling is wrong or something...

i can monitor my device with the comm monitor and it is saying that it's Tx is 4.00, just the screen on the other pc is showing 400...i can't access the other pc's settings though..only with a call from tech support.

As for serial, you most definitely can do this, though not in Express as Express only works with LabJacks. In the other versions of DAQFactory you can do this. How you do it depends on the protocol. What exactly is your device outputting?

the device i want to pull data from is transmitting a set of numbers in the same format as what i'm trying to send in...

!!

01103850.00

&&

01 = record number

10 = item number

3850.00 = channel value

not sure on how to pull it from the set and display it.

i was thinking it is

device.serout.read

Thanks!

Brandon

Link to comment
Share on other sites

Well, format("%04.1f",400) will output 400.0. The easiest way to see this is to use the command / alert window and type:

? format("%04.1f",400)

This is a great way to test this (and any other) function. To get 2 decimals, you need .2f, not .1f. If the Tx is saying 4.00 then that's what's going out the serial port.

As for the serial in, are the carriage return / line feeds in your input? This is important. When specifying the string coming across the port, you have to include carriage returns and line feeds if they are there.

Link to comment
Share on other sites

how would i move the decimal left though, i can add 2, 4, 6 ect zeros on the right, but i want something like 0.00450 instead of 450...

i have tried dividing/multiplying it out, but that's a no go though cause the

ch1*2000-10

is the correct math for my channel 1's final value...if i divide 1000 or multi 1000, my value is incorrect...

and yes, the

chr(13)  chr(10)

are in the incoming serial data..

sorry if i double posted this and thanks

:)

Link to comment
Share on other sites

I don't get what you mean. You want to take 450 and display it as 0.00450? If so, then you need to divide by 100000.

Now on the serial side. With this as input:

!!\013\010

01103850.00\013\010

&&\013\010

I'd probably do this code (assuming DF 5.70+):

private string datain
private data
private record
private item
while (1)
   try
	  datain = device.mydevice.readuntil(10)
	  if (strtodouble(datain) != NaN()) // if !! or &&, strtodouble() will return NaN
		 record = strtodouble(left(datain,2))
		 item = strtodouble(mid(datain,2,2))
		 data = strtodouble(mid(datain,4,10))
		 // do something here with the numbers...
	  endif
   catch()
	  delay(0.1)
   endcatch
endwhile

Link to comment
Share on other sites

I don't get what you mean.

:) i'll mess with it

Now on the serial side. With this as input:

!!\013\010

01103850.00\013\010

&&\013\010

I'd probably do this code (assuming DF 5.70+)

i dont need the item or record number...so i could just take those two out of the if statement, correct? or do i nan out the other items?

something like this? put private data in the vari valu comp expression? -

ignorance edit...

thanks a ton...

Brandon

Link to comment
Share on other sites

Great, glad that worked.

Now, sorry to be picky, but since this is forum that many people read, I'm going to mention a thing that I see a lot of, in hopes that some will change their ways: When specifying decimal numbers that are less than 1 but greater than -1, you should always precede the decimal place by a zero. So instead of:

ch1*20-.1)

You should put:

ch1*20-0.1)

The reason for this is that the decimal place can easily be missed. If you look through most scientific papers you will always see the preceding 0, so this applies to print as well as computer.

Link to comment
Share on other sites

i threw this in a new sequence and i'm getting an uncaught error in my sequence line 4

global string datain
global data
while (1)
	  datain = device.serout.ReadUntil(10)
	  if (strtodouble(datain) != NaN()) 
		 data = strtodouble(mid(datain,4,10))
	  endif
   catch()
	  delay(0.1)
   endcatch
endwhile

can i do something like device.serout.readuntil (999999999999) and take the catch and delay off and add datain=datain+1?

also a couple of OT questions -

when is DF 6.0 coming? :)

and will there be a charge to upgrade to it?

also when i run this program on an old monitor with a really low res like 800x600, i can't "scroll" through my program...i tried to add a scroll bar, and for some reason, there is no "Vertical" tick box to check...

thanks again for all your help...

Link to comment
Share on other sites

You forgot the "try":

while (1)
   try
	  datain = device.serout.ReadUntil(10)
	  if (strtodouble(datain) != NaN())
		 data = strtodouble(mid(datain,4,10))
	  endif
   catch()
	  delay(0.1)
   endcatch
endwhile

The indenting is the give-away and thus the reason good indenting pays off. You had a six space indent from while(1) to the next line.

That should fix your problem, so you don't want the readuntil(999999).

6.0 will come out probably the middle of next year. Why do you ask? Are there features you'd like to see?

There will not be an upgrade charge.

There is no screen scrolling at this point. This is one feature that will definitely be in 6.0. The scroll bar component doesn't have "Vertical" because it automatically determines the direction based on the shape of its box. Just resize it so its taller than wide and it will display vertical. The scroll bar component does not actually scroll anything. You have to program it to scroll something.

Link to comment
Share on other sites

i guess i needed the whole item, record, and data for the code...like this -

global string datain
global data
global record
global item
while (1)
   try
	  datain = device.WITS.ReadUntil(10)
	  if (strtodouble(datain) != NaN()) &= NaN())
		 record = strtodouble(left(datain,2))
		 item = strtodouble(mid(datain,2,2))
		 data = strtodouble(mid(datain,4,10))
	  endif
   catch()
	  delay(0.1)
   endcatch
endwhile

thanks for that again! :)

i'm not sure if i need .readuntil or just .read cause when i change readuntil to (1) or (100) i get an infinite loop or timeout error, but it does display until it errors out...

i thought i wanted an infinite loop?

this is the error -

infinite.jpg

the code does work though, i am reading and displaying the data variable in a variable value component..

there is one problem though...the data coming in is this -

txrx.jpg

it's reading all of those values and displaying them one at a time...all i want is the 3rd set, "7508.04", not the other values...

do i need to make the code recognize the 0110 in the if statement somewhere?

Link to comment
Share on other sites

if (strtodouble(datain) != NaN()) &= NaN())

I don't know what the extra &= Nan() is for and depending on your release of DAQFactory, the parser is probably ignoring it. It does not make syntaxical sense, so eliminate it.

If all you want is the data when the first four characters are 0110, you should change that line to:

if (left(datain,4) == "0110")

As for your infinite loop, you for some reason have that turned on. Just go to File - Preferences and turn it off. It defaults to off in new releases as it usually just gets in the way (as it is in your case).

You do want ReadUntil(10) because the \010 marks the end of a line and the rest of the code expects a full line. Considering how fast your loops are running, you should check your timeout on your serial port and make sure its set to 100 or more. Anything shorter is kind of pointless and will cause this loop to run too fast and chew up all your CPU power.

Vertical graphs: you can actually do this. Just treat the graph as an XY graph and put time on the Y axis. You can't put "Time" as the Y expression though, but instead need to get the time of the channel manually. So if you are normally plotting MyChannel vs Time, you'd instead plot MyChannel.Time vs MyChannel. Note that the axis label won't be in units of time, which is the only problem with this method. However, you can disable the Y axis labeling and add your own manually.

As for moving and rotating symbols, you can definitely already do that, and have been able to since the beginning of DAQFactory. To move any component, use the component's MoveBy() and MoveTo() functions. You'll need to name the component first. To rotate, you can specify a non-zero Rotation on the image to rotate the image as is with each screen refresh, or use multiple images in an animation to make it look like its rotating, which is good for off-axis rotation.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.