View previous topic :: View next topic |
Author |
Message |
njepsen
Joined: 13 Aug 2007 Posts: 472

|
Posted: Fri Sep 18, 2015 3:26 am Post subject: PUT |
|
|
After struggling to find exactly how "PUT" works with a file, I have come to the following conclusions after some testing.
In relation to the little test program below. ( I have an SD card connected to MISO & MOSI and
am writing and reading from the card )
Code: |
dim myfilename as fred.txt" 'file name
dim mytxt as string*20 'some random text
dim flength as long 'file length
dim n as long 'pointer
dim tempb as byte 'temporary byte
do
chdir "\" 'to root dir
open myfilename for binary as #128
'flength = filelen(myfilename)+1
mytext = "hello neil"
put #128 , mytxt
mytxt = "goodby"
put #128 , mytxt
flush #128
close #128
waitms 5000
'now see what has been written
print #1 , ""
print #1 , "reading file"
open myfilename for binary as #128
flength = filelen(myfilename)
for n = 1 to flength
get #128 , tempb , n
print #1 , "var=" ; chr(tempb)
next
close #128
loop
|
This program will write "hello neil" followed by 10 empty locations at the start of the file each time it goes through the loop, and it will append "goodby" followed by 14 empty cells after the word "goodbye"
Lesson:
PUT will overwrite the file the first time it is used after the file is opened.
Subsequent PUT commands will append to the file.
When the variable is a string, the number of bytes that will be written is equal to the dimensioned size of the string, and not how many bytes you write to the string. (this is in the help).
Appending.
If I un-comment the line "flength = filelen(myfilename)+1 " and change the next line to
Code: |
put #128 , mytxt, flength
|
then each loop will append to the file, and not overwrite it. _________________ Neil
Last edited by njepsen on Fri Sep 18, 2015 9:59 am; edited 1 time in total |
|
Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6245 Location: Holland

|
Posted: Fri Sep 18, 2015 9:41 am Post subject: |
|
|
Did you check the table in the 'AVR-DOS File System' topic? I mean : 'Validity of the file I/O operations regarding the opening modes'
Some statements are only intended to work with certain modes. put and get will only work in binary mode.
they might work in other modes too, but the results may be unexpected.
AVR-DOS and the help were written by Josef a long time ago. At that time, we assumed that all bascom users would be familiar with QB/VB file handling. As a result the help covers the basic description. I will have a look at that.
Append is typically used for text files where you use print.
The reason why PUT writes the complete string is that put/get can read data just by specifying the index. that would not be possible if all strings would have a different length. _________________ Mark |
|
Back to top |
|
 |
njepsen
Joined: 13 Aug 2007 Posts: 472

|
Posted: Fri Sep 18, 2015 9:57 am Post subject: |
|
|
Hi mark. Actually that was a typo. My testing was done with open for binary . The prog will not run with append.
I have edited the post.
Cheers _________________ Neil |
|
Back to top |
|
 |
njepsen
Joined: 13 Aug 2007 Posts: 472

|
Posted: Sun Sep 14, 2025 2:46 am Post subject: |
|
|
10 years later.
I have a new project and decided that my undertstanding of AVR-DOS commands was not up to par.
The following test program tests the operation of PUT, SEEK,LOC, and LOF and the results are interesting.
Code: |
$regfile = "m1284pdef.dat"
'$regfile = "m1284def.dat"
$crystal = 9830400 '10000000
$framesize = 500 ' This lib only uses ~10 $hwstack - please see the application note for more information
$hwstack = 500 ' This lib only uses ~10 $hwstack - please see the application note for more information
$swstack = 500 ' This lib only uses ~10 $hwstack - please see the application note for more information
$PROG &HEF,&HC7,&HD0,&HFD
,---------------------------------------------------
'Set up watchdog
'--------------------
config watchdog = 8192 '8 secs
start watchdog
'----------------------------------
'Declare variables
'-----------------------------------
'dim dtstamp as string *16
dim f as byte
dim index as byte
Dim n As word
dim k as dword
dim filenamestr as string * 25
dim writeposn as long
dim dstr1 as string * 100
'------------------------------------------
'Declare constants
'----------------------------------------------
declare Sub Save_to_sdcard1()
' '-------------------------------------------------------------------------------
'Open a software UART TRANSMIT channel for debug dB9
'-------------------------------------------------------------------------------
'Open a software UART TRANSMIT channel for debug dB9
Open "comc.3:38400,8,n,1" For Output As #1 '
'------------------------------------------
'Setup ports
'------------------------------------------
Heartbeatled Alias Portb.1 'heartbeat led
Config Heartbeatled = Output
'*******************************************
'MAIN LOOP
'*******************************************
startup:
'set up SD card
$include "config_mmcsd_hc.inc" 'set up the high capacity SD card comms
F = driveinit()
If F <> 0 Then
Print "SD init error"
End
else
print #1, "Good drive" 'we can now install DOS
End If
' Mount filesystem
$include "config_AVR-DOS.inc"
F = Initfilesystem(1)
If F <> 0 Then
Print "FAT init error"
End
else
print #1,"good mount. SD card all good"
End If
k = 0
filenamestr = "name1.txt"
do
reset watchdog
waitms 250
'change file name every 50 secs, create and write to a new file name
incr k
n = k mod 100
if n = 0 then
filenamestr = "new file name" + str(k) + ".txt"
filenamestr = right(filenamestr,12) 'maintain short name 8.3
end if
call save_to_sdcard1()
toggle portB.1 'flash a led
waitms 250
loop
'***************************************************************************
'***********************************************************************
Sub Save_to_sdcard1()
disable interrupts
Chdir "\"
dstr1 = "_hello_" + str(k) + "," 'dstr1 = payload
open filenamestr for binary As #128 'fist write will overwrite the file
print #1,"myfilenamestr=";filenamestr
print #1,"seek before PUT =";seek(#128)
print #1,"lof before PUT =";lof(#128)
print #1,"loc before PUT =";loc(#128)
put #128,dstr1,writeposn,len(dstr1)
print #1,"seek after PUT =";seek(#128)
print #1,"lof after PUT =";lof(#128)
print #1,"loc after PUT =";loc(#128)
writeposn = seek(#128) 'find where to append fotr the next PUT operation
print #1, "k= ";k
print #1, "filelength = ";filelen(filenamestr) 'just checking
Flush #128
Close #128
enable interrupts
Print #1 , "{027}[92m";">> Saving to SD done"; "{027}[0m" 'green
Print #1 , ""
Chdir "\"
exit sub
End Sub
'****************************************************************************
|
A printout of the debug looks like this, at the point where the file name changes.
Quote: |
myfilenamestr= name800.txt
seek before PUT =1
lof before PUT =1089
loc before PUT =0
seek after PUT =1101
lof after PUT =1100
loc after PUT =1100
k= 899
filelength = 1089
>> Saving to SD done
myfilenamestr= name900.txt
seek before PUT =1
lof before PUT =0
loc before PUT =0
seek after PUT =12
lof after PUT =11
loc after PUT =11
k= 900
filelength = 0
>> Saving to SD done
|
What this shows is:
1. if SEEK() and LOC() are read after the open command but before the PUT command, the results are not valid, and SEEK() and LOC() must be used after the PUT command.
2. LOF(#128) and FILELEN(myfilename) give the same result, which I would expect but had to check.
3. SEEK() = LOF + 1 if read after the PUT
4. SEEK = LOC +1 if read after the PUT
5. PUT(#128),dstr1,SEEK(#128) will write a file entry that is the dim'd length of dstr1 even if half of dstr1 is empty
6. PUT(#128),dstr1,SEEK(#128),LOF(#128) will write a file entry that is equal to the length of the data in dstr1 and not the dim'd length. _________________ Neil |
|
Back to top |
|
 |
|
|
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 vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|