In this post I’ll show how to establish a serial communication (over USB) between the BeagleBoard and Arduino using a Java program. The Program will send commands from BB-XM to toggle a LED on the Arduino.
Hardware components:
- Beagleboard-XM Rev C
- Arduino Uno
- LED connected between the GND and pin 13
- USB cable Type A Plug to B Plug
Arduino Set Up
Before begin interfacing BeagleBoard with Arduino, you must have the Arduino IDE installed and configured. Once it is done, upload the following sketch from your PC:
void setup(){ Serial.begin(9600); pinMode(13, OUTPUT); } void loop (){ if (Serial.available()) { //read serial as ascii integer int ser = Serial.read(); //The ascii equivalent of numbers 0-9 are 48-57 if(ser >= 48 && ser <= 57) { int xTimes = ser - 48; Serial.print("Blink LED x"); Serial.println(xTimes,DEC); TooglePin(13,xTimes); } } } void TooglePin(int pin,int n){ for (int i = 0; i < n; i++) { digitalWrite(pin, HIGH); delay(1000); digitalWrite(pin, LOW); delay(1000); } }
This sketch will basically set Arduino to read incoming serial data as ascii
integer, if it’s between 0-9 then the pin 13 (LED) is toogled xTimes (incoming command).
BeagleBoard Set Up
For the Software Angstrom Linux will be used, the default kernel has support for tty ACM devices (Arduino) if not you can use opkg to install the kernel module:
opkg install kernel-module-cdc-acm
when Arduino is attached to the Beagleboard, something like that should be displayed:
[ 7963.286163] usb 2-2.4: new full speed USB device using ehci-omap and address 16 [ 7963.417602] usb 2-2.4: New USB device found, idVendor=2341, idProduct=0043 [ 7963.425354] usb 2-2.4: New USB device strings: Mfr=1, Product=2, SerialNumber=220 [ 7963.433959] usb 2-2.4: Manufacturer: Arduino (www.arduino.cc) [ 7963.439788] usb 2-2.4: SerialNumber: 7493430303035111E150 [ 7963.459197] cdc_acm 2-2.4:1.0: ttyACM0: USB ACM device
Not so well known but the full Java Standard Edition can run on the BeagleBoard, luckily it is available in the Angstrom package feed.
you need to install for that the openjdk-6 java runtime environment on the BeagleBoard:
opkg install openjdk-6-jre
For Java serial communication the RxTx library is needed:
opkg install librxtx-java librxtx-jni
Now the BeagleBoard is ready to talk to Arduino using Java:
SerialComm.java
import gnu.io.CommPort; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import java.io.FileDescriptor; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class SerialComm { /** Milliseconds to block while waiting for port open */ private static final int TIME_OUT = 2000; /** Default bits per second for COM port. */ private static final int DATA_RATE = 9600; public SerialComm() { super(); } void connect ( String portName ) throws Exception { CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); if ( portIdentifier.isCurrentlyOwned() ) { System.out.println("Error: Port is currently in use"); } else { CommPort commPort = portIdentifier.open(this.getClass().getName(),TIME_OUT); if ( commPort instanceof SerialPort ) { SerialPort serialPort = (SerialPort) commPort; serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); InputStream in = serialPort.getInputStream(); OutputStream out = serialPort.getOutputStream(); (new Thread(new SerialReader(in))).start(); (new Thread(new SerialWriter(out))).start(); } else { System.out.println("Error: Only serial ports are handled by this example."); } } } /** */ public static class SerialReader implements Runnable { InputStream in; public SerialReader ( InputStream in ) { this.in = in; } public void run () { byte[] buffer = new byte[1024]; int len = -1; try { while ( ( len = this.in.read(buffer)) > -1 ) { System.out.print(new String(buffer,0,len)); } } catch ( IOException e ) { e.printStackTrace(); } } } /** */ public static class SerialWriter implements Runnable { OutputStream out; public SerialWriter ( OutputStream out ) { this.out = out; } public void run () { try { int c = 0; while ( ( c = System.in.read()) > -1 ) { this.out.write(c); } } catch ( IOException e ) { e.printStackTrace(); } } } public static void main ( String[] args ) { try { (new SerialComm()).connect(args[0]); } catch ( Exception e ) { // TODO Auto-generated catch block e.printStackTrace(); } } }
This simple code open a serial connection and then interact with it (receiving and sending data).
To be compiled on a Host PC (with an installed JDK) using:
javac SerialComm.java
The compiled classes need to be transferred on the BeagleBoard file system then can be started with:
root@beagleboard:~# java -cp .:/usr/share/java/RXTXcomm.jar \ -Djava.library.path=/lib:/usr/lib/jni \ -Dgnu.io.rxtx.SerialPorts=/dev/ttyACM0 \ SerialComm /dev/ttyACM0
The Java program need to be supplied with the Serial port name /dev/ttyACM0 as an argument. Now you can blink the LED from the Beagle by simply writing on the console numbers between 0-9 :