We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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  
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));
}
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
https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text
''' * 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)
@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.