Hello everyone,
I recently tried to create an application that can send code via the serial port to my arduino. Sadly the code doesn't work. I have two seperate JAVA classes: one is called "window" it manages the view and adds some sliders and so on. The other one is called "SerialCom" and shall connect to the specified serial port in windows. As said this doesn't work. I put the code together of some samples and since I'm quiet new to JAVA and RXTX and I don't quiet understand what is wrong with it. Can someone please help me out with this. Heres the "SerialCom" code:
package controlSurface;
import gnu.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
public class SerialCom implements SerialPortEventListener{
Window window = null;
CommPortIdentifier serialPortId;
Enumeration enumComm;
SerialPort serialPort;
OutputStream outputStream;
InputStream inputStream;
Boolean serialPortGeoeffnet = false;
int baudrate = 9600;
int dataBits = SerialPort.DATABITS_8;
int stopBits = SerialPort.STOPBITS_1;
int parity = SerialPort.PARITY_NONE;
String portName = "COM5";
Boolean foundPort = false;
public void searchForPorts()
{
enumComm = CommPortIdentifier.getPortIdentifiers();
while (enumComm.hasMoreElements())
{
serialPortId = (CommPortIdentifier)enumComm.nextElement();
//get only serial ports
if (portName.contentEquals(serialPortId.getName()))
{
window.setDebug(portName);
foundPort = true;
break;
}
}
}
public void connect(){
portName = "COM5";
//window.setDebug(portName);
if(foundPort != true){
//window.setDebug("Port not found!\n");
}else{
try {
serialPortId = CommPortIdentifier.getPortIdentifier(portName);
serialPort = (SerialPort) serialPortId.open("Opening port.", 500);
serialPort.setSerialPortParams(
baudrate,
dataBits,
stopBits,
parity);
serialPort.setFlowControlMode(
SerialPort.FLOWCONTROL_NONE);
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
} catch (NoSuchPortException e1) {
// TODO Auto-generated catch block
window.setDebug(e1.toString());
} catch (PortInUseException e) {
// TODO Auto-generated catch block
window.setDebug(e.toString());
} catch (UnsupportedCommOperationException ex) {
window.setDebug(ex.toString());;
} catch (IOException e) {
// TODO Auto-generated catch block
window.setDebug(e.toString());
}
}
}
public void disconnect() {
if (serialPort != null) {
try {
// close the i/o streams.
outputStream.close();
inputStream.close();
} catch (IOException ex) {
// don't care
}
// Close the port.
serialPort.close();
serialPort = null;
}
}
//Possibly unnecessary
public InputStream getSerialInputStream() {
return inputStream;
}
public OutputStream getSerialOutputStream() {
return outputStream;
}
public void initListener()
{
try
{
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
}
catch (TooManyListenersException e)
{
window.setDebug(e.toString());
}
}
public void setLED(int r, int g, int b){
if(serialPortGeoeffnet == true){
byte[] dataToSend = {(byte)r,(byte)g,(byte)b,(byte)0x0A};
//remove spurious line endings from color bytes so the serial device doesn't get confused
for (int i=0; i<dataToSend.length-1; i++){
if (dataToSend[i] == 0x0A){
dataToSend[i] = 0x0B;
}
}
try {
outputStream.write(dataToSend);
} catch (IOException e) {
// TODO Auto-generated catch block
window.setDebug(e.toString());
}
}
}
@Override
public void serialEvent(SerialPortEvent arg0) {
// TODO Auto-generated method stub
}
}
Thanks