
Application Note
Embedded Applications
Automatic meter reading with
M bus

First edition (Novemeber 2004)
Sony Ericsson Mobile Communications. publishes this manual without
making any warranty as to the content contained herein. Further Sony
Ericsson Mobile Communications. reserves the right to make modifications,
additions and deletions to this manual due to typographical errors, inaccurate
information, or improvements to programs and/or equipment at any time and
without notice. Such changes will, nevertheless be incorporated into new
editions of this manual.
All rights reserved.
© Sony Ericsson Mobile Communications., 2004

METERING AND M BUS
Contents
Contents.....................................................................................................3
1 Introduction ........................................................................................4
2 MBUS support in Embedded Applications .........................................5
2.1 Implementation..........................................................................................5
2.1.1 Hardware Setup.............................................................................6
2.2 The Script ..................................................................................................6
2.3 Testing the Application ..............................................................................7
2.4 Flexibility....................................................................................................7
2.5 Metering Application Script {MBUS.sc}......................................................8
3 References........................................................................................ 11
LZT 123 8014 R1A 3

METERING AND M BUS
1 Introduction
This application note describes the use of SPI bus with
embedded applications and how additional components
can be added simply to the radio device’s existing
interfaces to provide an extra level of functionality. This is
done in the context of an application which uses MBUS as
its communication mechanism, the Metering Bus standard
(See Ref. 1 of this application note). The script contained
in this document uses the Demo Board attached to the
Development Board.
LZT 123 8014 R1A 4

METERING AND M BUS
2 MBUS support in Embedded Applications
MBUS is a standard communications mechanism that
covers low and high layers of the OSI stack model. In
order to communicate MBUS packets need to be sent
from one device to another, there is a defined MBUS
packet format. The data has to be sent as 1 start, 1stop, 8
data bits and even parity. To do this, the radio device’s
programmable SPI bus may be attached to a MAX3100
SPI-UART IC.
A software SPI implementation is provided that utilizes
four I/O pins. This allows the communication of MBUS
data to the MAX3100 SPI-UART IC, which then constructs
the packets into the correct framing with the parity bits.
This can then be communicated to a metering device. The
embedded application has driver function calls for the
MAX3100 device that allow simple configuration of and
communication through the device. A further IO line is
used to service interrupts from the MAX3100.
Only the physical layer data framing is done with the driver
functions. The higher layers of the MBUS protocol need to
be implemented in the script. Checking whether the
incoming MBUS packets are valid and processing the
buffers is also implemented at script level for complete
flexibility.
2.1 Implementation
The example application receives MBUS packets in a data
call and sends them out to any connected metering
device. The response packets are then read in and
checked, before being communicated back up the data
channel to the remote host.
Overall system description
LZT 123 8014 R1A 5

METERING AND M BUS
2.1.1 Hardware Setup
In order to communicate with an MBUS device, the
following setup is required.
Hardware description
A SIM with a data phone number is required, so that a
CSD call can be made from a remote host to the radio
device.
2.2 The Script
The fully commented script listing can be found below. For
full descriptions of the individual script function calls,
please refer to the IDE help file.
The script application initially sits in a loop, waiting for an
incoming data call that requests an MBUS session be
setup.
Once a call is received and checked to be from the correct
number, the module answers the call and calls the
DoMBUS() function. This function creates a channel. The
channel goes to on-line mode and can carry the MBUS
traffic. Data is received from the channel, checked to be
valid MBUS data by the GetMBUSData() function and sent
forward to the MBUS slave device via the M3100. The
response data is received (must be received within 1 sec
timeout) and transferred back up the channel in the same
manner. In this way, the remote host can talk with one or
LZT 123 8014 R1A 6

METERING AND M BUS
more connected MBUS slaves using a single radio device.
The validation procedure ensures no invalid packets are
received or sent.
When the communication is finished, the call is cleared
and the channel is closed down. The script goes back to
monitoring for an incoming CSD call to initiate the next
data session.
2.3 Testing the Application
In order to confirm the working application, the RS232
outputs from the level shifters can be connected via a 9pin D-type header to a PC Com port. A suitable terminal
application that can send data in hexadecimal (Hex) can
be used, setting the UART configuration to the MBUS
speed and parity settings. A shareware tool that can be
used for Hex transfer is “RS232 Hex Com Tool” from
www.viddata.com
. The debug prints from the application
can be monitored if the service pin is asserted on the radio
device and UART2 is monitored at 115200 baud (8 data, 1
start, 1 stop bit). The script field for DATA_NUMBER will
need to be modified to reflect the data calling number.
2.4 Flexibility
This application demonstrates the kind of simple transfer
application that can be written for the radio device,
whereby data in some form is received by the radio
device, and is communicated across a different interface
to the slave, and vice versa. All that is required is the code
to transfer the data from one interface to the other.
Possible modifications to this script include:
• Changing the communication mechanism from CSD to
TCP/IP (GPRS based), or SMS.
• Changing the interface from MBUS to SPI, standard
UART or I2C.
• Sourcing the data call from the radio device, triggered by
an incoming SMS or call.
LZT 123 8014 R1A 7

METERING AND M BUS
2.5 Metering Application Script {MBUS.sc}
/*globals*/
int MAXCFGWORD = 0x2CC4; /* Check this */
int MAXDATA = 30;
int CALL_STATUS = 12;/*byte is status register*/
int RINGING = 1;/*CALL STATUS value*/
char DATA_NUMBER[12] = “07867900940”; /*change this to your number*/
int CALL_COUNT = 1000; /* wait for Data Connection Count */
int GETLOOPCNT = 100; /* check data call status every 100 loops */
int REC10MSCNT = 10; /* 100ms receive wait */
char Recp[261];
char iRec[261];
int st;
/*operates on Recp and iRec*/
GetMBUSData (int RcNotRsp)
{
/*Get MBUS data from Channel or Device*/
/*Contains MBUS packet validation algorithm*/
int Size = 0;
int index = 0;
int di = 0;
int len = 0;
int cnt = 0;
while(st != 0) /*Possibly check on data call status as well?*/
{
if(di >= Size)
{
Size = 0;
if(RcNotRsp > 0)
{
Size = chr(1,iRec,255); /*read more data from
channel*/
}
else
{
Size = mxr(iRec,261);
}
di = 0;
}
/*validate the MBUS packet*/
if(Size > 0)
{
Recp[index++] = iRec[di++];
if(Recp[0] == 0xE5)
{
len = 1;
break;
}
else if(Recp[0] == 0x10)
{
if(index >= 5)
{
index = 0;
if(Recp[4] == 0x16)
{
len = 5;
break;
}
}
LZT 123 8014 R1A 8

METERING AND M BUS
}
else if(Recp[0] == 0x68)
{
if(index > 5)
{
int psz = Recp[1];
if(index >= psz + 6)
{
index = 0;
if(Recp[psz + 5] == 0x16)
{
len = psz + 6;
break;
}
}
}
}
else
{
index = 0;
}
}
if( cnt++ < GETLOOPCNT)
{
cnt = 0; /*check call status every 100 loops*/
st = chsts(1) & 0x02;
}
}
if(st == 0)
{
len = 0;
}
return len;
}
DoMBUS()
{
int Sz;
int Cnt = 0;
/*start transmission */
/* get whole MBUS packet from Data Channel */
Sz = GetMBUSData(1);
/* send bytes over uart */
if(mxs(Recp,Sz) == Sz)
{
prtf(“\n Sent data to Max3100”);
}
Cnt = 0;
while(gtf(MAXDATA) == 0 && Cnt < REC10MSCNT)
{
dlyms(2); /*wait 100ms for data*/
prtf(“\n wt resp!”);
Cnt++;
}
if( Cnt < 10)
{
/* receive whole MBUS packet from Max3100 */
Sz = GetMBUSData(0);
/* send bytes back to data channel */
chw(1,Recp,Sz);
}
else
{
/*no response*/
}
}
LZT 123 8014 R1A 9

METERING AND M BUS
main()
{
int InCall = 0;
int NumBuf[13];
int TmpBuf[255];
prs(0);
chcrt(1); /*Create a channel for send/rcv data*/
prtf(“\n Script Created!”);
/* Setup SPI bus and Max3100 interface */
if(mxc(MAXCFGWORD)) /* Enable,Sync High, every 2 bytes*/
{
prtf(“\n Max3100 Configured!”);
}
while (1)
{
int i;
/*wait for incoming call*/
if(gtb(CALL_STATUS) == RINGING)
{
mset(NumBuf,0,13); /*Clear number buffer*/
clip(NumBuf,12); /* get incoming number*/
prtf(“\n Number = %s”,NumBuf);
if(scmp(NumBuf,DATA_NUMBER,12) == 0) /*if number
matches*/
{
/*answer call*/
chw(1,"ata\n\r",5);
/* wait for data mode */
for(i=0; i< CALL_COUNT;i++)
{
st = chsts(1);
prtf("\n chsts = %d",st);
if (st & 0x02) /* check second bit of status for
DCD */
{
break; /* data carrier detect */
}
dlyms(2); /* wait 100ms */
}
/* clear AT response from channel*/
chr(1,TmpBuf,255);
if(i < CALL_COUNT)
{
/*if count hasn't expired, set to in call*/
prtf(“\n In Call!”);
InCall = 1;
}
}
}
if(InCall)
{
DoMBUS();
st = chsts(1);
InCall = st & 0x02; /*Make sure still in data call*/
}
else
{
prtf(“\n waiting for Call!”);
dlys(1); /* poll every 1 second when out of call */
}
} /*end while*/
}
LZT 123 8014 R1A 10

METERING AND M BUS
LZT 123 8014 R1A 11
3 References
1. MBUS - A Documentation - www.m-bus.com
2. MAX3100 Component data sheet - www.maxim-ic.com