This library allows to transfer files from the embedded SerialFlash to another device or to/from the PC. It provides functions to exchange files between the communication peers via any “Serial” connection e.g. UART, USB or CAN-Bus (CAN2SER).

To use this library

include "FileTransfer.h"
include "SerialFlash.h"

Example

Sending a file to the PC

#include "FileTransfer.h"
#include "SerialFlash.h"
void setup()
{
  SerialFlash.begin();
  while(!Serial);
}
void loop()
{	
  if(i_FileTrans_SendFile("dice-ascii-logo.txt", &Serial, &SerialFlash)== 1)
  {
     delay(1000);
     Serial.println("File sent OK");
  }
}

Receiving a file from the PC

#include "FileTransfer.h"
#include "SerialFlash.h"
void setup()
{
  SerialFlash.begin();
  while(!Serial);
}
void loop()
{
  int i_Ret = 0;
  Serial.println("Sending receive enquiry to PC");	
  do
  {
    i_Ret = i_FileTrans_ReceiveFile(&Serial, &SerialFlash);
  }while( i_Ret != 1 );
  delay(1000);
  Serial.println("\n - DONE -");
}

Some details:

The ‘FileTransfer’ protocol features:

  • CRC checksum
  • data re-transmission
  • block counter checks
  • also transmits the version number of the file

In order to initiate a file transmission, the ‘sender’ has to be started first. Since the protocol is controlled from the receiver side, the transmission will start, when the ‘receiver’ starts sending the ‘enquiry’ signal to the ‘sender’. The timeouts are set by #defines in the FileTransfer.h file. By default, the ‘sender’ waits for 5 sec. for a receiver to be ready. During transmission, the timeout between adjacent transmitted bytes is 1 sec.

Use case examples

  • Downloading data-logging files from a device via CAN-Bus to the PC.
    using the CAN2SER-Monitor on the PC and the CAN2SER-Library on the device
  • Uploading binaries from the PC to the device (eg. firmware updates or applications)
  • Transfer files between devices over the CAN Bus

Find the examples in the Arduino IDE menu:

File -> Examples -> FileTransfer
Files -> Examples -> Console -> Console_Dev

The console application reacts on commands from the CAN2SER tool to initiate the receive or send file process. Find the commands in the CAN2SER-tool “misc” menu.

Further information

Device addressing scheme – inter-node communication background
SerialFlash – Library
CAN2SER – Library
Console – Library