Go Down

Topic: Read/write serially a file from PC to the Arduino. (Read 17133 times) previous topic - next topic

maurolarrat

Hi.

I am trying to read a file (ZIC2410_AES128_SnapV2.4.13.sfi), located at my Computer, trought the Arduino duemilanove RX Serial port, and then send it to the TX arduino port which is connected in RX port of a antenna module.

PC(TX) -> (RX)ARDUINO(TX) -> (RX)module

what i have found in some books and google is a txt file transfering examples, like  this:

Code: [Select]

import processing.serial.*;

int mySwitch=0;
int counter=0;
String [] subtext;
Serial myPort;


void setup(){
  //Create a switch that will control the frequency of text file reads.
  //When mySwitch=1, the program is setup to read the text file.
  //This is turned off when mySwitch = 0
  mySwitch=1;
 
  //Open the serial port for communication with the Arduino
  //Make sure the COM port is correct
  myPort = new Serial(this, "COM6", 9600);
  myPort.bufferUntil('\n');
}

void draw() {
  if (mySwitch>0){
    /*The readData function can be found later in the code.
      This is the call to read a CSV file on the computer hard-drive. */
    readData("D:/mySensorData.txt");
   
    /*The following switch prevents continuous reading of the text file, until
      we are ready to read the file again. */
    mySwitch=0;
  }
  /*Only send new data. This IF statement will allow new data to be sent to
    the arduino. */
  if(counter<subtext.length){
    /* Write the next number to the Serial port and send it to the Arduino
       There will be a delay of half a second before the command is
       sent to turn the LED off : myPort.write('0'); */
    myPort.write(subtext[counter]);
    delay(500);
    myPort.write('0');
    delay(100);
    //Increment the counter so that the next number is sent to the arduino.
    counter++;
  } else{
    //If the text file has run out of numbers, then read the text file again in 5 seconds.
    delay(5000);
    mySwitch=1;
  }



/* The following function will read from a CSV or TXT file */
void readData(String myFileName){
 
  File file=new File(myFileName);
  BufferedReader br=null;
 
  try{
    br=new BufferedReader(new FileReader(file));
    String text=null;
   
    /* keep reading each line until you get to the end of the file */
    while((text=br.readLine())!=null){
      /* Spilt each line up into bits and pieces using a comma as a separator */
      subtext = splitTokens(text,",");
    }
  }catch(FileNotFoundException e){
    e.printStackTrace();
  }catch(IOException e){
    e.printStackTrace();
  }finally{
    try {
      if (br != null){
        br.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}



I know C language and java, but i dont know much about arduino programming stuff.

could you help me with it please?

thanks.

PeterH

The Arduino sketch will be easy - it just needs to initialise the two serial ports, and then wait for input on the RX and immediately send it to the TX.

How you send the file out from the PC is another matter. You could write a client in Processing that sends the file to a COM port, or do it in Visual Basic, or Java, or 'C'/C++ - basically whatever you prefer. Since you're familiar with 'C' and Java you have plenty of options open to you. Or you can use any terminal emulator you like on the PC to send the file.


tkbyd

There's information on such things at...

http://sheepdogguides.com/arduino/art4SeIntro.htm

Jonakale1

#4
Aug 27, 2018, 11:15 am Last Edit: Aug 27, 2018, 11:25 am by Jonakale1 Reason: First attached file failed to upload
I'm trying to design and develop a program that can receive a file from the PC via the USART and upon completion, return the file data with one change implemented. Any help?

DKWatson

@jonakale1 You need to start your own thread son.

There's a must read tutorial here.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

UKHeliBob

Quote
I'm trying to design and develop a program that can receive a file from the PC via the USART and upon completion, return the file data with one change implemented.
How far have you got ?
What is sending the file from the PC ?
How large is the file ?
Can you receive characters from the PC and display them on the Serial monitor ?
Can you receive characters from the PC and display them on the LCD ?
Can you store the characters as they are received ?
How/where will you store them on the Arduino ?
Can you wait for an input on the Arduino and respond to it ?
Can you send characters from the Arduino to the PC ?
What is receiving the file on the PC ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up