Machsupport Forum

Mach Discussion => VB and the development of wizards => Topic started by: giannis121 on March 29, 2011, 12:43:41 PM

Title: Read value from file and put it to a DRO...
Post by: giannis121 on March 29, 2011, 12:43:41 PM
How can i read a value from a txt file (on a VB script button)
and then put the value to a DRO?

The txt file contains only the number value.
I have tried to use "file input/output" from the VB-Script-Commands.pdf in Mach Wiki,
with no success.

Any ideas would help.
Kind regards,
Giannis
Title: Re: Read value from file and put it to a DRO...
Post by: ger21 on March 29, 2011, 01:23:48 PM
Try something like this:

FilePath = "C:\DRO.txt" 'Put the full path here

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.getfile(FilePath)
Set DROfile = f.OpenAsTextStream(1)

DROValue = CDbl(DROfile.readline)

DROfile.Close

SetOEMDRO(*********, DROValue)
Title: Re: Read value from file and put it to a DRO...
Post by: zealous on March 29, 2011, 01:40:26 PM
Also make sure you are using the correct command.
If you are setting a OEMDRO or UserDRO.
I have tested and the "Input" works fine here is an example using Input as well:
If you have 1 value to set simply change the "DROS = 1" to what you want, for example to set DRO800, "DROS = 800".

Code: [Select]
Dim fich As String
Dim FileData As String
 
'////////////FILE LOCATIONS//////////////////////////////////////
fich="C:\Mach3\MyFile.txt"

'//////////CHECK If FILES EXIST/////////////////////////////
If Dir(fich)="" Then
MsgBox "File Isn't There!"
Else

'//////Set DRO's/////////////////////////
DROS = 1
'////////////////Open To Read file//////////////////////////
Open fich For Input As #1
Do While Not EOF(1)

'////////////Read a Line of data////////////////////////
Line Input #1, FileData

'////////////Set User DRO's////////////
If(DROS > 999) Then
SetUserDRO(DROS, FileData)
Else
'////////////Set OEM DROs////////////
SetOEMDRO(DROS, FileData)
End If

DROS = DROS + 1
Loop

Close #1
'////////////File Isnt there////////////
End If
Title: Re: Read value from file and put it to a DRO...
Post by: giannis121 on March 29, 2011, 02:30:51 PM
Very helpful!!
Thanks!

And by the same way how can i get a DRO and write it to a txt file?

Regards,
Giannis
Title: Re: Read value from file and put it to a DRO...
Post by: poppabear on March 30, 2011, 08:34:31 AM
from an old post,

'M1001.m1s Save User DROs and LEDs to file and/or this can run in your macro pump.
UserDROs = 1000
UserLEDs = 1000

'Optional, you can have a Brain using a timer
'that for every 5 minutes or so this file will run again, updating the status.
'OR you can have a SAVE user DROs/LEDs button that will activate this, you would need
'to put this code in your MacroPump for the AutoSave and manual Save to work.

'If GetUserLED(2100) Then   'Optional code see above.

Open "UserDROStates.txt" For Output As #1   ' Open to write file.
For x = 1000 To 2255
DROState = GetUserDRO(UserDROs)
Write #1, DROState
UserDROs =(UserDROs + 1)
Next x
Close #1

Open "UserLEDStates.txt" For Output As #2   ' Open to write file.
For y = 1000 To 2255
LEDState = GetUserLED(UserLEDs)
Write #2, LEDState
UserLEDs = (UserLEDs + 1)
Next y
Close #2

'End If 'optional from the if above for auto save functions

'Enjoy Scott
Title: Re: Read value from file and put it to a DRO...
Post by: giannis121 on April 01, 2011, 05:43:53 AM
Thank you! Thats it!