element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
RoadTests & Reviews
  • Products
  • More
RoadTests & Reviews
Review Blogs HARTING MICA: Read BOSCH CISS Sensor with Java - part 1: USB Connect and Listen
  • Blogs
  • RoadTest Forum
  • Documents
  • RoadTests
  • Reviews
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join RoadTests & Reviews to participate - click to join for free!
  • Share
  • More
  • Cancel
  • Author Author: Jan Cumps
  • Date Created: 2 May 2019 9:51 PM Date Created
  • Views 3225 views
  • Likes 7 likes
  • Comments 12 comments
Related
Recommended
  • debian
  • cross-platform development
  • industrial
  • mica
  • harting
  • jessie
  • automotive
  • bosch
  • netbeans
  • linux
  • java

HARTING MICA: Read BOSCH CISS Sensor with Java - part 1: USB Connect and Listen

Jan Cumps
Jan Cumps
2 May 2019

I'm road testing the Harting MICA Complete IIoT Starter Kit.

In this blog series: Java!

I want to talk to the BOSCH CISS sensor over USB, and - if I can get that to work - send the telemetry safely to MQTT.

 

image

I'm using the Linux Debian Stretch Container here, with a Java runtime 1.8 installed.

Check here how to install the jre.

 

End goal:

  • listen to the USB port for data, event driven
  • try to set up a meaningful conversation with the BOSCH CISS sensor.
  • connect to the default MQTT broker and try to mimic the payload of the CISSGateway container.
  • connect to a safe MQTT server with certificate authentication.

 

In this post:

  • set up the application
  • set up the command line parser to get port name and speed
  • initial architecture: serial comms and error logging libraries
  • Connect to the sensor and set an event listener

 

Development Environment

 

Because Java is cross-platform by default, and the BOSCH sensor works on Windows and Linux, I can develop on my laptop.

There are a few things to take into account to make it work on both platforms:

  • parameterise the USB port name. It will be different on both systems.
  • USB libraries in Windows and Linux are not compatible. The JSerialComm library luckily takes care of the platform specific dependencies.

 

I'm developing in the NetBeans IDE. I have a Java 1.8 JDK installed on my laptop.

This setup allows me to develop and debug on the Windows computer. I don't need the Harting MICA during that activity.

For my own peace of mind, I regularly test on the MICA though. After each major functionality change, I want to see if the program behaves on Linux.

I've ceated a NetBeans Maven project. Maven is a build tool that manages dependencies. Handy for the next step:

 

Dependencies

 

I'm usin a few java libraries to make life easy:

  • USB communication: JSerialComm library
  • Command line parsing: Apache CLI.

 

You can add the libraries to your project from NetBeans. Right-click on the Dependencies node and select Add Dependency ...

image

 

In the search button, first type CLI and add the Appache CLI lib, then enter JSerialComm and add that library too.

later, when you're deploying to the MICA, you have to move those .jar libraries over. You can right-click on them in the project to find their location on your pc.

 

 

Code

 

The main class - CISSBoschApp.java

 

package net.cumps.cissboschusb;

/**
 *
 * @author jancu
 */
public class CISSBoschApp {
    public static void main(String[] args) {
        new Cli(args).parse();
    }    
}

 

This is a very simple class. It just asks our parser to check the arguments. If all ok, the parser will kick off activities.

 

The command line parser - Cli.java

 

package net.cumps.cissboschusb;

/**
 *
 * @author jancu
 * credit where credit due: http://www.thinkplexx.com/blog/simple-apache-commons-cli-example-java-command-line-arguments-parsing
 * 
 */

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class Cli {
 private static final Logger log = Logger.getLogger(Cli.class.getName());
 private String[] args = null;
 private Options options = new Options();

 public Cli(String[] args) {
  this.args = args;
  options.addOption("h", "help", false, "show help.");
  options.addOption("u", "usb", true, "USB character device to listen to (e.g., COM13, /dev/ttyACM0)");
  options.addOption("b", "baud", true, "USB speed (baud, typical 115200)");
 }

 public void parse() {
  CommandLineParser parser = new BasicParser();
  CommandLine cmd = null;
  try {
   cmd = parser.parse(options, args);
   if (cmd.hasOption("h"))
    help();
   if (cmd.hasOption("u")) {
    log.log(Level.INFO, "Using cli argument -usb=" + cmd.getOptionValue("u"));
    // Whatever you want to do with the setting goes here
       } else {
    log.log(Level.SEVERE, "Missing usb option");
    help();
   }

   if (cmd.hasOption("b")) {
    log.log(Level.INFO, "Using cli argument -baud=" + cmd.getOptionValue("b"));
   } else {
    log.log(Level.SEVERE, "Missing baud option");
    help();
   }
    CISSBoschUSBReader.read(cmd.getOptionValue("u"), 
            Integer.parseInt(cmd.getOptionValue("b")));
  
  } catch (ParseException e) {
   log.log(Level.SEVERE, "Failed to parse comand line properties", e);
   help();
  }
 }

 private void help() {
  // This prints out some help
  HelpFormatter formater = new HelpFormatter();
  formater.printHelp("Main", options);
  System.exit(0);
 }
}

 

The USB handler - CISSBoschUSBReader.java

 

package net.cumps.cissboschusb;

import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;
import java.util.logging.Logger;

/**
 *
 * @author jancu
 */
public class CISSBoschUSBReader {

    private static final Logger log = Logger.getLogger(Cli.class.getName());

    public static void read(String cDevice, int iBaud) {
        SerialPort comPort = SerialPort.getCommPort(cDevice);
        comPort.setBaudRate(iBaud);
        if (comPort.openPort()) {
            comPort.addDataListener(new SerialPortDataListener() {
                @Override
                public int getListeningEvents() {
                    return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
                }

                @Override
                public void serialEvent(SerialPortEvent event) {
                    if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
                        return;
                    }
                    byte[] newData = new byte[comPort.bytesAvailable()];
                    int numRead = comPort.readBytes(newData, newData.length);
                    System.out.println("Read " + numRead + " bytes.");
                }
            });
        }
    }
}

 

Debug on Windows

 

In NetBeans, define the command line parameters via Run -> Set Project Configuration -> Customize ...

image

Build the project by pressing the Build button.

Set a breakpoint in CISSBoschUSBReader.java, on this line:

 

        SerialPort comPort = SerialPort.getCommPort(cDevice);

 

Then start the debugger. When it breaks, check the value of the usb port and speed.

If that is ok, you can continue running the program, or step through it if you want.

 

Test on Linux

 

Create a /root/java directory in Linux

Copy the two .jar files of the dependencies to that folder with winSCP. (install openssh-server if needed).

Copy the project's jar file t the same folder (it's found in the target folder of your project.

 

You should see this when you execute ls -l:

 

-rw-r--r-- 1 root root   5994 May  2 21:00 CISSBoschUSB-1.0-SNAPSHOT.jar
-rw-r--r-- 1 root root  53820 May  2 12:54 commons-cli-1.4.jar
-rw-r--r-- 1 root root 372375 May  2 20:14 jSerialComm-2.5.0.jar

 

Java files don't need the executable flag. It's the jre that needs to be executable, not the libs.

 

 

Now test your code:

 

java -cp ./CISSBoschUSB-1.0-SNAPSHOT.jar:./commons-cli-1.4.jar:jSerialComm-2.5.0.jar net.cumps.cissboschusb.CISSBoschApp -usb "/dev/ttyACM0" -baud 115200

 

image

 

If you have the CISSGateway container active, the program will exit with an error, because the gateway runs away with our data image.

If the gateway was inactive before you started the MICA, then the program will sit there and show nothing.

That is good, because the BOSCH CISS sensor hasn't been initialised. That's for a next post.

 

Related Blog
HARTING MICA: Develop and Debug a C GPIO Example in Eclipse - Part 1: User Experience
HARTING MICA: Develop and Debug a C GPIO Example in Eclipse - Part 2: MICA Debian Stretch Setup
HARTING MICA: Develop and Debug a C GPIO Example in Eclipse - Part 3: Eclipse Configuration on Windows
HARTING MICA: Develop and Debug a C GPIO Example in Eclipse - Part 4: Build and Debug the GPIO Example
HARTING MICA: Make a Safe(r) MQTT Container with Certificate and TLS/SSL
HARTING MICA: Manage Access to USB and other Devices
HARTING MICA: SD Card as Shared Storage
HARTING MICA: Alpine Linux and another MQTT Container with Certificate and TLS/SSL
HARTING MICA: Connect to Amazon Web Services
HARTING MICA: Install Java 8 VM in a Debian Stretch Container
HARTING MICA: Read BOSCH CISS Sensor with Java - part 1: USB Connect and Listen
Attachments:
CISSBoschUSB.zip
  • Sign in to reply

Top Comments

  • Jan Cumps
    Jan Cumps over 6 years ago in reply to Jan Cumps +2
    I tested this and it works (you have to restart the whole MICA to apply changes in the device selection): I created a user jancumps useradd -m jancumps passwd jancumps I moved the java libraries to my…
  • Gough Lui
    Gough Lui over 6 years ago +1
    Hi Jan - I think you mean 115200. You will also need to associate the /dev/ttyACM0 device to the container through Settings -> Devices on the MICA configuration page to ensure your container has access…
  • Jan Cumps
    Jan Cumps over 6 years ago in reply to Gough Lui +1
    I corrected the BAUD setting. Thanks. For some reason, I don't need to select the device in the MICA management page. I also read it in the documentation, but I can use the port from a Debian Stretch container…
  • Jan Cumps
    Jan Cumps over 5 years ago in reply to szoke12

    My application here runs its own thread. It continuously gets sensor info and logs it to the console. No MQTT involved.

    I can't recal if there were hickups in the read of my Java code here (note - I should attach the NetBeans project to the blog post edit: done).

     

    I can't recall if the CISS container paused on my installation. Gough has reviewed this aspect in detail too and doesn't mention it either:  Harting MICA RoadTest-in-Depth – Ch4: Bosch Connected Industrial Sensor Solution (CISS)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • szoke12
    szoke12 over 5 years ago

    Hi Jan!

     

    Thanks for the road test. I found an intesting thing. I'm using the default CISS container, and it sends the data in every seconds via mqtt. But in every minutes stops for 5-6 seconds. Is this bug in your java app too?

    What can be wrong?

     

    Thanks!

    szoke12

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 6 years ago

    The Java version installed on the Java8 container is, I think, the Oracle version - but the OpenJDK version is mentioned in the readme:

    image

    But when I run java -version on the box, I get non-OpenJDK type of info:

     

    java version "1.8.0_162"

    Java(TM) SE Runtime Environment (build 1.8.0_162-b12)

    Java HotSpot(TM) Client VM (build 25.162-b12, mixed mode)

     

    When I install the OpenJDK version of the jre on Linux (I tested on a MICA Debian Stretch container), I get a clear reference to OpenJDK:

     

    openjdk version "1.8.0_212"

    OpenJDK Runtime Environment (build 1.8.0_212-8u212-b01-1~deb9u1-b01)

    OpenJDK Client VM (build 25.212-b01, mixed mode)

     

    Does anyone know if the version info matches the Busybox OpnJDK call sign?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 6 years ago in reply to Jan Cumps

    The code running on the MICA:

     

    image

     

    You'll see that the light sensor has no data, while the mag and acc sensor have X, Y, Z data.

    That's because I haven't implemented all possible payload parsers yet - only the x-y-z type.

     

    The BOSCH python example also supports event based loging for a few sensors. Those I haven't implemented either.

    But being able to get at the accelerometer and magnetometer data is a good start - a proof of concept.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 6 years ago in reply to Jan Cumps

    Progress: Part of the BOSCH python example is ported. I'm getting the payload:

    image

     

    I added a shutdown interrupt hook, so that I can close the com port when someone kill -9's the program.

    There's much more work to do, such as initialising the sensor and actually interpreting the results.

    I'm currently using the python script from Bosch to initialise the sensor before I run the Java program - but this should be ported to my code too.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube