How to communicate Java application via serial port on Beagleboard running Angstrom

3,282 views

Published on

Tutorial - Communication through serial port in a java application running Beagleboard with Angstrom OS - RxTx library is needed.

Published in: Technology
2 Comments
2 Likes
Statistics
Notes
  • @Rishi Gupta Thanks for share this sugestion... I hope I can test this library in some oportunity.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Thanks dalton for this wonderful project and specially for detailed description. Please take a look at scm which is an alternative library to rxtx/javaxcomm for serial port communication. Wiki : http://www.embeddedunveiled.com/ Repository : https://github.com/RishiGupta12/serial-communication-manager Video : https://www.youtube.com/watch?v=fYLQbelGunQ It supports RS-232 control signals handshaking, monitoring and has been ported to Linux, MAC, Solaris and Windows operating system.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
3,282
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
47
Comments
2
Likes
2
Embeds 0
No embeds

No notes for slide

How to communicate Java application via serial port on Beagleboard running Angstrom

  1. 1. ################################################################# CETENE/LINCS - CampinaGrande/Recife - Paraíba/Pernambuco - Brasil #### Dalton Cézane Gomes Valadares – Embedded Systems Developer #### Masters/Specialist/Bachelor in Computer Science-UFCG ################################################################# How to communicate Java application via serial port on Beagleboard running Angstrom-Install Java on Angstrom opkg install openjdk-6-jre-Install RxTx Library for serial communication opkg install librxtx-java librxtx-jni-Create a class to open a serial connection and communicate with it:package cetene.lincs.cg.bioquali;import gnu.io.CommPortIdentifier;import gnu.io.PortInUseException;import gnu.io.SerialPort;import gnu.io.SerialPortEvent;import gnu.io.SerialPortEventListener;import gnu.io.UnsupportedCommOperationException;import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import java.util.TooManyListenersException;/** * @author … */public class SerialComm implements SerialPortEventListener { private static final String EMBEDDED_LINUX_PORT = "/dev/ttyS2"; private static final String WINDOWS_PORT = "COM1"; InputStream inputStream; public void execute() { String portName = getPortNameByOS(); CommPortIdentifier portId = getPortIdentifier(portName); if(portId != null) { try { SerialPort serialPort = (SerialPort)portId.open(this.getClass().getName(), 2000); inputStream = serialPort.getInputStream(); serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true);
  2. 2. serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (PortInUseException e) {} catch (IOException e) {} catch (UnsupportedCommOperationException e) { e.printStackTrace(); } catch (TooManyListenersException e) {} } else { System.out.println("Porta Serial não disponível"); } } /** * Get The port name **/ private String getPortNameByOS() { String osname = System.getProperty("os.name","").toLowerCase(); if ( osname.startsWith("windows") ) { // windows return WINDOWS_PORT; } else if (osname.startsWith("linux")) { // linux return EMBEDDED_LINUX_PORT; //Angstrom running onBeagleboard... } else if ( osname.startsWith("mac") ) { // mac return "???"; } else { System.out.println("Sorry, your operating system is not supported"); System.exit(1); return null; } } /** *Get the Port Identifier **/ private CommPortIdentifier getPortIdentifier(String portName) { Enumeration portList = CommPortIdentifier.getPortIdentifiers(); Boolean portFound = false; while (portList.hasMoreElements()) { CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  3. 3. System.out.println("Available port: " + portId.getName()); if (portId.getName().equals(portName)) { System.out.println("Found port: "+portName); portFound = true; return portId; } } } return null; } public void serialEvent(SerialPortEvent event) { switch (event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer = new byte[20]; try { int numBytes = 0; int count = 0; while (inputStream.available() > 0 && count <5) { numBytes =inputStream.read(readBuffer); count++; } String result = new String(readBuffer); result += result.substring(1, numBytes); System.out.println(result); // System.out.println(readBuffer); } catch (IOException e) {} break; } }}-After compile, run the application as follow: java -cp .:/usr/share/java/RXTXcomm.jar
  4. 4. -Djava.library.path=/lib:/usr/lib/jni -Dgnu.io.rxtx.SerialPorts=/dev/ttyACM0 SerialComm/dev/ttyACM0-In my case, adding other libs: java -cp ./EmbeddedBioquali/bin/.:./EmbeddedBioquali/lib/fluent-hc-4.2.2.jar:./EmbeddedBioquali/lib/httpcore-4.2.2.jar:./EmbeddedBioquali/lib/commons-codec-1.6.jar:./EmbeddedBioquali/lib/httpclient-4.2.2.jar:./EmbeddedBioquali/lib/httpmime-4.2.2.jar:./EmbeddedBioquali/lib/commons-logging-1.1.1.jar:./EmbeddedBioquali/lib/httpclient-cache-4.2.2.jar:/usr/share/java/RXTXcomm.jar -Djava.library.path=/lib:/usr/lib/jni-Dgnu.io.rxtx.SerialPorts=/dev/ttyS2 TestService /dev/ttyS2Reference:http://embeddedgeeks.wordpress.com/2012/02/26/arduino-beagleboard-serial-communication-using-java-rxtx/

×