samaygoenka
Posts: 6
Joined: Thu Dec 26, 2013 10:15 am

Processing & Serial - RPi + PIC microcontroller

Thu Dec 26, 2013 10:50 am

Hi,
I'm working on a project (a standalone test bench/ calibration bench) which requires the following stuff basically: It has a PIC micro controller which controls hardware (motors/buzzer/LEDs, ADC inputs and an LCD). The PIC calculates some error values and sends it to the Raspberry Pi over serial, which then calibrates the device under test. I would prefer to use the TX and RX pins on the RPi, and not some Serial-USB converter, so my circuit is smaller and less costly.
I'm fairly fluent with basic Java, and have used the Processing IDE in the past, and i think it'll make the Serial part of the process pretty simple. (Plus I can write the code on my laptop, the RasPi is a bit slow, really). Thing is, I can't seem to get Processing to talk to any serial port (RPi's own, or an Arduino connected to it).
I have looked all over the web for solutions on how to get processing up and running.
Here's some details:
Using Raspbian ; openjdk-6-jdk ; librxtx-java
Used http://cagewebdev.com/index.php/raspber ... our-raspi/ to get Processing 1.5.1 running. Processing works, and sketches without Serial library work OK.
I copied librxtxSerial.so from usr/lib/jni/ to the processing/modes/java/libraries/serial/library/linux32 folder.
Copied RXTXcomm.jar and RXTXcomm2.2-pre2.jar from use/share/java to processing/modes/java/libraries/serial/library/.
To test the serial port, I looped RX and TX pins and tested using minicom (on /dev/ttyAMA0) - it works OK. Also programmed an Arduino to send 'a' continuously over serial. RPi listed it as /dev/ttyACM0. Opened the port using minicom - Works OK.
But in processing, I try to get it to talk to either port (AMA0/ACM0) using the example codes provided:

Code: Select all

import processing.serial.*;
Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port

void setup() 
{
  size(200, 200);
  myPort = new Serial(this, "/dev/ttyACM0", 9600);     //or /dev/ttyAMA0
}

void draw() {
  background(255);
  if (mouseOverRect() == true) {  // If mouse is over square,
    fill(204);                    // change color and
    myPort.write('H');              // send an H to indicate mouse is over square
  } 
  else {                        // If mouse is not over square,
    fill(0);                      // change color and
    myPort.write('L');              // send an L otherwise
  }
  rect(50, 50, 100, 100);         // Draw a square
}

boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
When i press Run, it opens a draw window, but gives an error: Error in Serial.write().
java.lang.NullPointerException at processing.serial.Serial.write (unknown source)
And then there's about 15 lines of the same sort of errors.
I have tried to search as much as I could, but nothing has helped so far. I'd be eternally grateful if someone could help me get a serial port talking to java, preferably Processing. I really need to get this done.
Thanks.

User avatar
scruss
Posts: 2420
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Processing & Serial - RPi + PIC microcontroller

Thu Dec 26, 2013 2:19 pm

That article you followed is very out of date; it's a copy of something I wrote a few months before. Processing is now at v.2, so 1.5 is very old. Use the hardfloat official Java, too.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

samaygoenka
Posts: 6
Joined: Thu Dec 26, 2013 10:15 am

Re: Processing & Serial - RPi + PIC microcontroller

Thu Dec 26, 2013 6:01 pm

Thanks for the reply. I did come across your article while searching for a way out.
Could you do me a big favour and tell me exactly what packages to apt-get install for the hardfloat java? I've already downloaded Processing 2.1. What files would I have to replace to get serial working with it? And will changing the java package do any good to the serial communications problem?
Thanks again.

User avatar
scruss
Posts: 2420
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Processing & Serial - RPi + PIC microcontroller

Sat Dec 28, 2013 4:21 am

samaygoenka wrote:Could you do me a big favour and tell me exactly what packages to apt-get install for the hardfloat java?
http://bit.ly/19qJpuZ

Don't use soft-float java, unless you want to die of boredom. Use the latest Processing because no-one will help you on forums if you run old code.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

samaygoenka
Posts: 6
Joined: Thu Dec 26, 2013 10:15 am

Re: Processing & Serial - RPi + PIC microcontroller

Sat Dec 28, 2013 4:26 am

Thanks. I got serial comms working using the Pi4J library.
I'll give this another try, though.
Thanks again.:-)

User avatar
CopterRichie
Posts: 131
Joined: Tue Mar 26, 2013 3:14 am
Location: Los Angeles CA.

Re: Processing & Serial - RPi + PIC microcontroller

Sat Jan 04, 2014 3:06 pm

A sidebar issue please, what is the command to uninstall "OpenJDK"? I installed the Oracle Java but when entering java -version on the command line, the OpenJDK is found.

Thank you.

Code: Select all

java version "1.7.0_25"
OpenJDK Runtime Environment (IcedTea 2.3.10) (7u25-2.3.10-1~deb7u1+rpi1)
OpenJDK Zero VM (build 22.0-b10, mixed mode)

User avatar
CopterRichie
Posts: 131
Joined: Tue Mar 26, 2013 3:14 am
Location: Los Angeles CA.

Re: Processing & Serial - RPi + PIC microcontroller

Sat Jan 04, 2014 4:29 pm

I figured it out, this worked for me.

Code: Select all

sudo apt-get purge openjdk-6-jre
sudo apt-get purge openjdk-7-jdk
sudo apt-get autoremove

Then:

sudo apt-get update && sudo apt-get install oracle-java7-jdk

samaygoenka
Posts: 6
Joined: Thu Dec 26, 2013 10:15 am

Re: Processing & Serial - RPi + PIC microcontroller

Sat Jan 04, 2014 5:33 pm

Code: Select all

sudo update-alternatives --config java
This would have let you choose between the versions of java installed on your system, without having to uninstall one. Just FYI.

User avatar
CopterRichie
Posts: 131
Joined: Tue Mar 26, 2013 3:14 am
Location: Los Angeles CA.

Re: Processing & Serial - RPi + PIC microcontroller

Sat Jan 04, 2014 5:37 pm

Thank you, learned something new.

User avatar
scruss
Posts: 2420
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Processing & Serial - RPi + PIC microcontroller

Tue Jan 07, 2014 1:55 pm

Good call on the jSSC library (said to no-one in particular). It seems that Processing 2.1 only ships with x86 libraries for Linux, even though the main jSSC jar contains armel and armhf libraries. Downloading jSSC-2.6.0-Release.zip and replacing processing-2.1/modes/java/libraries/serial/library/jssc.jar with the jSSC-2.6.0-Release/jssc.jar version fixed most of my serial issues.

Processing 1.x→2.0 used libRXTX, but 2.1 moved to jSSC.

Full install instructions for Raspbian/hardfloat Java: Processing 2.1 + Oracle Java + Raspberry Pi + Serial + Arduino = ☺

cheers,
Stewart

(who apologises for his previous unhelpful post in this thread. lmgtfy.com really needs an “Are you feeling cranky?” filter …)
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

Return to “Interfacing (DSI, CSI, I2C, etc.)”