Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Lookup File


Sam Dec 1, 2011 11:02 PM

Customer wanted an idea of how to create a lookup file on the USR drive. The desire was to track the last known value for a particular sensor ID. There were a lot of sensor IDs and the value needed to persist across program loads. Here is an example program. Each ID and Value are constrained to 4 bytes each. So an ID/VAL pair is 8 bytes. IDs are located saved in ascending order. You can kind of think of the file like a spreadsheet where each row contains an ID in the first column and a value in the second column. But the program uses binary read / writes and byte offsets to make the whole process faster.

Below is an example program that demonstrates the concept. Load it onto your CR800, CR1000, or CR3000. Then use the WriteOut() and WriteNow variables to update an ID/VAL pair.
Use ReadID and ReadNow to lookup a stored ID/VAL pair.


'Variable Declaration
Dim I, J
Dim FHandle As Long
Dim FName As String * 31

Public WriteOut(2) As Long
Public WriteNow As Boolean

Public ReadID As Long
Public ReadNow As Boolean
Public ReadOut(2) As Long

BeginProg

'Set up a USR drive on the logger
SetStatus ("USRDriveSize","1000000")

'Establish the name of the lookup file
FName = "USR:data.bin"

'Make a dummy lookup file for testing
'Write out 8 bytes per ID/VAL pair
'Write out all IDs (0-8191)
'Increment VAL each time, wrap at 2047
FHandle = FileOpen(FName,"wb",0)
For I = 0 To 8191
WriteOut(1) = I
WriteOut(2) = J
FileWrite (FHandle,WriteOut,8)
J = J + 1
If J > 2047 Then J = 0
Next I
FileClose (FHandle)


Scan (1,Sec,0,0)

'Process to update an ID/VAL pair in the lookup file
If WriteNow Then
FHandle = FileOpen (FName,"r+b",WriteOut(1)*8)
FileWrite (FHandle,WriteOut(),8)
FileClose (FHandle)
WriteNow = FALSE
EndIf

'Process to lookup a ID/VAL pair in the file
If ReadNow Then
FHandle = FileOpen (FName,"rb",ReadID*8)
FileRead (FHandle,ReadOut(),8)
FileClose (FHandle)
ReadNow = FALSE
EndIf

NextScan

EndProg

Log in or register to post/reply in the forum.