|
-
May 29th, 2009, 10:02 AM
#1
Thread Starter
New Member
Reading file and assign it to file-related var
Hi, its probably going to be an easy question for some of you guys, but i've looked around and i haven't found how to do it...
I want my program to read a text file and assign the values to different variables... but the thing is that the vars i want them assigned to are in the text file.
Here a quick look of what the text file looks like:
Code:
[Miranda]
C150 = 0 ; Discrete (1= Helium used) ;
C152 = 0 ; Discrete (1= bypass used) ;
C151 = 0 ; Discrete (1= Vacuum used) ;
C202 = 0 ; Discrete (1 = Bar code used) ;
C203 = 1 ; Discrete (1 = Com with Tester) ;
C207 = 1 ; Discrete (1 = Com with Chiller) ;
C404 = 1 ; Discrete ; Use to trig recipe name in confirmation screen
C212 = 0 ; Discrete (1 = Helium through heat sink) ;
C204 = 0 ; Discrete (1 = Interlock controled by the box) ;
C153 = 1 ; Discrete (1 = CTI used) ;
C200 = 0 ; Discrete ;
So simply, if i read that file with VB6, i want C150=0, C151 = 0, etc...
Thanks
-
May 29th, 2009, 10:18 AM
#2
Re: Reading file and assign it to file-related var
Welcome to the forums.
Actually this is not very easy and would require some creative solutions. You cannot arbritarily create variables during runtime; they have to be created ahead of time.
I doubt you want to declare hundreds of variables in advance.
But maybe you can provide more information. For example, what range of C numbers will you have? Is this fixed or can it literally be anything between C1 thru C-infinite? That answer will allow us to provide a couple of workarounds. I can think of two potential solutions: one using arrays and one using a collection.
You should also think about how you will reference these "variables" if you don't know what their names will be in advance.
-
May 29th, 2009, 10:31 AM
#3
Thread Starter
New Member
Re: Reading file and assign it to file-related var
Hi, thanks for answering that quick and welcoming me here.
There is actually 19 C variables, along with 27 V variables to assign.
I'M writing to PLC memory as you probably already guessed. I might be adding couple of C and V memory in the future, thats why i'm trying to have it the most dynamic possible... But adding 1-2 variable in the future wont really be a problem, i'll have to dim them and add them to the writing sequence...
If you want to know what i am planning to do with these... Well my VB program will know that if C150 = 1, since its located at V40606, Byte location 10, the value will be 2^1 for the left MSB byte of V40606. I Will sum the V addresses all together... read whats already in the V address (sometimes there are bytes already written in the V address), and i have afunction that adds them (its more a OR function actually) and then write it to the PLC.
I dont know if its really clear?
I thought about relocating the C so that they are all one after the other, but that implies that i'll have to modify my ladder and c-more screen and related txt files that all together have more than 3-4K lines.
Thanks !
and and PS, Here are the C and V value list i am actually using, just FYI.
PS2: if i add C values, they will fit starting at C140 to C147, which is the same V memory as C150-157, so its going to be pretty easy.
Code:
C150
C151
C152
C153
C154
C155
C156
C157
C160
C161
C162
C200
C202
C203
C204
C207
C212
C404
C622
V2030
V2031
V2032
V2033
V2034
V2041
V2052
V2060
V2063
V2063
V2073
V2075
V2076
V2100-V2117
V2131
V2132
V2133
V2200
V2430
V2431
V2432
V2440
V2441
V2442
V2443
V2444
V2445
V2446
Thanks
-
May 29th, 2009, 10:56 AM
#4
Re: Reading file and assign it to file-related var
Not completely with you, but close. If you don't know how many C or V numbers you'll have in the future, not sure how you can code your routines to know when to look for "future" numbers and when not to. But that is a challenge for you I'd imagine.
1. You can use a cross-reference and an array, but that does mean you have to hardcode your cross-referneces. The array would be sized to 19 for your Cs and 27 for the Vs. Then you'd reference them something like this, using a function to convert
Code:
Private Function XRefC(ByVal Cnumber As Long) As Long
Select Case Cnumber
Case 150: XRefC = 0 ' first array item
Case 151: XRefC = 1 ' 2nd array item
...
Case 622: XRefC = 18 ' last array item (0 thru 18 = 19 items)
End Select
End Function
The above is probably the easiest but does require a hardcoded/maintained listing. And may also require a cross-reference in reverse: going from an array item to the Cnumber. Of course this is yet doubled, because you'd need the same thing for your Vs.
What would happen is that you'd pre-Declare your array in advance and read the text items and cross-reference them for storing:
Code:
Dim Cnumbers(0 to 18) As Long ' 19 items
... in your file read procedure
' parse out the C number, and parse out its value
' example: strC = "C150" & strVal = "0"
' next send to your XRef to get the array item to update
lngItem = XRefC(Val(Mid$(strC,2)))
Cnumbers(lngItem) = Val(strVal)
2. Another option may be to use a collection. The C/V number is the key for the collection item
Code:
Dim myCollection As New Collection
....
' parse out the C number, and parse out its value
' example: strC = "C150" & strVal = "0"
' add them to your collection
myCollection.Add CLng(strVal), strC
Now that you have them loaded, you can reference them by their Cnumber from the collection. However, you will need error checking in case you didn't read in a C number. Also note that if a Cnumber is duplicated in your text file, you will get an error when trying to add it to the collection if it already exists. Here is an example of referencing the C/V number with error checking...
Code:
Private Function GetCollectionValue(ByVal Key As String) As Long
' key is passed as C150, V2075, etc
On Error Resume Next
GetCollectionValue = myCollection(Key)
' if key doesn't exist, GetCollectionValue = 0
End Function
With a collection, you can also know how many items you read in and you can iterate them using For:Each or a For:Next loop. However, VB collections do not expose a Key as a property, so you can't determine if the 3rd collection item is C152 or C153 for example. There are workarounds for that too, though.
This may sound a bit odd and if you have any specific questions, ask away. Others may provide other potential solutions like maybe use of classes as a collection.
Last edited by LaVolpe; May 29th, 2009 at 11:06 AM.
-
May 29th, 2009, 11:14 AM
#5
Thread Starter
New Member
Re: Reading file and assign it to file-related var
 Originally Posted by LaVolpe
Not completely with you, but close. If you don't know how many C or V numbers you'll have in the future, not sure how you can code your routines to know when to look for "future" numbers and when not to. But that is a challenge for you I'd imagine.
I might end up having 5 more C and V at most... but i will add them manually at that time...
I have one question regarding the codes you just posted... i dont see where is the reference to the text file?!
thanks a lot for your help!! i appreciate it
-
May 29th, 2009, 11:17 AM
#6
Re: Reading file and assign it to file-related var
I was making the assumption that you knew how to read a text file. If not, take a quick look at the forum's FAQs. Here is the link.
-
May 29th, 2009, 11:22 AM
#7
Thread Starter
New Member
Re: Reading file and assign it to file-related var
Hehe yup i know how to read a txt file i just wanted to make sure there was not somekind of tricky way to load values to vars with xref directly from the txt file without having to build an array with the txt file at first then referencing to it!
thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|