error opening serial port busy

edited January 2017 in Arduino

Hello i have this processing sketch for my thesis project to move a servo left or right with Emotiv Epoc(Actually i want to send keystrokes r or l). The first goal is to identify automatically the port. I get an opening serial port busy error . I think the serial port is open inside the loop so when i try to open it (line 73-74) it gets me the error. Is there a way that i can close it an re open it? (Actually i want a sketch that checks which port has an Arduino, then connect to that port) any help will be greatly appreciated :((

import processing.serial.*;

Serial ser_port;                // for serial port
PFont fnt;                      // for font
int num_ports;
boolean device_detected = false;
String[] port_list;
String detected_port = "";

void setup() {
    size(400, 200);                         // size of application window
    background(0);                          // black background
    fnt = createFont("Arial", 16, true);    // font displayed in window

    println(Serial.list());

    // get the number of detected serial ports
    num_ports = Serial.list().length;
    // save the current list of serial ports
    port_list = new String[num_ports];
    for (int i = 0; i < num_ports; i++)  {
        port_list[i] = Serial.list()[i];
    }
}

void draw()
{
    background(0);
    // display instructions to user
    textFont(fnt, 14);
    text("1. Arduino or serial device must be unplugged.", 20, 30);
    text("   (unplug device and restart this application if not)", 20, 50);
    text("2. Plug the Arduino or serial device into a USB port.", 20, 80);

    // see if Arduino or serial device was plugged in
    if ((Serial.list().length > num_ports) && !device_detected) {
        device_detected = true;
        // determine which port the device was plugge into
        boolean str_match = false;
        if (num_ports == 0) {
            detected_port = Serial.list()[0];
        }
        else {
            // go through the current port list
            for (int i = 0; i < Serial.list().length; i++) {
                // go through the saved port list
                for (int j = 0; j < num_ports; j++) {
                    if (Serial.list()[i].equals(port_list[j])) {
                        break;
                    }
                    if (j == (num_ports - 1)) {
                        str_match = true;
                        detected_port = Serial.list()[i];

                    }
                }
            }
        }
    }
    // calculate and display serial port name
    if (device_detected) {
        text("Device detected:", 20, 110);
        textFont(fnt, 18);
        text(detected_port, 20, 150); 
        textFont(fnt, 14);
        text("3.  Now think Right or Left in this Window",20,190);

    }
}

char valToWrite = 'r' & 'l'; //a value to send as a single byte

void keyPressed(){

    ser_port = new Serial(this,detected_port,9600);
    ser_port.bufferUntil(10);
    valToWrite = key;
    ser_port.write(valToWrite);
}

void keyReleased(){


  valToWrite = 'r' & 'l';
  ser_port.write(valToWrite);
} 
Tagged:

Answers

  • Answer ✓

    There should be no need to keep reconstructing the ser_port object on each key press unless you also want to detect when a device disconnects. If that's what you want, I think you need ser_port.stop() at the end of keyReleased(). If that's not the problem, also make sure that no other processes are trying to use the port you need; in some instances you'll find that the JVM stays running after you exit something and still has an open connection on the port, especially if you're using the Arduino IDE.

    Why are you trying to send the value (char)'r'&'l'? I'm pretty sure that works out to '`'.

  • thank you for your reply. The ser_port.stop() worked.I am sending r & l because it is the emokey mapping tha i have from the emotiv epoc and plus this is how i programmed arduino to receive the input for the servo(i mean with r and l).

Sign In or Register to comment.