0
votes

I'm trying to use the RXTX library to send a string from an Arduino Uno to Java. My problem is once the Arduino is ready to send data, it occupys the COM port that the java application is supposed to read from? Other samples online have similar code, which works 'presumably' - by being connected on the same port? Thanks.

Main

    public class Main {
        public static void main(String[] args) throws IOException, PortInUseException, NoSuchPortException 
       {
        SerialPortHandler serial = new SerialPortHandler();
        serial.connect("COM10");
        OutputStream serialOut = serial.getSerialOutputStream();
        String s = "hello\r\n";
        serialOut.write(s.getBytes());
        System.out.println(s.toString());
        serialOut.flush();
        serialOut.close();
       }}



RXTX class

public class SerialPortHandler {
private SerialPort serialPort;
private OutputStream outStream;
private InputStream inStream;

public void connect(String portName) throws IOException, PortInUseException, NoSuchPortException {
    try {
        // Obtain a CommPortIdentifier object for the port you want to open
        CommPortIdentifier portId =
                CommPortIdentifier.getPortIdentifier(portName);

        // Get the port's ownership
        serialPort =
                (SerialPort) portId.open("Demo application", 5000);

        // Set the parameters of the connection.
        setSerialPortParameters();

        // Open the input and output streams for the connection. If they won't
        // open, close the port before throwing an exception.
        outStream = serialPort.getOutputStream();
        inStream = serialPort.getInputStream();
    } catch (NoSuchPortException e) {
        System.out.println("no port");
        throw new IOException(e.getMessage());
    } catch (PortInUseException e) {
        System.out.println("port in use");
        throw new IOException(e.getMessage());
    } catch (IOException e) {
        serialPort.close();
        throw e;
    }
}

/**
 * Get the serial port input stream
 * @return The serial port input stream
 */
public InputStream getSerialInputStream() {
    return inStream;
}

/**
 * Get the serial port output stream
 * @return The serial port output stream
 */
public OutputStream getSerialOutputStream() {
    return outStream;
}

/**
 * Sets the serial port parameters
 */
private void setSerialPortParameters() throws IOException {
    int baudRate = 57600;

    try {
        serialPort.setSerialPortParams(
                baudRate,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        serialPort.setFlowControlMode(
                SerialPort.FLOWCONTROL_NONE);
    } catch (UnsupportedCommOperationException ex) {
        throw new IOException("Unsupported serial port parameter");
    }
}}



Arduino - using the softwareSerial Example - on COM10

     #include <SoftwareSerial.h>
     SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Goodnight moon!");
  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}
void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

im getting the errors from java, once the arduino had compiled first. - had seen posts about USB3 ports messing with the Arduino, but still no luck on USB2's

        Stable Library
=========================================
Native lib Version = RXTX-2.2pre2
Java lib Version   = RXTX-2.1-7
WARNING:  RXTX Version mismatch
Jar version = RXTX-2.1-7
native lib Version = RXTX-2.2pre2
port in use
Exception in thread "main" java.io.IOException: Unknown Application
at arduinocon.SerialPortHandler.connect(SerialPortHandler.java:38)
at arduinocon.Main.main(Main.java:19)
Error 0x5 at ..\src\termios.c(892): Access is denied.
1
"Native lib Version = RXTX-2.2pre2 Java lib Version = RXTX-2.1-7" Your libraries are not compatible. Obviously you are using some beta as native lib, but the Java version requires a supported version of the native lib.Fildor

1 Answers

0
votes

I didnt close the COM connection afterwards, so it occupied the port. I had to manually close the Java.exe in processes for the port to be freed. - and write a proper close method ;)