Hallo Leute,
möchte via serielle Schnittstelle mit dem Arduino kommunizieren. Das Processing-Sketch soll den G-Code(im Anhang) einlesen und an den Arduino senden, der den Befehl dann mit der main empfängt und abarbeitet. Das funktioniert mit den zwei ersten G-Codezeilen auch sehr gut, aber sobald die dritte Zeile übertragen und ausgeführt wurde, wird das "goOn" nicht mehr ausgegeben und es wird eine Funktion ausgeführt, die nur einmal im Setup aufgerufen wurde.
Hoffe mir kann jemand helfen. Da ich noch Anfänger bin und mein Programmierstil noch nicht so schön ist, gerne auch Kritik dazu.
Mit freundlichen Grüßen
Janik
ProcessingCode
import processing.serial.*; //import the Serial library
Serial myPort; //the Serial port object
String val;
int i = 0;
// since we're doing serial handshaking,
// we need to check if we've heard from the microcontroller
boolean firstContact = false;
String[] zeilen; //zum Speichern der G-Code Zeilen aus der Text Datei
void setup() {
zeilen = loadStrings("Textdatei.txt");
size(200, 200); //make our canvas 200 x 200 pixels big
// initialize your serial port and set the baud rate to 9600
myPort = new Serial(this, "COM4", 9600);
myPort.bufferUntil('\n');
}
void draw() {
}
void serialEvent( Serial myPort) {
//put the incoming data into a String -
//the '\n' is our end delimiter indicating the end of a complete packet
val = myPort.readStringUntil('\n');
//make sure our data isn't empty before continuing
if (val != null) {
//trim whitespace and formatting characters (like carriage return)
val = trim(val);
//println(val);
//look for our 'A' string to start the handshake
//if it's there, clear the buffer, and send a request for data
if (firstContact == false) {
if (val.equals("B")) {
myPort.clear();
firstContact = true;
myPort.write("B");
println("contact");
}
}
else { //if we've already established contact, keep getting and parsing data
println(val + " 2");
if (mousePressed == true|| val.equals("goOn"))
{ //if we clicked in the window
myPort.write(zeilen[i]); //send a 1
println(val);
//val = null;
i++;
}
// when you've parsed the data you have, ask for more:
//myPort.write("A");
}
}
}
#include <Arduino.h>
#include <Terminal.h>
boolean stop = false;
void establishContact();
String val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13
boolean ledState = LOW; //to toggle our LED
int i = 0;
String zeilen[10];
char command;
Terminal terminal = Terminal();
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600);
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
if (Serial.available() > 0) {
val = Serial.readStringUntil('\n');
//eventuell Funktion die die Leerzeichen kürzt
if (val[0] == 'N')
{
Serial.println("Start GCode");
terminal.inputCommand(val);
Serial.println("goOn");
}
}
else {
Serial.println("Hello"); //send back a hello world
delay(50);
}
}
void establishContact() {
while (Serial.available() <= 0 && stop == false) {
Serial.println("B"); // send a capital A
delay(300);
}
stop = true;
}
Ausgabe:
contact
Hello 2
Hello 2
Hello 2
Hello 2
Hello
Start Gcode 2
X_Achse = 49 2
Y_Achse = 255 2
Z_Achse = 177 2
goOn 2
goOn
Hello 2
Start Gcode 2
X_Achse = 141 2
Y_Achse = 255 2
Z_Achse = 138 2
goOn 2
goOn
Hello 2
Start Gcode 2
B 2
B 2
B 2
B 2
B 2
B 2
B 2
B 2
B 2
B 2
B 2
Textdatei.txt (122 Bytes)