Development on Lattepanda using Java

A place to talk about all the Operating Systems, Software & Hardware
Forum rules

- Posts are to be made in the relevant forum. Users are asked to read the forum descriptions before posting.

- Members should post in a way which is consistent with "normal writing". That is users should not post excessive numbers of emoticons, large, small or coloured text, etc. Similarly users should not SHOUT or use excessive punctuation (e.g. ! and ?) in topic titles or posts.

- Members should use an appropriate, descriptive subject when posting a new topic. Examples of bad subjects include; "Help me!", "I'm stuck!", "I've got an error!", etc. Examples of good subjects include; "Getting an TABLE FULL error upon login", "Cannot restore a backup".

- Members are asked to post in English only so that posts can be well received and properly monitored.

- Spam is not tolerated here under any circumstance. This includes offering hosting services (charged and free), installation services, solicitation etc. Recruiting members for your own projects (moderators, designers, professional MOD work, etc.) is not permitted and is also considered spam. Users posting spam will be warned and their post removed.

- Members should post in a way that is respectful of other users. Flaming or abusing users in any way will not be tolerated and will lead to a warning. Vulgar language and inappropriate material is not allowed and will be removed. We try to maintain a family friendly atmosphere whenever it is possible within the main subject matter of this community. Please keep this in mind when participating.

- Each user is allowed to create one account. If you would like to change your username, please contact us and, most likely, we can do it for you while you can keep all of your profile data, posts, and other content.

- If a user violates our guidelines and shows a disregard for them, our staff and our community, they run the risk of losing their account. We reserve the right to deactivate any account and to edit or remove any content without warning. These guidelines are subject to change at any time without notice.

Thank you for visiting LattePanda.com!
Post Reply
User avatar
mamech
Posts: 1
Joined: Mon Oct 23, 2017 2:06 pm

Mon Oct 23, 2017 2:17 pm

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

User avatar
LattePanda
Site Admin
Posts: 2199
Joined: Wed Jan 20, 2016 9:09 am

Tue Oct 24, 2017 8:47 am

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. :)
Enjoy Tinkering with LattePanda ! :lol:

User avatar
TomPittman
Posts: 2
Joined: Mon Nov 06, 2017 9:40 pm

Mon Nov 06, 2017 9:57 pm

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

User avatar
TomPittman
Posts: 2
Joined: Mon Nov 06, 2017 9:40 pm

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

User avatar
Lutz
Posts: 165
Joined: Mon Apr 18, 2016 4:30 am
Location: Germany, NRW

Tue Nov 07, 2017 5:17 am

The forum has a button for code format "</>", did you try it ?

User avatar
LattePanda
Site Admin
Posts: 2199
Joined: Wed Jan 20, 2016 9:09 am

Tue Nov 07, 2017 10:11 am

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! :)
Enjoy Tinkering with LattePanda ! :lol:

Post Reply