In this post I discuss in detail the development of Java application for the open source quartz crystal microbalance openQCM. This post is intended for developers and contributors of the open source project. The entire project is available on github repository at this link
This is the latest release of openQCM Java software version 0.2. The main feature of this release is the development of a new graphical user interface (GUI) using NetBeans IDE GUI builder. The designer Glenda Torres Guizado and me have worked together for the development of the new graphical user interface . The overall structure of the software project is totally changed in order to use the Swing components of NetBeans. Moreover, it was added a method for smoothing the real time frequency and temperature data.
The main features of the latest openQCM application are:
- Connect Arduino find the COM port and set the serial connection between the Arduino board and your PC.
- Read Data read the QCM frequency and temperature data available at the connection COM port.
- Plot Data plot the QCM frequency and temperature data using a real time chart.
- Save Data save the frequency and temperature data in a text file.
The openQCM Java software is developed using Java 8 programming language and NetBeans development environment (IDE). You can download the Java Development Kit JDK 8u31 with NetBeans 8.0.2 at this link I strongly suggest to uninstall any previous version of Java software in your machine before installing the latest version of JDK.
Download the entire project openQCM as ZIP folder and import the project as zip file in NetBeans IDE
File > Import Project > From zip …
Before compile and run the project you need to configure the RXTX Java library for the Java Runtime Engine JRE
RXTX Library Setting
openQCM Java project uses the RXTX Java library which provides serial communication for the Java Development Kit JDK. You can download the right library for your machine at this link
Depending on your operating system you have to follow different procedures to correctly install the RXTX library. For more info on setting up the RXTX library see the Arduino and Java post on Arduino Playground and for advanced installation setting the RXTX wiki
Find the directory in which you have installed JDK and navigate to the jre directory:
C:\Program Files (x86)\Java\jdk1.8.0_20\jre
Open the folder containing the RXTX library and copy the following files in the java directories:
RXTXcomm.jar -> …\lib\ext
rxtxSerial.dll -> …\bin
Mac OS X
Copy RXTXcomm.jar from the library folder to /Library/Java/Extensions
Copy the MAC_OS_X/librxtxSerial.jnilib (or, if this version does not work for you, obtain a 64 bit compiled version) and paste into /Library/Java/Extensions
Append the directory containing librxtxSerial.jnilib files into your DYLD_LIBRARY_PATH environment variable, using the following command:
export DYLD_LIBRARY_PATH = /Library/Java/Extensions
You can encounter some issues with Mac OS X permissions and locking port system. In this case follow these instructions:
Open terminal and create the lock directory (as root) by using the following command:
sudo mkdir /var/lock
Apply the right permissions to the /var/lock directory by using these command:
sudo dscl . -append /groups/_uucp GroupMembership username
sudo chgrp uucp /var/lock
sudo chmod 775 /var/lock
Java Application Project
I will discuss in detail the whole openQCM Java software project trying to cover the key features of the source code. This section is intended for developers and contributors of the open source project.
NetBeans Swing GUI Builder
The software project takes advantage of the NetBeans built-in feature for creating graphical user interface the Swing GUI builder
Although many Java programmers dislike this function because you can loose the control of the generated code, in my opinion the Swing GUI builder is the only way for designing an awesome graphical user interface for your project. You can simply drag and drop the swing components from a palette in your frame. Then you are able to align and space the component to design your GUI. Moreover you can build your own Swing components, as I have done for this project by programming a swing component for the real time chart ChartDynamicData.java
Third-party Java Libraries
openQCM Java application is designed using the third-party Java libraries:
Ardulink the open source Java library for control and communication with Arduino boards. The communication protocol of openQCM Java software is based on Ardulink. Ardulink also provides ready Java Swing components able to communicate with Arduino. In particular, it is used the Java Swing component for the serial connection with Arduino and for serial reading of the quartz crystal frequency and temperature data.
JFreeChart the open source Java libraries for displaying chart. openQCM software uses JFreeChart for dynamic displaying real time chart of frequency and temperature data.
Commons Math the Apache commons mathematics library. openQCM uses Commons Math mainly for real-time data processing. In particular the measurement of the quartz crystal frequency is affected by glitches, mainly caused by the algorithm for counting the number of pulses in a fixed interval of time. The frequency data are averaged over an interval of fixed data points for smoothing the glitches.
Connection to the Arduino Board
The openQCM communication protocol is based on Ardulink.
The Link class is the most important Ardulink class for communication between your computer and the Arduino board connected at the serial port portCOM. The connection and communication between the Arduino board and PC is implemented in the sequence of instructions:
The Java mainGUI class implements the Ardulink interface RawDataListener The method of the class returns the string read at the serial port
The data string is formatted as “RAWMONITOR_frequency_temperature” . The string starts with “RAWMONITOR” in order to check if the message contains new data, in this way you can avoid any spurious message at the serial port.
Plot Data using Dynamic Chart
The frequency and temperature data read at the serial port are plotted in real time using the CombinedDomainXYPlot class of JfreeChart library. The dynamic chart is implemented in the class ChartDynamicData.java The dynamic chart plot the data versus the current time, using a first axis for the frequency and a secondary axis for the temperature. The ChartPanel is not currently a JavaBean, so you can't manipulate it with a GUI designer tool (see JfreeChart blog post at this link) I have added the ChartPanel to a Jpanel, so when you compile the project you can use the ChartDynamicData.java as a Swing component and manipulate it with the GUI designer.
The ChartDynamicData class was developed implementing these examples on java2s.com
JFreeChart: Dynamic Data Demo 3: how to dynamically add data to a time series chart.
JFreeChart: Combined XY Plot Demo 4: how to add a second axis to combined XY plot
Data processing concerns mainly in an algorithm for smoothing the real time frequency and temperature data. The main goal of data processing is the smoothing of frequency glitches, which are caused by the algorithm for counting the number of pulses in a fixed interval of time.
To this aim I have used a moving average over a fixed time interval. The moving average is calculated as a standard mean on a FIFO circular buffer, which is implemented in the class ArrayCircularBuffer.java
The class was developed based on the work of Brad's Source Code Blog which is the most efficient example on circular buffer I have found in the internet (massive congrats and thank you Brad).
The frequency and temperature can be saved on a text file in ASCII format. You can select the data file using the JFileChooser in mainGUI.java
You can select an existing file or create a new one. Once the data file is selected the software append the new data into the file. The data file is formatted as follows
date (MM/dd/yy) hour (hh:mm:ss) frequency(%.1f) temperature(%.1f)
These are the main resources used for the development of the openQCM Java application plus a summary the references used overall in the post
openQCM open source project
openQCM the open source quartz crystal microbalance
openQCM Java software project repository
openQCM Java project download zip
Java JDK + NetBeans
Download Java Development Kit JDK 8u31 and NetBeans 8.0.2 at this link
Java chart library for displaying charts http://www.jfree.org/jfreechart/
How to implement an efficient circular bufer in Java