Reading data from an energy meter via Modbus – Activity diagram in practise

Background: For a data logger application the voltage and real power values should be read from an energy meter using the Modbus RTU protocol. The application runs in the context of collectd1) collectd is a daemon which collects system performance statistics periodically and provides mechanisms to store the values in a variety of ways, for example in RRD files. on a FreeBSD server and stores the values in a RRD database. The program can be called with a command line parameter that defines the data point to read and the serial port to use. The console output is in a format that is directly processed from collectd.

Example call and console output to read the line voltages:

./energy /dev/cuaU0 VOLTAGE PUTVAL "/ENERGIE/UL1" N:239 PUTVAL "/ENERGIE/UL2" N:242 PUTVAL "/ENERGIE/UL3" N:238

Solution: In contrast to the microwave oven example where an activity is called from within a state diagram (see news entry from 24.5.2014) in this example a complete small command line program is modelled. The model was created with Enterprise Architect. Besides the basic design elements like actions, initial – and final nodes also a conditional node was used. This type of node is a convenient block to model multiple choices and actions in one block instead of using multiple choices and action blocks.

To generate code from the model export it into an XMI file using the latest XMI 2.4.1 format (this is important). Then call the code generator the following way to generate C-code from the mode file:

java -jar ../../codegen.jar -p EA -A -t "Model:implementation_model:modbus_client" -o modbus_client modbus_client.xml

And finally compile and link the object with the used Modbus library to create the exe file:

gcc -I/usr/local/include/modbus/ -g strommessung.c -L/usr/local/lib -lmodbus

Thats all! Here is the generated code: modbus_client_activity.c A simple main file calls the generated code:

#include "modbus_client_activity.h" int main(int argc, char* argv[]){ return activity(argc, argv); }

If you are interested in the model file just send a mail! Your thoughts are also very welcome!

Have fun!

Peter

{(rater>id=24062014|name=How do you like this article?|type=rate)}