This is a follow-on from my previous post. I have still not resolved that issue, so decided to post some code.
Here is a loop, which does a dir(*.*) of a folder on an SD card, and reads a file to a modem for upload to a server. If the upload is successful, the loop then does a dir() to get the next file in the folder and do another upload etc etc .This works fine. If there is a problem with uploading tot he server, an error file is created which is saved to the SD card. ( this is nooped out in the listing below).
[code:1:68377aaf64]
Do
Print #1 , ">> myfilename = " ; Myfilename ; "length=" ; len(myfilename)
If Myfilename = "" Then
Print #1 , ">> no files left to send"
Exit Do 'exit when no files left to send
End If 'This is the normal exit
call Tcp_connect(main_server , 80 , 3) 'www.name, port,retries
If Msg_id <> 0 Then Exit Do 'give up. Not able to connect
Gosub Prepare_to_post
Gosub Read_83file_to_telitmodem
Print #1 , ">> waiting for gudupld" 'gudupld comes from the server
Call Waitfortelitresponse_sec( "GUDUPLD" , 30) 'gudupld from server arrives here
If Msg_id = 0 Then 'good response
Chdir ""
Chdir Cachefoldernamestr
Kill Myfilename 'delete from sd card
Print #1 , ""
Print #1 , ">> Deleting " ; Myfilename
Else
Print #1 , ">> incorrect or no response from server"
error_msg = error_msg + "incorr or no resp frm srver" + Datetimestr + chr(13) + chr(10)
End If
Chdir ""
Chdir Cachefoldernamestr
Myfilename = Dir() 'get the next file in the folder
If Myfilename = "" Then
Print #1 , ">> No files left to send"
if Msg_id = 0 then gosub setmyrtc
Exit Do 'exit when no files left to send
End If
Print #1 , ">> Closing Socket1. wait 30 sec..."
'call write_to_errorlog(Error_msg , "errors.txt") 'copy to error_log & clr msg
Call Timedelay_ms(30000)
loop 'get next file
[/code:1:68377aaf64]
This code runs OK, and if I set the server to bounce the uploads, the correct response "gudupld" is not sent and the loop continues to loop through all the files, trying them one at a time.
Here is the problem: If I add in the commented line [i:68377aaf64]call write_to_errorlog....[/i:68377aaf64] then the error file gets written to the SD card OK, BUT next time I try to upload a file to the server, the file length is shown as zero, and the upload fails.
Here is the subroutine that saves (appends) the error file. [code:1:68377aaf64]
'***********************************************************************
sub Write_to_errorlog(errtext , errorlogmyfilename)
'--------------------------------------------------------------------------------------
'Appends current error to SD card file and creates file if file not exist.
'Datetime in datetimestr
'error is in errtxt
'--------------------------------------------------------------------------------------
gosub sd_error_check 'do driveinit and initfile if reqd
Disable Interrupts
Chdir "" 'to root
Mkdir "Errorlog" 'mk dir if not exist
Chdir ""
Chdir "Errorlog"
Open errorlogfilename For Binary As #128
Filelength = Lof(#128)
Filelength = Filelength + 1 'find where to append
Seek #128 , Filelength 'set pointer
Tempb = Len(errtext)
Put #128 , errtext , , Tempb
Cr = 13 : Lf = 10
Put #128 , Cr , , 1 'CRLF between records
Put #128 , Lf , , 1
Flush #128
Close #128
Print #1 , ">> Entry saved to errorlog "
Print #1 , ""
Chdir "" 'back to root dir
errtext = "" 'erase global (byref used)
Enable Interrupts
end sub
'***********************************************************************[/code:1:68377aaf64]
For some reason, this sub screws up reading the next file on the do...loop, and I have not been able to find out why. Its not the seek, or any of the PUTs.
↧