reapzor / FiloFirmata

MIT License
7 stars 2 forks source link

Simple example not working #2

Open pambrose opened 8 years ago

pambrose commented 8 years ago

Hi,

I am trying to get the example code shown in the README.md to work.

If I run this code, the listener is never invoked. I am getting the same results with OSX and Linux clients. Any help would be greatly appreciated.

Cheers, Paul

Firmata firmata = new Firmata("/dev/ttyACM1");
if (!firmata.start()) {
  System.out.println("Failed to start");
  System.exit(0);
}

CountDownLatch latch = new CountDownLatch(1);
MessageListener<SysexReportFirmwareMessage> firmwareListener =
    MessageListener.from(message -> {
      System.out.println(message.getFirmwareName());
      System.out.println(message.getMajorVersion());
      System.out.println(message.getMinorVersion());
      latch.countDown();
    });

firmata.addMessageListener(firmwareListener);
firmata.sendMessage(new SysexReportFirmwareQueryMessage());

for (int i = 0; i < 10; i++) {
  System.out.println(".");
  latch.await(1, TimeUnit.SECONDS);
}

firmata.removeMessageListener(firmwareListener);
firmata.stop();
pambrose commented 8 years ago

I should have added that I have tried with StandardFirmata and StandardFirmataPlus

MatzeS commented 8 years ago

I have no idea what this ´CountDownLatch´ is but commenting out ´latch.countdown();´ solves the problem for me ^^

M

pambrose commented 8 years ago

Interesting. Which firmata library are you using?

CountDownLatch is in the JDK. The call to latch.countdown() notifies latch.await() that the listener has been executed. That prevents removing the listener before the callback takes place.

MatzeS commented 8 years ago

27.April: Standard Firmata, btw on an Arduino Mega connected to Windows (COM5) /dev/ttyACM1 seems correct to me as well, just don't mess it up on OSX, there are some "wrong serial ports" but looking up the port in the Arduino IDE is always a save way

Tomorrow I may find time to test it for you on OSX.

28.April: Cloned FiloFirmata and PureJavaComm to my mac, cleaned up all the errors in the project, and tested, and... I know nothing, once it runs, once PJC throws an exception... may I can figure this out, but it will take some time