myPort.available always 0

edited January 2016 in Arduino

Hy,

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
  printArray(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[32], 9600);
  myPort.clear();
  // 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() {
  println(myPort.available());

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

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

Can someone help me ?

thanks

Answers

  • edited May 2019

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

    1. https://Processing.org/reference/libraries/serial/Serial_bufferUntil_.html
    2. https://Processing.org/reference/libraries/serial/serialEvent_.html

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

    1. https://Processing.org/reference/noLoop_.html
    2. https://Processing.org/reference/redraw_.html

    /**
     * Efficient Serial Reading (v1.02)
     * GoToLoop (2016-Jan-19)
     *
     * Forum.Processing.org/two/discussion/14534/
     * myport-available-always-0#Item_1
     *
     * Forum.Processing.org/two/discussion/16618/
     * processing-with-arduino-void-serialevent#Item_5
     *
     * Discourse.Processing.org/t/
     * map-function-worked-in-processing-2-and-early-3-
     * but-broke-in-3-5-3-need-guidance/11371/2
     */
    
    import processing.serial.Serial;
    
    static final int PORT_INDEX = 32, BAUDS = 9600;
    String myString = "";
    
    void setup() {
      noLoop();
      final String[] ports = Serial.list();
      printArray(ports);
      new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);
    }
    
    void draw() {
      println(myString);
    }
    
    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
    null
    

    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. http://www.nirsoft.net/utils/usb_devices_view.html 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.