Sie sind auf Seite 1von 19

Contents

1. Introduction. .................................................................................................................................. 2
2. Problem Description. ..................................................................................................................... 3
- Requirements. ............................................................................................................................ 3
3. Methods.......................................................................................................................................... 4
4. Analysis. ......................................................................................................................................... 4
5. Implementation.............................................................................................................................. 5
5.1. Functional modeling............................................................................................................... 5
5.2. The Program. ......................................................................................................................... 7
5.2.1. The AgroLogTMS2500 application................................................................................ 7
5.2.2. The Graphics Application. ........................................................................................... 14
6. Tests. ............................................................................................................................................ 18
7. Conclusion.................................................................................................................................... 19
8. Perspectives.................................................................................................................................. 19

1
1. Introduction.

The goal of this project is building a data logging software for the AgroLog TMS 2500
device. The device is used for monitoring temperatures in grain silos. It can be connected to a
number of up to 250 sensor lines, each line having a maximum of 20 sensors. Lines are
placed inside silos. When connected to them the AgroLog device will collect all the
temperature information and store it in memory. The project is divided in two parts. One part
of the project consists of building a computer software application that will take the
temperature information from the device and store it inside a database. The second part is
building a graphical user interface (GUI) in order to let users access the temperature
information and allocate sensor lines to silos or dispose them from the silos.

Figure 1: Project description.

2
The AgroLog TMS 2500 is propriety of Supertech Agroline: www.supertech.dk

2. Problem Description.

Monitoring the temperatures of stored grain is a practice that will ensure the quality of the grain.
If a temperature is above a certain level the cause of that might be the appearance of certain
insects in the grain storage, insects that can damage the quality of the grain. This is why it’s good
to know from the beginning when something can threaten the production and take immediate
action to stop any possible threats.

- Requirements.

Supertech Agroline produces a handheld data logger with USB-interface. These are the
requirements as they were received from Supertech:

• The data logger is used for measuring temperatures in grain storage (silos).

• Temperatures from 1 to 250 Sensor Lines with each from 1 to 20 temperature Sensors
can be logged. Each log has a time stamp.

• The logged temperatures must be transferred from the handheld data logger to a PC and
stored in a database.

• The logged data can then be presented as curves and tables. It must be possible to scroll
the datasets in the curves and tables.

• The clock in the handheld data logger must be set from the PC-program.

• The protocol at the USB interface is defined.

• The data logger specifies Sensor Lines and Sensors. In the PC-program it must be
possible to allocate Sensors Lines to silos or buildings.

• Internet communication could be an option.

3
3. Methods.

The software is being realized using the Java programming language due to its vast portability
and the many easy to use features. Important libraries that are used are the MySQL-Connector/J
together with Connector/MXJ and RXTX.

MySQL-Connector/J is used for accessing MySQL database servers, handling and managing
database information and tables.

Connector/MXJ offers the possibility to embed MySQL servers inside any Java application
starting them with plenty of custom options.

RXTX is a library with many features that allow communication over different types of ports:
parallel, serial, I2C, RS485.

4. Analysis.

The problem was analyzed mainly based on the company’s (Supertech) requirements (page 3).

After the requirements were set clear, the technologies were chosen. The choices were: .NET
using C#, Java and LabVIEW. Due to the lack of documentation time C# and LabVIEW were
not chosen, but .NET still doesn’t offer such portability as Java does anyway. Java was chosen
thanks to the advanced Java knowledge accumulated so far.

The work is mostly done in NetBeans IDE 6.8 – a specially designed IDE for Java. NetBeans
makes the work with javax.swing forms very easy.

4
5. Implementation.

5.1. Functional modeling

Following next are the Use-Case diagrams describing what the system can do and how it can
be used:

Figure 2: Data logging from the user’s


side.

Self evident – all that the user has to do


after the temperatures are stored in the
AgroLog’s memory is connect the
device to a computer on which the
AgroLog TMS 2500 software is
running. The program will do the rest
of the job

After the user connects the device to an USB port from the computer, the program will
automatically detect the device by its response to the Timestamp update message sent by the
program. If the device responds with the appropriate message, then the computer will continue
the communication and request the temperatures. These use cases are described in Figure 3.

The temperatures are then stored in a database table after the program checks whether the
timestamp for each line is not already existing inside the table. This is to prevent duplicated
values inside the table.

5
Figure 3: Data logging from the computer application side.

Figure 4: Graphical User Interface Use-Case.


Describes how the user can interact with the GUI.

6
5.2. The Program.

As mentioned before, the software package contains two applications. One of them handles the
communication over the serial port and puts the temperature information into the database. This
one was named after the name of the device: AgroLog TMS 2500 and its class diagram looks
like this:

5.2.1. The AgroLogTMS2500 application.

Figure 5: AgroLog TMS 2500 application class diagram

The program starts with the main method of the PortCheckerThread class. An object of this
class is instantiated as a thread and starts running. This thread actually controls the entire
program. It instantiates an object of the CommMessageProcessor class through which the
communication and message processing over the serial port is realized.

After the program starts it enters an infinite loop and inside of this loop it looks for all the
available serial ports. The application connects to each port, one at a time, sends the timestamp
and waits for a response. If the response from the serial port is valid and the checksum received
in the response matches the calculated checksum then the communication continues. If not, then

7
the port is closed and the computer connects to the next available serial port. When the
communication continues after the checksum check, the computer sends the temperature request
and waits until the response is processed.

By using a flowchart Figure 6 makes a short conceptual description of the algorithm on which
the main thread of the program (the object of the PortCheckerThreadClass) is running.

Figure 6: Flowchart describing the run() method of the PortCheckerThread

8
In order to explain the CommMessageProcessor class a few aspects have to be taken into
consideration first. This class starts the serial communication over the ports that are supplied by
the PortCheckerThread object.

The class SerialCommunication is on the lowest layer of this application. It sets up the
connection, it starts it and it ends it. It has two inner classes handling the message transfers over
the serial port. One class is for sending messages - SerialWriter and the other one for receiving -
SerialReader. With this class most of the raw data handling is done: taking bytes, converting
them to characters and ordering them in strings in order to be sent to the
CommMessageProcessor object and it converts strings of characters into byte arrays in order to
be sent to the port. It calculates the checksums and removes or adds characters which are
particular to the protocol of the AgroLog device and which are not relevant to the methods of the
CommMessageProcessor.
SerialWriter is implementing the Runnable interface. An object of this inner class is
instantiated as a thread in the connect() method of the SerialCommunication. Whenever the
command field of this thread changes its value from an empty string to any string, the string is
converted to a byte array. A start character, a checksum and an end character are added to the
bytearray and the message is sent.

The SerialReader implements the SerialPortEventListener interface. Objects of this class will
wait for messages from the serial port, will convert them to strings and upon these strings the
process(String message, char header) method of the CommMessageProcessor will be called.

Aspects of the protocol:

General protocol format of the AgroLog TMS 2500


Start character Type Data Checksum End
1 character 1 character 0 to many 1 byte 1 character
Characters

Start & End Character


Start character (ascii)
SOH (0x01) Start of a command/request/response
Hyperterminal ctrl-shift-A
ETB (0x17) Start of a command/request/response
Hyperterminal ctrl-shift-W

9
The Type character specifies the type of data that the message contains. A message sent from the
computer to the AgroLog device that starts with ‘T’ tells the device that the characters following
‘T’ are values for the new timestamp. If the response message from the AgroLog has the Type ‘t’
that means the timestamp update was successful. If the application sends ‘A’ then the
temperatures are requested. If the response has the type ‘a’, that means the following string of
characters contains temperature information in the following format:

<SOH>a(LineNumber)(SensCount)(TimeStamp)(Temp1)(Temp2)….

(LineNumber)(SensCount)(TimeStamp)(Temp1)(Temp2)… (Checksum)< ETB >

a Type = temperature response


LineNumber Sensorline number (001-255)
SensCount Number of Sensors in the SensorLine (01-20)
TimeStamp YYYYMMMDDDHHHMMM
(Temperature) Temperature
(Temperature)
(Temperature)

(Checksum) Sum of all the received bytes starting from the first character
and ending with the character before the checksum. The sum is
truncated to 1 byte and the result is the Checksum

Having these protocol aspects known, the flowchart in Figure 7 conceptually describes how the
messages are handled by the process() method of the CommMessageProcessor class.

The SQLEmbedded class is built for embedding a MySQL server into the application and
managing a MySQL database. It uses the Connector/MXJ and MySQL-Connector/J libraries.
Methods of this class are specially designed for this data logging application. The requirements
for this application are not many from the database point of view and as a consequence it’s not a
complex class.

The Time class contains methods for retrieving the current time form the computer and for using
different time values under different forms. In the context of this application the Time class is
used to update the timestamp of the AgroLog.

Display is a JScrollPane used mostly for debugging purpose. It contains a JTextArea in which
debugging results are displayed.

10
Figure 7: Flowchart describing the process(String,char) method
of the CommMessageProcessor

11
• The Database.

Figure 8.Database description.

There is no relationship set between tables and no normalization. All the constraints are handled
by the applications. When the AgroLogTMS2500 application runs for the first time on a
computer, it starts a MySQL server, creates a user name ‘AgroLog’ identified by ‘4grolo6’
password, it creates then the database and the tables. Every time when the application starts
running it checks whether each table can be found. If any of the tables can’t be found, the
application creates it.

line_to_silo_allocation is the table that specifies which sensor line is allocated to which silo.

readings contains all the read temperature values for each sensor of each sensor line that is
allocated.

sensor_lines contains the existent sensor lines IDs with the number of sensors of each line.

silos contains all the existent silo IDs together with a description for each silo.

Next is the method that processes the messages that came from the Serial Port:

/* method that processes the message string which came from the serial port*/

public synchronized void process(String message,char header)


{

receivedMessage=new String(message);
answerReceived=true;

if (header!='a')
if ((command.contains("T")) && (header!='t')) { //what to do when the headers don't match

12
answerReceived=false;
this.disconnect();

}
if(header=='a'){ //what to do when message containing the temperatures is received

String year, month,day,hour,min;


int lineNo, sensorQty; //info from message
float[] temperature=new float[20];
int i, first , last; //for incrementations
first = 0;
last = 3;

while(last<message.length())
{

lineNo = Integer.parseInt(message.substring(first, last));


sensorQty = Integer.parseInt(message.substring(first+=3,last+=3));
year = message.substring(first+=3,last+=4);
month = message.substring(first+=4, last+=3).substring(1,3);
day = message.substring(first+=3, last+=3).substring(1,3);
hour = message.substring(first+=3, last+=3).substring(1,3);
min = message.substring(first+=3, last+=3).substring(1,3);

sqlServer.lineInDatabase(lineNo, sensorQty);
boolean validData=sqlServer.dataIsValid(year+month+day+hour+min+"00", lineNo);
//checking whether the data is already in the database or not
for(i=0;i<sensorQty;i++) {
temperature[i]=(Integer.parseInt(message.substring(first+(i+1)*3, last+(i+1)*3))-40)/2;
if(validData)
// if data is not in the database already then it has to be added
sqlServer.add("INSERT INTO readings(line_id, silo_id, times, sensor, temperature) " +
"values ("+lineNo+", "+sqlServer.getSiloWhereIsLine(lineNo)+",
"+year+month+day+hour+min+"00 , "+(i+1)+", "+temperature[i]+")");
else System.out.println("---------->>>>>>>INVALID DATA!<<<<<<<<--------- ");
}
first+=(sensorQty+1)*3;
last +=(sensorQty+1)*3;
}

JOptionPane.showMessageDialog(null, "The data was processed. You can disconnect the device now. Click OK
when disconnected!");
dataLogged=true;

13
5.2.2. The Graphics Application.

Figure 9.

The MainFrame class is built using the javax.swing library. It’s the main class of this
application and the graphical user interface.

The DrawPanel is a class inheriting form javax.swing.JPanel and overriding the


paintComponent and getPreferredSize methods of the JPanel class. The paintComponent will
paint the temperature curves for given periods of time.

The Reading class is as simple as this:

public class Reading

public String time;

public int Temperature;

14
It’s meant to describe temperature sensor readings at certain moments in time. An ArrayList with
elements of this class is used in order to display the temperature curves in an instance of the
DrawPanel class.

In an instance of the class Manager, the connection to a MySQL server is established to the
AgroLog database. The Manager class contains methods for handling MySQL requests, queries
and results. All the information regarding temperature readings is taken from the database using
the sqlHandler object of this class which is actually a field in the MainFrame class, line to silo
allocation and silo descriptions are as well managed by the GUI through this object.

Figure 10. Graphics tab

In the Graphics tab the user can choose through silos, sensor lines, sensors and different time
periods in order to get a graphical representation of the temperatures over the selected periods.

15
Figure 11. Readings tab.

In the Readings tab the user can view in a table the temperatures for the selected options
available in the lists above the table. Each click in any of the above 6 lists will update the
Temperature Readings table.

16
Figure 12. Decisions tab.

In the Decisions tab the user can chose between unallocated lines of sensors and allocate them to
the available silos.

The user does not need to add lines of sensors to the database manually. In case a new line will
be connected to the AgroLog device, when the temperatures wil be read form the device, the new
lines will automatically be added to the lines table in the database together with their number of
sensors.

The user can add silos with their descriptions, remove silos from the database and change the silo
descriptions.

17
6. Tests.

Tests were conducted while implementing. Every class and method was tested right after its
implementation or even while implementing where possible. When new changes affected old
parts of the program leading to errors or malfunction, the affected parts were immediately
corrected to fit to the rest of the program and meet the requirements.

No errors or malfunctions were found in the AgroLogTMS2500 application as it is at this


moment. When making the SerialCommunication class, the serial port could not be closed by
using its close() method but a workaround has been found. This consists in making a thread
especially for closing the port. The reason why the close() method doesn’t work as it should on
some computers still remains unknown. Maybe the problem will be fixed in a future version of
the RXTX libraries. The communication between the computer and the AgroLog device is done
with no problems: the timestamp update works, request for temperatures receives the proper
response, the temperatures are stored into the database without errors.

The GUI works as expected.

Only one of the JLists in the Graphics tab has a mouse click event listener attached to it and it
works without problems. All the selected values from the lists are put together in a query and
sent to the MySQL server. The reply from the server is stored into an array of objects and copied
into an ArrayList with elements of Reading type and then passed to the DrawPanle instance and
the repaint() method of this object is called to repaint the curves in for the new values.

In the Readings tab again for each click in every one of the six JLists queries are composed
containing the options from the JLists and table is renewed according to the user’s selections. No
errors occur and the results are as expected.

The Decisions tab works with no problems.

18
7. Conclusion.

Most of the requirements have been met entirely. The Graphics tab of the GUI was not entirely
finished but it was done in a way that can prove it’s possible to do it, but it just takes more time.
The mouse click event listeners for all the six lists have to call the updateGraph method. So far
only the list for the months does this. The values for the curves should be only the maximum
temperature values. At the moment all the values are displayed.

8. Perspectives.

A new table could be useful in the Database of this software, a table that would keep a history of
the line to silo allocation. This could enable the user to see the temperatures that were read by
some lines after the lines have been moved to other silos. Switching sensor lines between silos is
not something that can occur often. Maybe it wouldn’t happen in years, but adding this feature
would cover more possible situations that could occur.

Internationalization of this software might bring an advantage for the users and a short Help
documentation would be necessary.

I can add to the GUI the possibility of choosing IP’s from different computers’ MySQL servers
and connect to them. Making a web server could also be possible and connect to it from any web
browser to view graphs and temperature values.

During the work on this project I found the Connector/MXJ library. After I found it I started
having many ideas about what I can do with it. The same thing happened after I made the
DrawPanel class because I learned a little bit about graphics in Java.

19

Das könnte Ihnen auch gefallen