-1
  import processing.serial.*; 
import java.io.*; 
Serial myPort; 
int mySwitch; 
String transmitter[] = new String[1280000]; //initialize array 

void setup(){ 
         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 == 1)
        {       
           readData("input.txt");  
           int index = 0; 
           while(index < 128000) 
           {
             myPort.write(transmitter[index++]);
             if(index%128 == 0)
               delay(100);
           }
        }  

         mySwitch = 0; 
} 


void readData(String myFileName){ 

         File file=new File(myFileName); 
         BufferedReader br=null; 

         try{ 
           br=new BufferedReader(new FileReader(file)); 

           String transmit=null; 
           int i = 0; 
           /* keep reading each line until you get to the end of the file */ 
                while((transmit = br.readLine()) != null) 
                   transmitter[i++] = transmit; 

         }catch(FileNotFoundException e){ e.printStackTrace(); } 

         catch(IOException e){ e.printStackTrace(); } 

         finally{ 
                 try { 
                   if (br != null) br.close();  
                 } 
                 catch (IOException e) { e.printStackTrace(); } 
         } 
}

In the above code, I have an String array and I'm reading lines from a file and saving them as strings. How would I save them as byte/char inside an array of byte/char?

Basically, I have a file of integers on each line, each integer in the range [0, 255]. And I want to read this integer and send it to the Arduino. The Arduino serial.read() function reads a byte at a time. This is the issue I am trying to solve.

Jonathan
  • 264
  • 3
  • 14
  • Hi, please could you give us an idea of what you have tried or written and what your results have been so far. – RSM Apr 13 '16 at 11:25
  • Well I am writing each index in my transmitter array and each index holds a "String" value. Thus, serial.write() will write a string to the serial port. However, I want it to write it as an integer for the purposes of speed and efficiency. – Jonathan Apr 13 '16 at 16:19
  • how are your values arranged on each line of the file? – RSM Apr 13 '16 at 16:43
  • just a number in the range of [0, 255] on each line. No other splits such as comma, etc. – Jonathan Apr 14 '16 at 00:25

1 Answers1

0

I believe I have actually found a very efficient solution to this problem by re-writing the code the following way by using the "Scanner" class and not the "BufferedReader" class. Any input on making this more efficient is welcome!

import processing.serial.*; 
import java.io.*; 
import java.util.*;
Serial myPort; 
int mySwitch; 
Scanner file;
byte transmitter[] = new byte[1280000]; //initialize array 

void setup(){ 
    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 == 1)
    {       
        readData("input.txt");  
        int index = 0; 
        while(index < 128000) 
        {
            myPort.write(transmitter[index++]);
            if(index%128 == 0)
                delay(100);
        }
    }  

    mySwitch = 0; 
} 


void readData(String myFileName){ 
    try{
      file = new Scanner(new File(myFileName));
    }
    catch(Exception e){System.out.println("NOT FOUND!");}

    int i = 0;
    while(file.hasNextByte())
      transmitter[i++] = file.nextByte();

    file.close();
} 
Jonathan
  • 264
  • 3
  • 14