I want to send data (G-code) from a Java program on my PC to an Arduino. The communication between the Arduino and my PC works fine because I can send data in in both directions.
If I test the parsing code of my Arduino with the serial monitor of the Arduino I works fine.
If I do the same thing with my Java program, it's not working. In my Java program I receive the data I send to the Arduino but I'm not receiving any parsed data. The problem is, that this is hard to debug because I can't open the serial monitor of the Arduino when I'm communicating with my Java program.
Below you can find my programs:
Arduino:
#include <Arduino.h>
char ch;
const int NUMBER_OF_FIELDS = 6;
int fieldIndex = 0;
double values[NUMBER_OF_FIELDS];
int decimalPoint[NUMBER_OF_FIELDS];
void setup()
{
Serial.begin(9600);
Serial.println("setup");
delay(250);
}
void loop()
{
// G/M position 0 (code)
// X position 1
// Y position 2
// Z position 3
// F feedrate
// E length of filament to consume
// a typical array looks like this:
// [G/M, X, Y, Z, F, E]
if (Serial.available() > 0)
{
ch = Serial.read();
delay(250);
Serial.println(ch);
delay(250);
if (ch == 'G' || ch == 'M') {
fieldIndex = 0;
} else if (ch == 'X') {
fieldIndex = 1;
} else if (ch == 'Y') {
fieldIndex = 2;
} else if (ch == 'Z') {
fieldIndex = 3;
} else if (ch == 'F') {
fieldIndex = 4;
} else if (ch == 'E') {
fieldIndex = 5;
} else if (ch >= '0' && ch <='9') {
values[fieldIndex] = values[fieldIndex] * 10 + (ch - '0');
if (decimalPoint[fieldIndex] > 0) decimalPoint[fieldIndex] *= 10;
} else if (ch == '.') {
decimalPoint[fieldIndex]++;
} else if (ch == ' ') {
if(fieldIndex < NUMBER_OF_FIELDS -1);
fieldIndex++;
} else {
delay(250);
Serial.print(fieldIndex + 1);
delay(250);
Serial.println(" field received");
delay(250);
for (int i = 0; i <= fieldIndex; i++) {
if (decimalPoint[i] > 0) {
Serial.println(values[i] / decimalPoint[i]);
delay(250);
} else {
Serial.println(values[i]);
delay(250);
}
values[i] = 0;
decimalPoint[i] = 0;
}
fieldIndex = 0;
}
}
}
Java
package communicationArduino;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.fazecast.jSerialComm.SerialPort;
public class Main {
static SerialPort chosenPort;
static int x = 0;
public static void main(String[] args) {
// create and configure the window
JFrame window = new JFrame();
window.setTitle("Sensor Graph GUI");
window.setSize(600, 400);
window.setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create a drop-down box and connect button, then place them at the top of the window
JComboBox<String> portList = new JComboBox<String>();
JButton connectButton = new JButton("Connect");
JPanel topPanel = new JPanel();
topPanel.add(portList);
topPanel.add(connectButton);
window.add(topPanel, BorderLayout.NORTH);
// populate the drop-down box
SerialPort[] portNames = SerialPort.getCommPorts();
for(int i = 0; i < portNames.length; i++)
portList.addItem(portNames[i].getSystemPortName());
// configure the connect button and use another thread to listen for data
connectButton.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent arg0) {
if(connectButton.getText().equals("Connect")) {
// attempt to connect to the serial port
chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());
chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
if(chosenPort.openPort()) {
connectButton.setText("Disconnect");
portList.setEnabled(false);
}
// create a new thread that listens for incoming text and populates the graph
Thread thread = new Thread(){
@Override public void run() {
System.out.println("connected read");
Scanner scanner = new Scanner(chosenPort.getInputStream());
while(scanner.hasNextLine()) {
try {
String line = scanner.nextLine();
System.out.println(line);
window.repaint();
} catch(Exception e) {}
}
scanner.close();
}
};
thread.start();
// create a new thread that listens for incoming text and populates the graph
Thread thread2 = new Thread(){
@Override public void run() {
System.out.println("connected write");
Scanner scan = new Scanner(System.in);
String input = "";
PrintWriter output = new PrintWriter(chosenPort.getOutputStream());
while (true) {
input = scan.nextLine();
output.print(input);
output.flush();
}
// output.close();
}
};
thread2.start();
} else {
// disconnect from the serial port
chosenPort.closePort();
portList.setEnabled(true);
connectButton.setText("Connect");
x = 0;
}
}
});
// show the window
window.setVisible(true);
}
}