Send large data from Processing to Arduino via serial port communications

Hi, all,

I am planning to use Arduino Uno to control one LED based on the binary sequence (100011100…) stored in one text file. I have written one Processing code to read and pass the data to the arduino board via serial communication port.

For a short binary sequence length, e.g. 10 or 20 , I can get exactly the output signal from the arduino board’s digital pin. However, once the length increases to more than 64, it seems only the first 64 bit data can be passed via the serial port.

I am aware that the input serial port buffer is 64 byte, so I use a if-else statement in the Processing code to pass only 64 binary data to the serial port, then clear the buffer, then pass another 64 binary data. However, it will not change the output to the one I expect.

One strange thing is that, if I change the baud rate to a low value (e.g. 600) from the standard one (9,600 ), I can get the output signal that is even longer than expected from the arduino board. In practice, I need to send a binary sequence with a length of ~1 million. Is there any efficient way of solving this problem? Or do you have any other recommendations on hardware part which can handle such large data?

I have attached both the Processing and Arduino code, any suggestion will be appreciated. Many thanks in advance.

Funjea

read_data_100bit.pde (2.04 KB)

test.ino (936 Bytes)

100bit.txt (199 Bytes)

import processing.serial.*;
import java.io.*;
int mySwitch=0;
int counter=0;
String [] subtext;
Serial myPort;
//boolean firstContact = false;

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, "COM11", 600);
 //myPort.bufferUntil('\n');
 background(255,0,0);  //Change background to red
}

void draw() {
 if (mousePressed == true)
 {
   if (mySwitch>0)
   {
   /* Rread data from txt or csv file */
   readData("100bit.txt");
   println("Length of the binary sequence is ",subtext.length);
   /*The following switch prevents continuous reading of the text file, until we are ready to read the file again. */
   mySwitch=0; 
  /* Send new data. This for loop will allow new data to be sent to the arduino. */
   for (counter=0;counter<subtext.length;counter++)
   {
     
     if (counter>0 && counter%64==0)
     {
       myPort.clear();
       myPort.write(subtext[counter]);
       println(subtext[counter]);
     }
     else
     {
       myPort.write(subtext[counter]);
       println(subtext[counter]);
     }
     
     //delay(1);
     
   }
   background(0,255,0);
   //delay(1000); 
   }
 }
 else {background(255,0,0);
 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,"\tab");
 }
 }catch(FileNotFoundException e){
 e.printStackTrace();
 }catch(IOException e){
 e.printStackTrace();
 }finally{
 try {
 if (br != null){
 br.close();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
}
int outPin = 2; //define outpin
byte binary_signal;
void setup() {
  Serial.begin(600);
  pinMode(outPin,OUTPUT); //set output pin
}

void loop() {
    /* check if data has been sent from the computer: */
    while (Serial.available()>0) {
    /* read the most recent byte */
    //Serial.println("0");
    binary_signal = Serial.read();
    //You have to subtract '0' from the read Byte to convert from text to a number.
    binary_signal=binary_signal-'0';
 
    //Send LOW signal to the output pin if the byte Read = 0
    if(binary_signal==0){
      //Turn off all LEDS
      digitalWrite(outPin, LOW);
      //Serial.println("0");
      //Serial.println(byteRead);
      delay(10);
      }
      //Send HIGH signal to the output pin if the byte Read = 1
    else{
      digitalWrite(outPin,HIGH); //set the output to high
      delay(5);
      digitalWrite(outPin,LOW); //then set the output to low
      delay(5);
      }
  }
}

In case you cannot download the source codes, I have copy them here. Thanks.

You need what is called handshaking. You send the data in chunks, say 32 bytes at a time. When the arduino has recieved 32 bytes it sends an acknoglement to processing, this can just be a single byte say a letter A. Processing will not send the next chunk of data until it receives the acknowledgment character.