Pages: [1]   Go Down
Author Topic: Problem with communicating from Mac/Processing/SerialPort  (Read 1689 times)
0 Members and 1 Guest are viewing this topic.
New York
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am at chapter 6 of Getting Started with Arduino (http://makezine.com/getstartedarduino/) and I am having problem with my Processing code.   Specifically:

 String arduinoPort = Serial.list()[0]; // I checked that the right port is returned
 port = new Serial(this, arduinoPort, 9600); // port was previously declared as "Serial port;"

Cause this problem:
" To use the serial library, first open
  Applications -> Utilities -> Terminal.app
  and enter the following:
  sudo mkdir -p /var/lock
  sudo chmod 777 /var/lock
  Exception in thread "Animation Thread" java.lang.RuntimeException: Please use Tools ? Fix the Serial Library.
   at processing.serial.Serial.<init>(Serial.java:153)
   at processing.serial.Serial.<init>(Serial.java:116)
   at example_8a_lamp_sketch_processing.setup(example_8a_lamp_sketch_processing.java:67)
   at processing.core.PApplet.handleDraw(PApplet.java:2103)
   at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
   at processing.core.PApplet.run(PApplet.java:2006)
   at java.lang.Thread.run(Thread.java:680)
"
(the rest of the code is here: http://examples.oreilly.com/0636920021414/ch06/Example_08A/)


Any ideas ?

Thanks a lot !

Bernardo
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you follow the instructions?

Quote
To use the serial library, first open
  Applications -> Utilities -> Terminal.app
  and enter the following:
  sudo mkdir -p /var/lock
  sudo chmod 777 /var/lock
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 635
Posts: 34572
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The code:-
Code:
String arduinoPort = Serial.list()[0]; // I checked that the right port is returned
 port = new Serial(this, arduinoPort, 9600); // port was previously declared as "Serial port;"
Is quite poor.

This is what I use to talk to an arduino through a Mac. Note that the line:-
Code:
String adaptor= "/dev/tty.usbmodem1a21";
must be changed to match what ever your arduino shows up as.

Code:
import processing.serial.*;

Serial scope;
String adaptor= "/dev/tty.usbmodem1a21";

void portConnect(){
    int portNumber = 99;
    String [] ports;
    println(Serial.list());
    ports = Serial.list();
      for(int j = 0; j< ports.length; j++) {
    if(adaptor.equals(Serial.list()[j])) portNumber = j;         
    }
    if(portNumber == 99) portNumber = 0;
    String portName = Serial.list()[portNumber];
    println("Connected to "+portName);
    scope = new Serial(this, portName, 9600);
    scope.bufferUntil(10);
 }
 
   void serialEvent(Serial scope) {  // this gets called every time a line feed is received
  String recieved = scope.readString() ; 
          println(recieved + " from serial port");  // show it at the bottom of the processing window
// also do the stuff you want to do when you get things back from the arduino
}

void setup() {
  size(200, 200);
    portConnect();
  }
 
void draw(){
}

void keyPressed() {
    print(key);
    delay(10);
    switch (key) {
      case 'e':
      case 'E':
        scope.write('e');
      break;
      case 'i':
      case 'I':
        scope.write('i');
      break;
      case 'q':
      case 'Q':
        scope.write('q');
      break;
    }

  };
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 641
Posts: 50351
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The code:-...Is quite poor.
I'll respectfully disagree. Having the Processing sketch enumerate the serial ports that it knows it can successfully talk to (Serial.list()) is a good thing. Choosing the correct item from that list ([0]) is a good thing.

Now, obviously this relies on knowing that there is at least one element in the list, and, if there is more than one, which one is the correct one.

Quote
This is what I use to talk to an arduino through a Mac. Note that the line:-...must be changed to match what ever your arduino shows up as.
The name created this way should match one of the names in the list that Serial.list() generates. If it doesn't, Processing won't be able to talk to it.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 635
Posts: 34572
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now, obviously this relies on knowing that there is at least one element in the list, and, if there is more than one, which one is the correct one
On a Mac no two devices will be given the same name. It the device is not on the list it will default to the first device in the list.
I have found that the list on a Mac is sometimes in the order the devices were added and sometimes in the reverse order depending on the version of Processing. The "bad" way just picks the first device on the list. The way I have developed attaches a specific arduino. This is usefull when there is more than one arduino in your setup. The printing of what device was chosen is a good reminder that things are set up correctly.
Logged

Pages: [1]   Go Up
Jump to: