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)!
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.
*/
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;
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();
}
Hi everyone! I have a major problem! I am trying to use an if statement to do something when the serial data (in the form of a string) equals "received". But whatever I do, the if statement wont respond to ANY change in the serial data. I can send things to the arduino just fine, but receiving is the big problem. Also any programming tips are helpful, I'm a noob.
Thanks to anyone who tries to help me! I don't care if you solve the problem or not, just that you try to help means a lot to me :D
Arduino code:
int DataIn = 0; // data coming from computer
int ComStat = 0;
void setup() {
Serial.begin(19200);
// I picked 19200bps (not the problem, I tried 9600) }
void loop() {
if (Serial.read() == 73) {
ComStat = 1;
}
if (Serial.read() == 48) {
ComStat = 0;
}
while (ComStat == 1) {
// The computer won't pick it up at all without it constantly sending data Serial.println("received");
// send "received" to the computer delay(100);
}
}
(I know I'm not very efficient, I need all the help I can get!)
Now here is probably the worst written code in the history of processing!
Processing code:
import processing.serial.*;
Serial Arduino; // named the port "Arduino"
String ConStat = "Connect";
String DataIn;
// global variable for data coming from arduino String GoodIn;
// global variable for processed data (arduino data processed) void setup() {
Arduino = new Serial(this, "COM3", 19200);
// set up port on COM3 Arduino.bufferUntil(10);
size(1000, 600);
background(225);
}
void draw() {
fill(130);
rect(50, 40, 80, 40, 7);
fill(255);
textSize(16);
text(ConStat, 59, 67);
println(GoodIn);
// this is not necessary, just for debugging (doesn't help much)
if (mousePressed == true && mouseX >= 50 && mouseX <= 130 && mouseY >= 40 && mouseY <= 80) {
fill(50);
rect(50, 40, 80, 40, 7);
Arduino.write(73);
//Send capital letter I to arduino. ConStat = "Found";
// indicates that there is something connected to COM3 if (GoodIn == "received") {
// PROBLEM HERE!!! Even though the monitor says that it is = "received", it never changes the outcome of this conditional! ConStat = "Active";
println("Active");
}
}
}
void serialEvent(Serial Arduino) {
// this is where the data processing occurs