using serial library - code works in java, not in python

edited February 2016 in Python Mode

Hello,
I am a high-school teacher, interested in using the Python mode of Processing to communicate with Arduino for one of my classes. The first program below works fine in java mode, but the Python version below it doesn't work. I don't get any exception, and the device is correct, but the Python version of serialEvent() is never called. Any ideas or suggestions?
thanks
Andy

Java version, works fine:

import processing.serial.*;

Serial myPort;
PImage img;
float lightVal;

void setup() {
  size(300,300, P3D);
  img = loadImage("cat1.jpeg");
  String devs[] = Serial.list();
  println(devs[devs.length - 1]);
  myPort = new Serial(this, devs[devs.length - 1], 9600);
  myPort.bufferUntil(10);
}

void draw() {
  background(255);
  stroke(0);
  fill(175);

  translate(width/2, height/2);
  rotateY(lightVal);

  imageMode(CENTER);
  image(img, 0,0);
}

void serialEvent(Serial myPort)
{
  String inString = myPort.readStringUntil('\n');
  lightVal = map(float(inString), 0, 150, 0, TWO_PI);
  println("LightVal=" + str(lightVal));
}


Python version, serialEvent never called:

add_library('serial')

lightVal = 0;
LF = 10 #linefeed ascii

def setup():
  global img
  global myPort
  size(300,300, P3D)
  img = loadImage("cat1.jpeg")
  portList = Serial.list()
  portName = portList[len(portList) - 1]
  print(portName)
  myPort = Serial(this, portName, 9600)
  myPort.bufferUntil(10)

def draw():
  background(255)
  stroke(0)
  fill(175)

  translate(width/2, height/2)
  rotateY(lightVal)

  imageMode(CENTER)
  image(img, 0,0)

def serialEvent(evt):
  inString = evt.readStringUntil('\n')
  lightVal = map(float(inString), 0, 150, 0, TWO_PI)
  print("lightVal=", lightVal)

Answers

  • edited November 2018

    https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text

    • I'm still very noob about Python Mode. But my guess is nobody knows how to pull that out! :(
    • Many Processing libraries apply a technique called "reflection" in order to find and call back methods from our sketches.
    • However, they all seem blind when it comes to find Jython methods.
    • So I'm afraid we won't be able to use serialEvent() and other such callbacks in Python Mode! =((
    • Here's another example which tries out method(), thread() & registerMethod(). But utterly fails:
    '''
     * Call Reflection (v1.01)
     * GoToLoop (2016-Feb-26)
     
     * forum.processing.org/two/discussion/15143/
     * using-serial-library-code-works-in-java-not-in-python
    '''
    
    from processing.core.PApplet import thread, method, registerMethod
    
    def setup():
        method(this, 'test1') # Silently fails!
        thread(this, 'test2') # Silently fails!
    
        noLoop()
        #Test(this) # Crashes!!!
        print ENTER, this, ENTER, isinstance(this, PApplet), ENTER
    
    
    def draw(): print frameCount
    
    def test1(): print 'test1'
    
    def test2():
        print 'test2'
        while True: delay(1000), redraw()
    
    
    class Test:
        def __init__(self, pa): registerMethod(pa, 'pre', self) # Crashes!!!
    
        def pre(_): print 'Test class:\t'
    

    Same sketch working in Java Mode now: ~O)

    /**
     * Call Reflection (v1.01)
     * GoToLoop (2016-Feb-26)
    
     * forum.processing.org/two/discussion/15143/
     * using-serial-library-code-works-in-java-not-in-python
     */
    
    void setup() {
      method("test1");
      thread("test2");
    
      noLoop();
      new Test(this);
      println(ENTER, this, ENTER, this instanceof PApplet, ENTER);
    }
    
    void draw() {
      println(frameCount);
    }
    
    void test1() {
      println("test1");
    }
    
    void test2() {
      println("test2");
    
      while (true) {
        delay(1000);
        redraw();
      }
    }
    
    public class Test {
      Test(PApplet pa) {
        registerMethod("pre", this);
      }
    
      void pre() {
        print("Test class:\t");
      }
    }
    
  • @GoToLoop's answer is essentially correct.

    Any library that depends on method name reflection is incompatible with Python Mode. Having said that, I have successfully canvassed the authors of other libraries to make their code compatible with non-Java-mode sketches by allowing the passing of function references rather than method names.

    Also, I could add special support for the serial library in Python Mode, as I've done for the video library. I've created a bug to track this issue:

    https://github.com/jdf/Processing.py-Bugs/issues/157

    Also, I'm very sorry that it took me so long to see this thread. I've now subscribed to the forum to receive emails for mode-related questions.

  • OK; fixed! Upgrade Python Mode to 3008 or better.

Sign In or Register to comment.