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.

Resending files via FTP


zhangyp Jul 1, 2013 10:36 AM

Hello everyone:
I use Tablefile instruction to backup data to CRD drive,and also send it via FTPClient.

Problem is, some of the sites have intermittant 3G coverage so often the file cannot be sent. If it still can't be sent by the time the next file is created. so some files will be missed.

I noticed that FTPClient instruction's help document has a example program about how to manage resending files if an FTPClient transfer were to fail. I refered it , and use following code to test:

PipeLineMode

'The following code is an example of how to manage resending files if an FTPClient transfer were to fail.
Public Batt_V,Panel_T
Public TC(8)
Const Sitename = "ChangBS"
Const Max_files = 200
Units TC = C


Public OutStat As Boolean, FileNames As String * 64, public destfilename As String * 64

Public wr As Long = 1, rd As Long = 1 'indeces into array of names
Public transmit As Boolean
Public success As Boolean


DataTable(Table1,TRUE,-1)
DataInterval (0,2,Sec,10)
TableFile("CRD:RootName",8,-1,0,30,Sec,OutStat,FileNames)
Average (8,TC(),FP2,False)
Sample (1,Batt_V,FP2)
Sample (1,Panel_T,FP2)
EndTable

BeginProg

Scan (2,Sec,3,0)

Battery (Batt_V)
PanelTemp (Panel_T,250)

TCDiff (TC(),8,mV2_5C,1,TypeT,Panel_T,True ,0,250,1.0,0)

CallTable(Table1)

If OutStat = true Then transmit = true

NextScan

SlowSequence
Scan (30,Sec,3,0)

If transmit = true Then wr = (wr MOD Max_files) + 1

If rd <> wr Then

destfilename = Sitename + FileNames

'advance to next file if successfully sent via FTP

success = FTPClient ("192.168.1.221","jason","123456",FileNames,destfilename,0)

If success = TRUE Then

rd = (rd MOD Max_files) + 1

EndIf

EndIf

NextScan

EndProg


After test, i found this program can't realize resending files when communication restored. It only sending the new files.


zhangyp Jul 1, 2013 10:38 AM

Anyone help me to modify the program or have other good method for resending multiple files?


aps Jul 1, 2013 10:52 AM

Please get the latest logger OS and run the downloaded file which will upgrade your CRBasic editor files. Then look at the description of the new options for the FTPClient instruction. It can be used to automatically send files (and queue the requests) without using and being independent of any files stored on the USR drive.


Sam Jul 1, 2013 03:20 PM

The new "streaming" options for FTPClient() are quite nice. I like to think about it as TableFile() with the destination being the FTP servers instead of a local drive.

With that said here is an example I wrote for a training class recently.

------------------------------
FTP_BufferNameNoDelete.CR1
------------------------------

Public PTemp, batt_volt

'TABLE FILE DECLARATIONS
Public OutStat As Boolean
Public LastFileName As String * 31

'SERVER DECLARATIONS
Const Host = "ftp.server.com"
Const User = "user"
Const Pass = "pass"
Const RDir = "/"
Public LName As String * 31
Public RName As String * 31
Public Success As Boolean

'FILE QUEUE DECLARATIONS
Const QL = 10 'queue buffer size
Public Queue(QL) As String * 31

' SUB ROUTINE FOR ADDING TO QUEUE
Sub QueueAdd()
Dim I
If OutStat Then 'add new file to queue
For I = 1 To QL 'find an empty spot
If Queue(I) = "" Then
Queue(I) = LastFileName
ExitFor 'queued so exit
EndIf 'end if queue
Next I 'end for I
If I > QL Then 'there wasn't room, make some and add
Move (Queue(1),QL-1,Queue(2),QL-1) 'shift queued names down - 2 to 1, 3 to 2, 4 to 3, etc.
Queue(QL) = LastFileName
EndIf
EndIf 'end if outstat
EndSub

'SUB ROUTINE FOR REMOVING OLDEST ITEM IN QUEUE
Sub QueueRemove()
Move (Queue(1),QL-1,Queue(2),QL-1) 'shift queued names down - 2 to 1, 3 to 2, 4 to 3, etc.
Queue(QL) = "" 'clear trailing name
EndSub

'DATA TABLES
DataTable (Test,1,1000)
DataInterval (0,1,Sec,10)
TableFile ("USR:Test_",8,QL,0,30,Sec,OutStat,LastFileName)
Sample (1,batt_volt,FP2)
Sample (1,PTemp,FP2)
Call QueueAdd()
EndTable

DataTable(TxLog,1,1000)
Sample (1,Success,Boolean)
Sample (1,LName,String)
Sample (1,RName,String)
EndTable

'MAIN PROGRAM
BeginProg
SetStatus("USRDriveSize",1000000)

Scan (1,Sec,0,0)
PanelTemp (PTemp,250)
Battery (batt_volt)
CallTable Test
NextScan

SlowSequence
Do While TRUE
Do While (Queue(1) <> "") 'If queue is not empty
LName = Queue(1)
RName = RDir + Replace (Queue(1),"USR:","")
If FileTime(LName) > 0 Then 'if file exists
Success = FALSE
Success = FTPClient (Host,User,Pass,LName,RName,2) 'attempt to ftp
CallTable (TxLog) 'diagnostics table
Delay (1,4,Sec) 'pause after attempt, yes this is a good idea
If Success Then Call QueueRemove() Else ExitDo 'send succeeded or failed, if failed exit and try later
Else 'file does not exist
Call QueueRemove()
EndIf 'end if filetime
Loop 'end do while queue
Delay (1,10,Sec) 'Control SlowSequence Execution Frequency
Loop 'end do while true
EndSequence

EndProg

* Last updated by: Sam on 7/1/2013 @ 3:20 PM *


zhangyp Jul 17, 2013 09:35 PM

Thank you :

I have test the latest OS, the new option of the FTPClient instruction is nice, now the resending function is achieved.

And I found a new problem about CR3000. CR1000(OS26) send FTP file is normal,the file contain data record. but CR3000(OS26) send the "empty" file. the file size is 0KB and there is not records in the file. CR3000's program same as CR1000, and i also used a mini program to test, the problem still.

The CR3000's SN is 8172, OS version is std 26 . I don't know why.


GaryTRoberts Jul 21, 2013 09:19 PM

If you are using Active FTP, switch to Passive FTP. This could also indicate a firewall issue if you are already sending files via Passive FTP.


loggerhead Jun 30, 2015 09:02 AM

Hi Sam. I am using your FTP sample for one of my CR3000 logger programs. The program, with two data tables, sends data collected from an instrument to an FTP server.
The problem that I am having is that only one of my data tables is being sent (100mSec interval) while the other one (1 min interval) does not. The data files for the second tables remain in the queue.
Here is ow I modified your program

'TABLE FILE DECLARATIONS
Public OutStat As Boolean
Public LastFileName As String * 31

Public OutStat2 As Boolean
Public LastFileName2 As String * 31


'SERVER DECLARATIONS
Const Host = "ftp.server.com"
Const User = "user"
Const Pass = "pass"
Const RDir = "/"
Const RDir2 = "/"
Public LName As String * 31
Public RName As String * 31
Public Success As Boolean

Public LName2 As String * 31
Public RName2 As String * 31
Public Success2 As Boolean

'FILE QUEUE DECLARATIONS
Const QL = 10 'queue buffer size
Public Queue(QL) As String * 31

'FILE QUEUE2 DECLARATIONS
Const QL2 = 10 'queue buffer size
Public Queue2(QL2) As String * 31

' SUB ROUTINE FOR ADDING TO QUEUE
Sub QueueAdd()
Dim I
If OutStat .....
EndSub

'SUB ROUTINE FOR REMOVING OLDEST ITEM IN QUEUE
Sub QueueRemove()
.....
EndSub

' SUB ROUTINE FOR ADDING TO QUEUE2
Sub QueueAdd2()
Dim I2
If OutStat2 .....
EndSub

'SUB ROUTINE FOR REMOVING OLDEST ITEM IN QUEUE2
Sub QueueRemove2()
.....
EndSub

'DATA TABLES
DataTable (Test,1,1000)
DataInterval (0,100,mSec,10)
TableFile ("USR:Test_",8,QL,0,30,Sec,OutStat,LastFileName)
Sample (1,batt_volt,FP2)
Sample (1,PTemp,FP2)
......
Call QueueAdd()
EndTable

DataTable (Test2,1,1000)
DataInterval (0,1,Sec,10)
TableFile ("USR:Test2_",8,QL2,0,30,Sec,OutStat2,LastFileName2)
Average (1,batt_volt,FP2)
Average (1,PTemp,FP2)
......
Call QueueAdd()
EndTable

'MAIN PROGRAM
BeginProg
SetStatus("USRDriveSize",1000000)

Scan (100,mSec,0,0)
PanelTemp (PTemp,250)
Battery (batt_volt)
CallTable Test
CallTable Test2
NextScan

SlowSequence
Do While TRUE
Do While (Queue(1) <> "") 'If queue is not empty
LName = Queue(1)
RName = RDir + Replace (Queue(1),"USR:","")
If FileTime(LName) > 0 Then 'if file exists
Success = FALSE
Success = FTPClient (Host,User,Pass,LName,RName,2) 'attempt to ftp
'CallTable (TxLog) 'diagnostics table
Delay (1,4,Sec) 'pause after attempt, yes this is a good idea
If Success Then Call QueueRemove() Else ExitDo 'send succeeded or failed, if failed exit and try later
Else 'file does not exist
Call QueueRemove()
EndIf 'end if filetime
Loop 'end do while queue
Delay (1,10,Sec) 'Control SlowSequence Execution Frequency
Loop 'end do while true

'SECOND TABLE FILE
Do While TRUE
Do While (Queue2(1) <> "") 'If queue is not empty
LName2 = Queue2(1)
RName2 = RDir2 + Replace(Queue2(1),"CRD:","")
'RName = Replace(Queue(1),"CRD:","")
If FileTime(LName2) > 0 Then 'if file exists
Success2 = FALSE
Success2 = FTPClient (Host,User,Pass,LName2,RName2,2) 'attempt to ftp
'CallTable (TxLog) 'diagnostics table
'Delay (1,4,Sec) 'pause after attempt, yes this is a good idea
Delay (1,4,Sec) 'pause after attempt, yes this is a good idea
If Success2 Then Call QueueRemove2() Else ExitDo 'send succeeded or failed, if failed exit and try later
Else 'file does not exist
Call QueueRemove2()
EndIf 'end if filetime
Loop 'end do while queue
Delay (1,10,Sec) 'Control SlowSequence Execution Frequency
Loop 'end do while true

EndSequence

EndProg

Where am I going wrong with the second data table? I can see that the files are there on the logger but somehow FTPSuccess 2 always remains false.

* Last updated by: loggerhead on 6/30/2015 @ 9:04 AM *

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