Hi, i'm having some trouble setting up a connection and sending data to arduino, here is my code:
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Scanner;
public class Serial extends Thread implements SerialPortEventListener {
SerialPort serialPort;
String str = new String();
private static final String PORT_NAMES[] = {"/dev/tty.usbserial-A9007UX1", "/dev/ttyUSB0", "COM3"};
private InputStream input;
private OutputStream output;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;
private int[] buffer = new int[100];
private int len = 0;
public Seriale() {
CommPortIdentifier portId = null;
System.out.println("Port enumeration...");
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
System.out.println("Completed. Searching for COM3 port...");
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
System.out.println("Completed. ");
if (portId == null) {
System.err.println("Port not found.");
return;
}
System.out.println("Port found. Establishing connection... ");
try {
serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
serialPort.setDTR(false);
serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
input = serialPort.getInputStream();
output = serialPort.getOutputStream();
//serialPort.addEventListener(this);
//serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println("Error connecting.");
e.printStackTrace();
}
System.out.println("Connection established.");
start();
}
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
@Override
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int data = input.read();
if ((data != 10) && (data != 13)) {
buffer[len] = data;
len++;
} else {
for (int i = 0; i < len; i++) {
str = str + (char) buffer[i];
}
System.out.println(str);
str = new String();
len = 0;
input.skip(1);
}
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
new Serial();
}
public void run() {
int i=0;
while (true) {
String data = "A";
System.out.println("Sending data...");
try {
i+=1;
output.write(data.getBytes());
System.out.println("Data sent.");
if(i==4) break;
} catch (IOException ex) {
}
}
}
}
There are no errors shown while connecting and sending data, i also see the RX-TX LEDs on the arduino blinking for 4 times, so i suppose Arduino is receiving all the data i send.
The point is, i've a Stepper motor connected to arduino, and i set up arduino so it rotates 90 degrees clockwise when it receives "A" via serial communication.
All of this works if i use the Arduino Serial Monitor, the stepper rotates 90 degrees clockwise, with this class it seems arduino is receiving and recycling all the data i send. Can anyone help me?