Application note aims to help you with the implementation of the Kamstrup Multical 602 (or 603 thank's to the similarities with previous version) and read it's serial number (as an example of adressing registers).
To connect Modbus module to Acrios Modbus/RS485 converter, use RS485 terminal (A and B) of the module and connect it to terminal marked as MBUS + (= A) and MBUS - (= B). Don't get mistaken by MBUS marking, it's Modbus/RS485 converter.
Modbus request has 6 bytes. The frame contains device address (1 B), function code (1 B), register address (2 B) and register address (2 B)
Here's an example with description.
Dev. address | Func. code | Reg. address | Reg. address | Reg. count | Reg. count |
0xE3 | 0x03 | 0x00 | 0x98 | 0x00 | 0x02 |
Device address - Hexadecimal interpretation of decimal number 227 (last 3 numbers of serial number - for Kamstrup multical address is usually last 2 or 3 numbers)
Function code - 0x03 means “Read Holding Registers”
Register address - Calling defined register address with desired information (0x0098 contains serial number)
Register count - Number of registeres to read
Register address and Register count is defined by the 602 datasheet or 603 datasheet and datagrams - where you can find the register adrdress and number of modbus registers for the meter value (#Regs = Reg. count) .
For example let's use default LUA script:
-----------------------
--- CONFIGURATION -----
-----------------------
----- LoRaWAN ----------
ack = 0 -- 1 for acknowledged, 0 for non-acknowledged
port = 100 -- transmit port
receiveTimeout = 10000 -- the maximum execution time in milliseconds
----- Modbus -----------
baudrate = 9600 -- baudrate: up to 921600 baud
parity = 0 -- communication parity: 0 for none, 1 for odd and 2 for even parity
stopBits = 1 -- number of stop bits: 1 or 2
dataBits = 8 -- number of data bits: 7 or 8
rxTimeout = 500 -- slave device receive timeout in ms
------ Timing ---------
-- device wakeup interval
periodHours = 2
periodMinutes = 30
-----------------------
-----------------------
-- CONFIGURATION END --
-----------------------
function onWake ()
-- set link parameters - e.g. 9600 baud, 8N1
api.rs485Setup(baudrate,parity,stopBits,dataBits)
api.rs485State(1)
-- EXAMPLE Modbus request:
-- check our applicaton notes or https://simplymodbus.ca/FC03.htm
-- req = ""
-- SLAVE ADDRESS (0xAA)| FUNCTION CODE (0x04)|
-- MSB REG ADDR (0x00) | LSB REG ADDR (0x08) |
-- MSB REG COUNT (0x00) | LSB REG COUNT (0x01)
req = pack.pack('<b6', 0xE3, 0x03, 0x00, 0x98, 0x00, 0x02)
crc = api.modbusCrc(req)
req = req .. crc -- add CRC, request is complete and ready for sending
if req == nil then
print("Please provide configuration!")
ans = "NO CONFIGURATION PROVIDED!"
else
print("To RS485: ")
api.dumpArray(req)
api.rs485Send(req)
ans,length=api.rs485Receive(rxTimeout)
print("From RS485: ")
api.dumpArray(ans)
api.delayms(500)
api.rs485State(0)
end
if #ans < 1 then
buf = "NO DATA RECEIVED"
else
buf = ans
end
print("To LoRaWAN: ")
api.dumpArray(buf)
print("Sending to LoRaWAN")
res, rxport, rcvd = api.loraSend(ack, receiveTimeout, buf, port)
print("Done sending")
print("Sleep now, wake in " .. tostring(periodHours) .. "hrs:" .. tostring(periodMinutes) .. "mins.....")
api.wakeUpIn(0,periodHours,periodMinutes,0)
end
function onStartup()
print("Starting up LoRaWAN default Modbus script")
end