One more time a discription of serial communication between an Arduino and a Processing sketch. But this time the important steps in making up a serial connection between an random serial device to the Arduino, which itself has a another serial communication to the pc running a Processing programm.

First the custom made Firmware for Arduino. Firmata won’t work here, because Firmata can’t control a serial communication to the PC and another device (in my case the SM130 RFID reader module). Also have a look on the Arduino serial reference:

  • #include <SoftwareSerial.h>
  • SoftwareSerial SM130reader(6, 7);
  • In the void setup() write
    Serial.begin(19200);
    SM130reader.begin(19200);
  • The rest happens in the void loop()
  • Transmitting data to the reader module with SM130reader.print((char)0xFF);
  • Receiving data from the reader module with
    while (SM130reader.available() > 0) {
    String newInputfromReader = String(SM130reader.read(), HEX);}
  • Transmitting data to processing with
    Serial.println(stringMessage); //print the found tagID over the Serial port (usb)
    This is the same data, that’s normally also received by the serial monitor of the Arduino IDE. But when the Processing programm should be able to receive the data, one can’t use Arduino‘s serial monitor anymore, because it occupies the serial port.

Processing 2.0b7 or lower but not the 64 bit version, because the Processing Serial Library only works in 32 bit:

  • import processing.serial.*;
  • Serial arduino;
  • in void setup() write
    arduino = new Serial(this, Serial.list()[0], 19200); 19200 was the maximum baud rate with which the serial communication of the Arduino and another device was working
  • in void draw() write arduino.bufferUntil('\n');
  • in void serialEvent(Serial arduino) get the latest serial input, trim it and then parse it to do something with it
    String inString = arduino.readStringUntil('\n');// get the ASCII string:
    if (inString != null) {
    inString = trim(inString);
    if(inString.equals(doSomething)){}}

In Processing you can also make an Arduino Object with which you can communicate. But then you have to run Firmata on the Arduino. Be careful with the version of Firmata, I recommend to use 2.107b or higher! Before you can use your Arduino Object in Processing, you have to copy the Arduino library into the sketch folder. You can do this from within the Processing IDE. Go to Sketch > Import Library > Add Library > Search for Arduino (Firmata) > Press on Install. Now it should automatically have written import cc.arduino.*; on the top of your Processing sketch. Now when you run the sketch it throws the error that it can’t find the Arduino library. This is the most incomprehensible bug ever. Search for the libraries folder of your Processing sketch and then go to libraries\arduino\library and rename the Arduino.jar to arduino.jar . OMG WTF that’s it, I really don’t understand why this is still in whitlow and not solved yet! But now this should work fine!

Sounds all pretty straight forward, but stuff like this is not well documented in the web. I had own troubles with it before and have spent myself quite a few time in figuring it all out. The many small errors can make it really frustrating, because what happens is actually not so complicated.

Advertisements