Beijer Electronics (formerly QSI Corporation)

Manufacturer of Mobile Data and Human Machine Interface Terminals.
It is currently Sat Nov 18, 2017 5:53 pm

All times are UTC - 7 hours




Post new topic Reply to topic  [ 30 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Files
PostPosted: Fri May 05, 2006 4:12 pm 
Offline

Joined: Mon May 01, 2006 3:27 pm
Posts: 60
Where can I find infomation on creating, open and close files for use in my project ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 6:46 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
The Qlarity Program Reference, chater 4.16 describes the file system functions. Most of the information can be found in the object documentation (press F1 in Qlarity Foundry) under the topic File System.



All of your basic file system operations are supported. Some advanced operations, such as user permissions and file locking, don't make sense for the Qlarity platform and are not supported.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 7:33 am 
Offline
User avatar

Joined: Thu Mar 02, 2006 2:12 pm
Posts: 487
Location: Salt Lake City, Utah
If you look up the File System API's under object documentation (F1) you will also find example code for each function.



You may also find the FileObjectV2 and PersistenVariablesV2 objects easier to use than the core API functions.

_________________
Ron L.

http://www.beijerelectronicsinc.com/


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 9:12 am 
Offline

Joined: Mon May 01, 2006 3:27 pm
Posts: 60
[Administrator Edit: Reformatted the call to WriteFile to make the post more readable]



I was able to create a file called ProjectFile.dat

I assime this is stored as a string ? Now I need to read the file and convert to an Array. How can I convert the Data ?











'create and open a file for writing

fnum = OpenFile("ProjectFile.dat", FILE_BINARY and FILE_WRITE)

'write string to file

WriteFile(fnum, "0,1,2,3,4,5,6,6,6,6,6,6,6,6,6,6,6," + ->

"6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6," + ->

"6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6," + ->

"6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6," + ->

"6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6," + ->

"6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6," + ->

"6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6")



'close file

CloseFile(fnum)







'open file for reading

fnum = OpenFile("ProjectFile.dat", FILE_BINARY and FILE_READ)

'get file size

SetFilePos(fnum, -1, true)

fsize = GetFilePos(fnum)

SetFilePos(fnum, 0, true)

'resize data to store string from file



ReadFile(fnum, data)





label_6.value=str(data) 'Just to View



for i = 0 to 17

RED[i]=data[i]

GREEN[i]=data[i]

BLUE[i]=data[i]

next


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 9:34 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Well, the first thing to remember is that in Qlarity Arrays and Strings are the same thing.



Normally when writing an array of data to a binary file, you should also write the number of elements you plan to write.



Code:

func WriteData()

    dim chunk as string

    init chunk := "0,1,2,3,4,5,6,7,8,6,6,6,6" 'This string can be of arbitrary length

    dim desc as fileDesc



    desc = OpenFile ("ProjectFile.dat", FILE_BINARY and FILE_WRITE)

    WriteFile(desc, len(chunk)) 'Write the number of elements in chunk

    WriteFile(desc, chunk)

    closeFile(desc

endfunc



func ReadData()

    dim chunk as string

    dim length as integer



    desc = OpenFile("ProjectFile.dat", FILE_BINARY and FILE_READ)

    readFile(desc, length)

   

    'Resize chunk to the proper size.  When ReadFile is called with an array

    'it attempts to read each element of the array from the file.  Therefore

    'you must size the array to the proper size before calling ReadFile

    Redim(chunk, length)

   

    'Read the value

    ReadFile(desc, chunk)



    'Now no translation is needed.  Calling str(chunk) is superfulous and

    'and will translate chunk into something you don't want here.

    label_6.value = data



   'The FOR loop you included will almost certainly NOT do what you want

   'It will NOT store a value 0 in RED[0], instead it will store 48 (the

   'ASCII value of 0)

endfunc





If you are looking to simply save the values of the RED, GREEN and BLUE arrays, you might consider using the PersistantVariables object which does exactly that. If you really want to store those arrays and they are always 17 elements long, the really trivial code to do that is:



Code:

func WriteData()

    dim desc as fileDesc



    desc = OpenFile("ProjectFile.dat", FILE_BINARY AND FILE_WRITE)

    WriteFile (desc, Red)

    WriteFile (desc, Green)

    WriteFile (desc, Blue)



    CloseFile(Desc)

endfunc



func ReadData()

    dim desc as fileDesc



    desc = OpenFile("ProjectFile.dat", FILE_BINARY AND FILE_WRITE)

    ReadFile (desc, Red)

    ReadFile (desc, Green)

    ReadFile (desc, Blue)



    'Str will take the arrays and convert them into a readable text

    'format.

    label_6.value = str(red) + "\n" + str(Green) + "\n" + str(blue)

endfunc


_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 11:55 am 
Offline

Joined: Mon May 01, 2006 3:27 pm
Posts: 60
Thanks you I will give that a try.



Jerry


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 5:46 pm 
Offline

Joined: Mon May 01, 2006 3:27 pm
Posts: 60
How do I split the string up



RED="1,2,3,4,5,6"



This is what I need



RED(0)=1

RED(1)=2

RED(2)=3

RED(3)=4

RED(4)=5

RED(5)=6


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 7:04 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
If I understand correctly, you have a comma separated string and you wish to take each item in the string, convert it to an integer then store that integer in sequential elements in an array.



Well, if every statement in that previous paragraph is trueAND you know that the string you have also contains exactly the right number of items, there is a handy shortcut you can use:

Code:

dim stringSrc as string

init stringSrc = "1,2,3,4,5,6"



'This will throw an exeception if stringSrc is not a comma seperated

'list of numeric values

val(red, "[" +  stringSrc + "]")



The preceeding works because by surrounding the string with square brackets you make the string look like an array initialization and the Val API understands array initialization strings.



For reference, here is how you would manually parse the same string

Code:

func ParseAString()

    dim pos as integer

    dim oldPos as integer

    dim index as integer

    dim separator as string

    init separator := ","

    dim data as string

    init data := "1,2,3,4,5,6"



    'parse for commas (separator)

    pos := find (data, 0, -1, separator)

    while pos >= 0 do

        check error

            val(RED[index], mid(data, oldPos, pos-oldPos))

        on error

            'By not adding exception handling code here, we will allow parsing to

            'continue, BUT will cause the exception to be rethrown later.

            'We add a dummy statement to keep the compiler from warning us about

            'an empty control block

            index = index

        enderr

        oldPos := pos+len(separator)

        pos := find (data, oldPos, -1, separator)

        index := index + 1

    loop



    'add remainder (anything after the last comma) --

    'If your protocol adds a comma (separator) after the last data item, then you can omit

    'this step

    if oldPos < len(data) then

        RED[index] = mid (data, oldPos, -1)

    endif

endfunc


_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 8:14 am 
While we are on files.....



I had a hell of time figuring out what I was doing. I guess I am a bit spoiled with Win32 API.



My problem was I was trying (not actual calls):

WriteInt(MyInt, 1)

WriteInt(MyInt2, 1) <-- should append to file according to docs.





But in reality, I had to manually do the spacing between data for example,

WriteInt(MyInt, 0) <-- begin file

WriteInt(MyInt2, 4) <-- 4bytes

WriteInt(MyInt3, 8) <---4 bytes





I was expecting that when I write an integer, the FileObject knows that an integer is 4 bytes, and would automatically update the fileposition. That isnt case however.







I just wanted to throw that on the table because I spent some time beating the keys till I got it working. I tried using the EmptyOnOpen however that had negative results for me. Does that erase a file everytime its opened? What if I want to read it?


Top
  
 
 Post subject:
PostPosted: Tue May 09, 2006 8:23 am 
Offline

Joined: Mon May 01, 2006 3:27 pm
Posts: 60
Im lost I tried the code as sampled in the last thread but come up with error. I am foggy on the Str , Array thing.



Error is at label_7.value = str(RED[0]) + str(RED[1]) + str(RED[2])





func ReadData()

dim fnum as fileDesc



fnum = OpenFile("ProjectFile.dat", FILE_BINARY AND FILE_READ)



ReadFile(fnum, RED_Data)



CloseFile(fnum)



val(red, "[" + RED_Data + "]")



label_7.value = str(RED[0]) + str(RED[1]) + str(RED[2])

endfunc


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 8:26 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Cloud: What you experienced may be an artifict of the FileObject object. Unfortunately, I don't have experience with the FileObject -- I usually use the raw API (which I find friendly enough), or I use the persistent variables object when I simply want to save variable states.



I am going to have to defer discussion of the FileObject to Ron (who is not in today). I do know that the raw file APIs are smart enough to advance the file position automatically when you write a value.



I just took a second glance at your code, cloud, I think you have it slightly wrong. What you wanted to do is the following:



Code:

    FileObject_1.Write_Integer(MyInt, 0)

    FileObject_1.Write_Integer(MyInt, -1) '-1 (not 1) appends.


_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 8:43 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Colormaker,



Ok, I am going to play a hunch here -- I suspect the error in your code may be in a portion that you did not provide.



The line

Code:

ReadFile(fnum, RED_Data)



will read enough lines from the file to fill the array RED_Data. (I am assuming that RED_Data is declared as a string or array of bytes.). By default, RED_Data will be a zero byte array. This means that the ReadFile line will read zero bytes from the file.



If the file contained the string "1,2,3,4,5,6" (as you indicated earlier), you would want to do the following:



Code:

redim(RED_Data, 11)

ReadFile(fnum, RED_DATA)





This should correct your problem.







P.S. In the future, if you could surround any code you post with a Code block (use the "Code" button above the editor), it would make reading your code samples a lot easier as it will preserve formatting.

_________________
Jeremy
http://www.beijerinc.com


Last edited by Jeremy on Tue May 09, 2006 8:49 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 8:47 am 
Another big AHA! Well that would certainly explain why it didn't work as intended. I will try that. Thanks alot.


Top
  
 
 Post subject:
PostPosted: Tue May 09, 2006 8:58 am 
Offline

Joined: Mon May 01, 2006 3:27 pm
Posts: 60
No good still errors.



Lets scratch to string thing "1,2,3,4,"

Is there anyway to write and read file data using integers



Code:





for I =0 to 50

WriteFile(RED[i])

next







Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 9:12 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Colormaker,



Yes. Your code should work. I would also like to direct you to the second code sample in my second reply to this topic which I think does exactly what you want, or with minor edits could do what you want.



When writing an array to a binary file, you can write each element individually or you can write the entire thing at once.



If you are really just trying to save the state of one or more variables to be restored at a later time, you might really want to consider the PersistentVariablesV2 object which is designed to do exactly that and takes the guesswork out of everything.



You might also consider using a text mode file. When using files opened for text reading and writing, you don't have to worry about converting your data to a string and back again. The API takes care of that for you. What you gain, is a "record" based access with some minimal type safety. For instance, you don't have to resize an array prior to calling ReadFile, the API will take care of that for you. You call WriteFile and pass in a variable to write a record. You later call ReadFile and pass in a variable (of compatible type) and the variable is filled with whatever you wrote. The disadvantage to text files is that they are slower and consume much more space.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 30 posts ]  Go to page 1, 2  Next

All times are UTC - 7 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group