Page 1 of 1

Development on Lattepanda using Java

Posted: Mon Oct 23, 2017 2:17 pm
by mamech
Hello there

I have made some java based applications before on raspberry pi and I am happy to see a user friendly platform like lattepanda and I would like to try development under it, but I have a question. where can I find the needed libraries that enable me from making java to communicate with arduino pins? I have seen examples in C# but I found nothing using java. If this should be done using serial communication (for example using Rxtx library in java) then which COM port should I call to talk to arduino ?


thanks

Re: Development on Lattepanda using Java

Posted: Tue Oct 24, 2017 8:47 am
by LattePanda
Hi, you can use Java to communicate with Arduino pins. But we have no libraries of it, maybe you can Google it to find some useful info, there may be some people share their project. :)

Re: Development on Lattepanda using Java

Posted: Mon Nov 06, 2017 9:57 pm
by TomPittman
LattePanda runs Win10, and Win10 runs Java, but the interface to the Arduino is specified and written in C#. The Arduino itself is programmed with (open-source) Firmata, so I built a package to replace the supplied LattePanda driver with a Java equivalent.

We are only using the Arduino to drive two servos, so most of the Firmata interface is not implemented, but only the two APIs to blink the LED and to drive servos. The LattePanda code is well-commented, and with reference to the Firmata C++ source code, the Firmata documentation on GitHub, adding the additional APIs should be straight-forward knowing only Java and neither C nor C#. I tried to preserve the LattePanda text as much as possible, changing only the spelling as required by Java syntax.

Unfortunately, there is no standard way in Java to access the serial port that Arduino uses for communication to the host computer. There are several serial port implementations, every one I looked at is huge and hard to understand and overkill for this application, but I did not want to re-invent that wheel, so we chose the Java-Simple-Serial-Connector (JSSC) implementation as being slightly more transparent. After a hiccup or two (readily identified in the Java debugger and fixed by making sure their DLL is in the correct folder), I was able to make my test program blink the blue Arduino LED.

The code:

package fake;

import jssc.SerialPort;

public class Arduino { // Adapted to Java from arduino.cs ... -- 2017 November 6

private static final int SET_PIN_MODE = 0xF4;
// set a pin to INPUT/OUTPUT/PWM/etc
private static final int DIGITAL_MESSAGE = 0x90;
// send data for a digital port
private static final int ANALOG_MESSAGE = 0xE0;
// send data for an analog pin (or PWM)

public static final byte LOW = 0;
public static final byte HIGH = 1;
public static final byte INPUT = 0;
public static final byte OUTPUT = 1;
public static final byte ANALOG = 2;
public static final byte PWM = 3;
public static final byte SERVO = 4;

int[] digitalOutputData;

SerialPort surrealPort;

/// <summary>
/// Sets the mode of the specified pin (INPUT or OUTPUT).
/// </summary>
/// <param name="pin">The arduino pin.</param>
/// <param name="mode">Mode Arduino.INPUT Arduino.OUTPUT
/// Arduino.ANALOG Arduino.PWM or Arduino.SERVO .</param>

public void pinMode(int pin, byte mode) {
byte[] message = new byte[3];
message[0] = (byte) (SET_PIN_MODE);
message[1] = (byte) (pin);
message[2] = (byte) (mode);
try {
surrealPort.writeBytes(message);
} catch(Exception ex) {System.out.println(ex);}
} //~pinMode

/// <summary>
/// Write to a digital pin that has been toggled to output mode
/// with pinMode() method.
/// </summary>
/// <param name="pin">The digital pin to write to.</param>
/// <param name="value">Value either Arduino.LOW or Arduino.HIGH.</param>

public void digitalWrite(int pin, byte value) {
int portNumber = (pin >> 3) & 0x0F;
byte[] message = new byte[3];
int digiData = digitalOutputData[portNumber&0xF];

if ( (int) value == 0)
digiData = digiData & ~(1 << (pin & 0x07));
else digiData = digiData | (1 << (pin & 0x07));
digitalOutputData[portNumber&0xF] = digiData;

message[0] = (byte) (DIGITAL_MESSAGE | portNumber);
message[1] = (byte) (digiData & 0x7F);
message[2] = (byte) (digiData >> 7);
try {
surrealPort.writeBytes(message);
} catch(Exception ex) {System.out.println(ex);}
} //~digitalWrite

/// <summary>
/// controlling servo
/// </summary>
/// <param name="pin">Servo output pin.</param>
/// <param name="angle">Servo angle from 0 to 180.</param>

public void servoWrite(int pin, int angle) {
byte[] message = new byte[3];
message[0] = (byte) (ANALOG_MESSAGE | (pin & 0x0F));
message[1] = (byte) (angle & 0x7F);
message[2] = (byte) (angle >> 7);
try {
surrealPort.writeBytes(message);
} catch(Exception ex) {System.out.println(ex);}
} //~servoWrite

public Arduino() { // outer class constructor..
System.out.println("new Arduino ");
surrealPort = new SerialPort("COM3");
digitalOutputData = new int[16];
try {
surrealPort.openPort(); // Open serial port
surrealPort.setParams(57600, 8, 1, 0);
} catch(Exception ex) {System.out.println(ex);}

}} //~Arduino

// Testing Arduino from Java.. // 2017 November 6

package testard;

import java.lang.Thread;
import jssc.SerialPort;
import fake.Arduino;

public class ArduTest {

static Arduino ardueeno = new Arduino(); // create an instance and
// ..and initialize with the default parameters

public static void main(String[] args) {
try {
ardueeno.pinMode(13, Arduino. OUTPUT);
// Set the digital pin 13 as output
ardueeno.pinMode(9, Arduino. SERVO);
// to drive a servo
while (true) { // ==== set the led on or off
ardueeno.digitalWrite(13, Arduino. HIGH); // set the LED = on
ardueeno.servoWrite(9, 180);
// tell the servo motor go to the position in 180 degrees
Thread.sleep(1000); // delay 1 second
ardueeno.digitalWrite(13, Arduino. LOW); // set the LED = off
ardueeno.servoWrite(9, 0);
Thread.sleep(1000);} // delay 1 second
} catch(Exception ex) {System.out.println(ex);}}

public ArduTest() { // outer class constructor..
System.out.println("ArduTest ");}} //~ArduTest

Re: Development on Lattepanda using Java

Posted: Mon Nov 06, 2017 9:59 pm
by TomPittman
Sorry, the forum software made a mess of my code, but you should be able to figure it out ;-)

Tom Pittman

Re: Development on Lattepanda using Java

Posted: Tue Nov 07, 2017 5:17 am
by Lutz
The forum has a button for code format "</>", did you try it ?

Re: Development on Lattepanda using Java

Posted: Tue Nov 07, 2017 10:11 am
by LattePanda
TomPittman wrote:
Mon Nov 06, 2017 9:59 pm
Sorry, the forum software made a mess of my code, but you should be able to figure it out ;-)

Tom Pittman
Thanks for your sharing! :)