myPort.available always 0

edited January 2016 in Arduino


I'm on Linux kubuntu. I would like receive data from an arduino nano (3 axis motion for head tracking). The arduino code works because I can read data with Pure Data (Pd). The arduino is on port [32] (/dev/ttyUSB0)

I use the common code:

// Example by Tom Igoe

import processing.serial.*;

int lf = 10;    // Linefeed in ASCII
String myString = null;
Serial myPort;  // The serial port

void setup() {
  // List all the available serial ports
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[32], 9600);
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
  myString = myPort.readStringUntil(lf);  
  myString = null;

void draw() {

  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(lf);
    if (myString != null) {

But myPort.available is always 0. I can't read anything.

Can someone help me ?



  • edited March 2016

    Dunno whether it's gonna be of any help to ya.
    But here's a more efficient Serial reading, based on bufferUntil() + serialEvent():


    Plus noLoop() + redraw() too: :D


     * Efficient Serial Reading (v1.0)
     * GoToLoop (2016-Jan-19)
    import processing.serial.Serial;
    static final int PORT_IDX = 32, BAUDS = 9600;
    String myString;
    void setup() {
      final String[] ports = Serial.list();
      new Serial(this, ports[PORT_IDX], BAUDS).bufferUntil(ENTER);
    void draw() {
    void serialEvent(final Serial s) {
      myString = s.readString().trim();
      redraw = true;
  • For me it was COM16 in the arduino IDE but i had to use Serial.list()[1] in processing... Why don't you try it out and also post the printArray(Serial.list()); results.

  • edited January 2016

    you wrote printArray(Serial.list());(another option is to write println(Serial.list()); to view your serial port , post your results like alex_pr said , i had the same problem ....and tried different ports , also you have to close anyprogram that uses your serial port for example arduino serial monitor, hyperterminal ,etc,etc , usually (i am windows user) , the list port name is not the same that your port for example in my case the available port in serial.list is [0] although i have the com 8 so , for me this code works

    myPort = new Serial(this, Serial.list()[0], 9600);

  • Here is the printArray(Serial.list()):

    [0] "/dev/ttyS0"
    [1] "/dev/ttyS1"
    [31] "/dev/ttyS31"
    [32] "/dev/ttyUSB0"

    I don't know how to insert an image in this forum (I have to enter an image url ?). When I unplug the arduino kit, the list finishes at [31] and an error occurs: ArrayIndexOutOfBoundsException:32

    So I think it is the good port. If I do: println(myPort); This returns: processing.serial.Serial@61e0aea1

    but no serial port available...

    The sketch given by GoToLoop gives:

    /dev/ttyS0 /dev/ttyS1 /dev/ttyS2 /dev/ttyS3 /dev/ttyS4 /dev/ttyS5 /dev/ttyS6 /dev/ttyS7 /dev/ttyS8 /dev/ttyS9 /dev/ttyS10 /dev/ttyS11 /dev/ttyS12 /dev/ttyS13 /dev/ttyS14 /dev/ttyS15 /dev/ttyS16 /dev/ttyS17 /dev/ttyS18 /dev/ttyS19 /dev/ttyS20 /dev/ttyS21 /dev/ttyS22 /dev/ttyS23 /dev/ttyS24 /dev/ttyS25 /dev/ttyS26 /dev/ttyS27 /dev/ttyS28 /dev/ttyS29 /dev/ttyS30 /dev/ttyS31 /dev/ttyUSB0

    I think it is the same problem...

  • I find the blue button... for the image.

    Here is the pure data patch which works: pure_data

    we can see (with good eyes) the port is 32. Then the data are reading from arduino and send to OSC. I would like directly receive the data with processing without pure data and OSC messages.

    I will try on another computer, to see if it works...

  • I tried on windows, same problem...

  • Recently a class for a plug&play was posted here. Don't you want to try it - you can run code without arduino attached and then plug it and it should work automatically. That's just a option, cause it seams to me that for some reason you just can't identify the right port.

  • I just try... It finds /dev/ttyUSB0... Same problem. Can the problem come because the arduino is a clone one (CH340) ? Why does it work on Pure Data (pd-extended) ? any idea ?

  • Maybe go throught the wiki. What can I think about is maybe you don't close Pure Data when running sketch (or some other software uses the port) and it stays unavailable for processing.

  • same problem with windows (it's on com 5, port [2]). All applications are closed. There is no error when I run the sketch. I don't understand why processing can't read anything...

  • Not sure if this is helpful, but it might give you an insight on what devices are using what USB ports. Not sure if Windows port assignments translate to linux, but hey, it's a tool to try. It's free, no install, just unzip and run. It helped me work out a problem on Win7 when I couldn't link to my mobile device in Android mode.

  • Hi. I think I got the GoToLoop program working. No errors and I am sure it is connecting, but no serial output from COM3 (in my case with PORT_IDX == 1).

    I notice .bufferUntil(ENTER); and I know that I have no CR or LF with my input data. How should I modify??? Regards JC.....

Sign In or Register to comment.