Inconsistent results transferring EEprom from Arduino to Processing

I’m trying to setup a processing program to read the eeprom on a pro mini into a gui. My problem is that I’m not able to get the computer and the mini to communicate consistently. Here are the two sketches:

On the Pro Mini:

#include <EEPROM.h>

void setup()
{
  // start serial port at 9600 bps and wait for port to open:
  Serial.begin(57000);

}

int inByte;

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int address = 0;
byte value;

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    //inByte = Serial.read();
    //Serial.println("One way");  


    while(Serial.available() > 0) // Don't read unless
      // there you know there is data
    {
      if(index < 19) // One less than the size of the array
      {
        inChar = Serial.read(); // Read a character
        inData[index] = inChar; // Store it
        index++; // Increment where to write next
        inData[index] = '\0'; // Null terminate the string
      }
    }
    // Now do something with the string (but not using ==)
    if (strcmp(inData, "Read")  == 0){
      Serial.println("SndEprom");
      sendProm();
      
    }

  }
}

void sendProm(){
  for (int i=0; i <= 512; i++){
       // read a byte from the current address of the EEPROM
  value = EEPROM.read(address);
  
  Serial.print(address);
  Serial.print(",");
  Serial.print(value, DEC);
  Serial.println();
  
  // advance to the next address of the EEPROM
  address = address + 1;
  
  // there are only 512 bytes of EEPROM, from 0 to 511, so if we're
  // on address 512, wrap around to address 0
  if (address == 512)
    address = 0;
    
  delay(15);
   } 
   address = 0;
}

Processing Sketch:

// Need G4P library
import g4p_controls.*;
import processing.serial.*;

Serial port;
int val = 0; 
int linefeed = 10;


public void setup() {
  size(480, 320, JAVA2D);
  createGUI();
  customGUI();
  // Place your setup code here
  println(Serial.list());
  port = new Serial(this, Serial.list()[4], 57000);
  
}

public void draw() {
  background(230);
}

// Use this method to add additional statements
// to customise the GUI controls
public void customGUI() {
}

// Called whenever there is something available to read
void serialEvent(Serial port) {
  String myString = port.readStringUntil(linefeed);

  if (myString != null) {
    print(myString);
  }
}

public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:696807:
  println("button1 - GButton event occured " + System.currentTimeMillis()%10000000 );
  port.write("Read");
}

The pro mini will only send the eeprom data about one third of the time. I can see through my ftdi setup that the command is being sent out to the mini. What can I do to get the functions to work reliably on both ends?

Thanks,

Loren

I don't know if makes a difference but

Serial.begin(57000);

probably should be 57200 in both the Arduino and Processing code.

groundfungus:
I don't know if makes a difference but

Serial.begin(57000);

probably should be 57200 in both the Arduino and Processing code.

57200 threw an error in processing but 57600 seems to be working better. Thanks groundfungus. The only little sticky wicket left is how to clear the inData variable once the full message has been received?

Loren

Does inData need to be global? Put the initializer in loop() before you read the serial port to clear it before reading.

The pro mini will only send the eeprom data about one third of the time.

Because it only sends data when it receives “ready” on a single pass through loop. Frankly, I’m surprised that one out of three times that happens. I would have expected far fewer times than that.

What you need to is send “”, and then use code like this to read the serial data:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where it says “Process the packet”, inData will contain “ready” if you sent “”.