0

I have written Java code to read sensor data sent from an Arduino to a Mac using USB. I'm using the RXTX library version 2.1.7.

Code is:

    CommPortIdentifier portId = getPortId();
    logger.info(PORT_NAME + " has ID " + portId);

    try
    {
        serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);

        // set port parameters
        serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

        input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));

        // add event listeners
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
        logger.error(ex.toString());
    }

I then have a method that reacts to events on the serial port:

public void serialEvent(SerialPortEvent e) {

    String url = LOCALHOST;

    if (e.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {

            String inputLine=input.readLine();
            logger.info("Read: " + inputLine);

            String[] tokens = inputLine.split(",");
            String tmp = tokens[0];
            String lgt = tokens[1];
            String vib = tokens[2];
            String mot = tokens[3];

            logger.info("Temperature: " + tmp);
            logger.info("Light: " + lgt);
            logger.info("Vibration: " + vib);
            logger.info("Motion: " + mot);

            logger.info("Sending update to " + url);
            sendUpdate(url, tmp, lgt, vib, mot);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
    }
}

Ideally, this program runs indefinitely scraping sensor observations from the serial ports. However, I start the program in an evening and when I return the next morning my Mac's bluetooth keyboard and mouse no longer respond. It is definitely this code that is causing the problem. Is it a security thing within Mac OS? Is it best to close and re-open the port after a certain time to stop this happening?

Bailz
  • 605
  • 2
  • 8
  • 17

0 Answers0