ArrayIndexOutOfBounds error!
in
Integration and Hardware
•
21 days ago
Hi. This is my second problem in my current project, and I hope you can help me solve this one too!
The purpose of the program is to bind to a serial port and communicate with an Arduino board through serial communication. The problem I am having occurs when there are no serial ports open. When that happens I (of course) get an ArrayIndexOutOfBounds error. I have used try / catch and if(index < Array.length) statements. but they don't seem to work with what I'm doing. I may have just been using them wrong though.
Here is what is required for error handling:
- If there is no error, than do the normal sequence.
- If there is an error, don't bind to a port in setup, but wait for one to become available. (hard part).
- Once one becomes available, let the user know, and change ConStat to connect (connection status to "connect").
- Once connect is pressed, bind to that port and send the test bit "I", and wait for response "O".
- Set ConStat to Active (connection status to "Active").
- BUT if the connection it abruptly interrupted (disconnected and port closed), or if the stop command is typed (port closed), it should clear previous serial data and wait for another port to open again and also alert the user of what has happened. (hardest part).
And as always,
ANY HELP AT ALL IS GREATLY APPRECIATED!
Warning! Code is messy!
Here is the code! If you want to test it yourself before replying back, remember to get G4P controls (library)!
---------------------------------------------------
/*
(© 2013 REI)
(© 2013 REI)
This program will attempt to communicate with an Arduino board with "REI serial comm"
software installed on it. Plans for future use involve controlers for robots and
other devices based off Arduino.
*/
software installed on it. Plans for future use involve controlers for robots and
other devices based off Arduino.
*/
import g4p_controls.*; //gui control library (for the text field).
import processing.serial.*;
String Text;
PImage Logo;
color Black = color(1, 1, 1);
color Red = color(255, 60, 60);
color Green = color(10, 150, 10);
Serial Arduino;
String Port = null;
String ConStat = "Connect";
String DataIn;
String GoodIn;
String[] Text1 = null;
GTextField txf1;
import processing.serial.*;
String Text;
PImage Logo;
color Black = color(1, 1, 1);
color Red = color(255, 60, 60);
color Green = color(10, 150, 10);
Serial Arduino;
String Port = null;
String ConStat = "Connect";
String DataIn;
String GoodIn;
String[] Text1 = null;
GTextField txf1;
void setup() {
Arduino = new Serial(this, Serial.list()[0], 19200);
Arduino.bufferUntil(10);
Port = Serial.list()[0];
print("connected to: ");
println(Port);
size(1000, 600);
background(225);
Logo = loadImage("REI logo2.png");
fill(Black);
stroke(Green);
rect(700, 400, 275, 175);
fill(255);
text("Debug Window:", 704, 415);
stroke(0);
G4P.setGlobalColorScheme(GCScheme.RED_SCHEME);
txf1 = new GTextField(this, 145, 50, 200, 20);
txf1.tag = "txf1";
txf1.setDefaultText("Enter Commands here");
}
void draw() {
if (Port != null && Serial.list()[0].equals(Port)==false) {
ConStat = "Retry?";
Arduino.clear();
Arduino.stop();
fill(Black);
rect(701, 420, 273, 154);
fill(Red);
textSize(16);
text("> Error Code: 001", 715, 440);
text(">", 715, 455);
text(">", 715, 470);
textSize(13);
text(" device disconnected from ", 715, 455);
text(Port, 900 , 455);
text(" Defaulting to first port in list", 710, 470);
GoodIn = null;
} else {
if (Port != null && Serial.list()[0].equals(Port)==false) {
ConStat = "Retry?";
Arduino.clear();
Arduino.stop();
fill(Black);
rect(701, 420, 273, 154);
fill(Red);
textSize(16);
text("> Error Code: 001", 715, 440);
text(">", 715, 455);
text(">", 715, 470);
textSize(13);
text(" device disconnected from ", 715, 455);
text(Port, 900 , 455);
text(" Defaulting to first port in list", 710, 470);
GoodIn = null;
} else {
if(ConStat != "Active" && ConStat != "Stopped" && ConStat != "Retry?") {
ConStat = "Connect";
fill(Black);
rect(701, 420, 273, 154);
fill(Green);
textSize(16);
text("> Device connected", 715, 440);
text(">", 715, 455);
textSize(13);
text(" There was a device found", 715, 455);
text(" on , but may not be initalized ", 710, 470);
fill(200);
text(Port, 735, 470);
fill(Green);
text(" properly", 710, 485);
}
ConStat = "Connect";
fill(Black);
rect(701, 420, 273, 154);
fill(Green);
textSize(16);
text("> Device connected", 715, 440);
text(">", 715, 455);
textSize(13);
text(" There was a device found", 715, 455);
text(" on , but may not be initalized ", 710, 470);
fill(200);
text(Port, 735, 470);
fill(Green);
text(" properly", 710, 485);
}
}
fill(100); //create the Enter button.
rect(352, 50, 60, 20, 10);
fill(255);
textSize(13); //text for the Enter button.
text("Enter", 365, 66);
scale(0.5,0.5); //set scale for the Logo to 50%.
image(Logo, 1, 975); //display REI logo.
scale(2.0,2.0); //set scale back to 100%.
fill(225);
stroke(225); //set the boarder of the rect. below to the same as the background.
rect(5, 580, 200, 18); //rect. to backdrop branding text
fill(Black);
textSize(12);
text("(© 2013 REI) Ver.0.0.2 (WIP)", 8, 595);
stroke(0);
fill(130); //create the connect button.
rect(50, 40, 80, 40, 7);
fill(255);
textSize(16); //text for the connect button.
text(ConStat, 59, 67); //display the button with the text in "ConStat".
if (keyPressed == true && key == ENTER || key == RETURN || mousePressed == true && mouseX >= 352 && mouseX <= 432 && mouseY >= 50 && mouseY <= 70) { //if enter button is pressed.
fill(80); //create the Enter button (pressed).
rect(352, 50, 60, 20, 10);
if(Text != null) { //if text field has a valid value.
Text1 = match(Text, "Stop || stop"); //check if "Stop" or "stop" is in the command.
if (Text1[0] != null && ConStat == "Active") {
Arduino.stop();
Arduino = new Serial(this, Serial.list()[0], 19200);
ConStat = "Stopped";
println("Stopped");
fill(Black);
rect(701, 420, 273, 154);
fill(Red);
textSize(15);
text("> Current communication stopped", 715, 440);
text(">", 715, 455);
text(">", 715, 470);
textSize(12);
text(" because you entered the stop command", 715, 455);
textSize(12);
text(" Enter a specific port and press enter.", 710, 470);
}
Text1 = match(Text, "COM"); //check if "COM" is in the command.
if (Text1 != null) {
Arduino.stop(); //stop current communication.
Port = Text;
Arduino = new Serial(this, Port, 19200);
println("Changed");
ConStat = "Connect";
}
} else {
println("Invalid entry");
}
}
if (ConStat != "Stopped" && mousePressed == true && mouseX >= 50 && mouseX <= 130 && mouseY >= 40 && mouseY <= 80) { //if connect button is pressed.
fill(50);
rect(50, 40, 80, 40, 7);
if (GoodIn != null && GoodIn.equals("Connected") == true) {
Arduino.write(73); //Send capital letter I to arduino.
ConStat = "Found";
}
delay(10);
if (GoodIn != null && GoodIn.equals("received") == true) {
ConStat = "Active";
println("Active");
fill(Black);
rect(701, 420, 273, 154);
fill(255);
textSize(16);
fill(Green);
text("> Connected Successfully!", 715, 440);
text("> Don't disconnect!", 715, 455);
}
else {
ConStat = "Retry?";
fill(Black);
rect(701, 420, 273, 154);
fill(Red);
textSize(16);
text("> Error Code: 002", 715, 440);
text(">", 715, 455);
textSize(13);
text(" You may have plugged the device in", 715, 455);
text("at the wrong time, or you have connected", 710, 470);
text("to the wrong port. Type in the correct", 710, 485);
text("Port name and click Enter, then connect.", 710, 500);
}
}
}
void serialEvent(Serial Arduino) {
DataIn = Arduino.readStringUntil('\n');
if (DataIn != null) {
DataIn=trim(DataIn);
GoodIn = DataIn;
}
}
public void handleTextEvents(GEditableTextControl tc, GEvent event) {
Text = tc.getText();
}
if (DataIn != null) {
DataIn=trim(DataIn);
GoodIn = DataIn;
}
}
public void handleTextEvents(GEditableTextControl tc, GEvent event) {
Text = tc.getText();
}
1