The project I’m working on is about making automation system using an ARM Cortex A8 1Ghz Android based development board and slave bm6PTI that communicates using rs485 and modbus.
Acquire distinctly 6 temperature probes mod. PT100 class B through an ‘expansion into RS485 using Modbus communication at a speed of 19200.
If desired, it is possible to extend the application adding other expansion modules up to a total of 63 bm6PTI with a ‘capture 378 temperature probes.
The prerequisites are the following:
- Eclipse IDE,
- Eclipse ADT plugin,
- Android SDK
Basically, the Android project is formed by the following files:
- on the /src folder, the main Activity and the Java class that defines the modbus signatures for the external modbus library
- on libs/armeabi folder, the modbus library
- on res/layout, the layout of the main activity
- on res/values folder, string.xml file
- on the main folder, the AndroidManifest.xml
[sourcecode language=”xml”]
<AbsoluteLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="wrap_content"
android_layout_height="match_parent"
tools_context=".MainActivity" >
<TextView
android_id="@+id/TextView01"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="-346dp"
android_layout_y="7dp"
android_text="MAX"
android_textAppearance="?android:attr/textAppearanceSmall"
android_textColor="@color/red" />
<RelativeLayout
android_id="@+id/relativeLayout1"
android_layout_width="match_parent"
android_layout_height="wrap_content" >
</RelativeLayout>
<TextView
android_id="@+id/textView1"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="37dp"
android_layout_y="11dp"
android_text="ANALOG 1"
android_textAppearance="?android:attr/textAppearanceMedium"
android_textSize="18dp" />
<TextView
android_id="@+id/textView2"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="245dp"
android_layout_y="11dp"
android_text="ANALOG 2"
android_textAppearance="?android:attr/textAppearanceMedium"
android_textSize="18dp" />
<TextView
android_id="@+id/textTemp2"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="11dp"
android_layout_y="23dp"
android_text="0"
android_textColor="@color/red"
android_textSize="82dp" />
<TextView
android_id="@+id/texTemp1"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_x="229dp"
android_layout_y="28dp"
android_text="0"
android_textColor="@color/red"
android_textSize="82dp" />
<TextView
android_id="@+id/TextTemp4"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_x="457dp"
android_layout_y="28dp"
android_text="0"
android_textColor="@color/red"
android_textSize="82dp" />
<TextView
android_id="@+id/textView3"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="479dp"
android_layout_y="11dp"
android_text="ANALOG 3"
android_textAppearance="?android:attr/textAppearanceMedium"
android_textSize="18dp" />
<TextView
android_id="@+id/textTemp3"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="12dp"
android_layout_y="213dp"
android_text="0"
android_textColor="@color/red"
android_textSize="82dp" />
<TextView
android_id="@+id/TextView02"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="246dp"
android_layout_y="192dp"
android_text="ANALOG 5"
android_textAppearance="?android:attr/textAppearanceMedium"
android_textSize="18dp" />
<TextView
android_id="@+id/TextView03"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="50dp"
android_layout_y="192dp"
android_text="ANALOG 4"
android_textAppearance="?android:attr/textAppearanceMedium"
android_textSize="18dp" />
<TextView
android_id="@+id/TextTemp6"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="230dp"
android_layout_y="213dp"
android_text="0"
android_textColor="@color/red"
android_textSize="82dp" />
<TextView
android_id="@+id/TextTemp5"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_x="452dp"
android_layout_y="213dp"
android_text="0"
android_textColor="@color/red"
android_textSize="82dp" />
<TextView
android_id="@+id/TextView04"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="480dp"
android_layout_y="192dp"
android_text="ANALOG 6"
android_textAppearance="?android:attr/textAppearanceMedium"
android_textSize="18dp" />
[/sourcecode]
The ModbusLib class simply declares the signatures of the modbus library functions that will be used in the Android project.
[sourcecode language=”xml”]
package com.biemme.modbus;
public class ModbusLib {
public native static int openCom(int baudrate, long rtimeout, long wtimeout);
public native static long ReadHoldingRegisters(int fd, int id, int address, int no_of_registers,int []holdingRegs);
public native static int closeCom(int fd);
static{
System.loadLibrary("com_biemme_modbus_ModbusLib");
}
}
[/sourcecode]
It’s now time to analyze the java code that manages the main activity (called MainActivity.java). As a matter of clarity, I’ll comment out each key part of the class separately.
[sourcecode language=”xml”]
public class MainActivity extends Activity implements OnClickListener {
[/sourcecode]
The first method that is triggered (by an intent) when the windows is open is the OnCreate. It simply set the content view (i.e., it explicitly connects the view to the controller) and links the graphical elements like text, image view to the referring class’ private attributes (in such a way they can be referenced back later on in the class).
[sourcecode language=”xml”]
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
temperature1= (TextView) findViewById(R.id.texTemp1);
temperature2= (TextView) findViewById(R.id.textTemp2);
temperature3= (TextView) findViewById(R.id.textTemp3);
temperature4= (TextView) findViewById(R.id.TextTemp4);
temperature5= (TextView) findViewById(R.id.TextTemp5);
temperature6= (TextView) findViewById(R.id.TextTemp6);
}
[/sourcecode]
The following onResume method will be executed when the activity first starts (but after the onCreate) or after it is paused. It contains the methods that open the device’s serial port and stores the file id into a private variable.
[sourcecode language=”xml”]
protected void onResume() {
super.onResume();
if (fid == 0){
fid = ModbusLib.openCom();
Log.d("uart=",String.valueOf(fid));
}
cancelReadThread();
workingthread=new ReadsWrites();
workingthread.start();
}
[/sourcecode]
In the next post we explain how, in a simple command line, you can save the temperature in a sd so as to realize a data logger