Modbus RTU Example with ECS Modbus Energie Counter

Place code snippets and demo code here
Post Reply
Helmut_number_one
Posts: 122
Joined: Fri Dec 09, 2022 10:03 am
Has thanked: 53 times
Been thanked: 14 times

Modbus RTU Example with ECS Modbus Energie Counter

Post by Helmut_number_one »

This a little example for Modbus RTU
https://www.janitza.de/files/download/m ... -de-en.pdf

Code: [Local Link Removed for Guests]

'Example of MODBUS RTU with the L1,L2,L3 to N Volage, Power_now, Power_consum 
READ_HOLD_REGISTER  = 3 '
READ_INPUT_REGISTER = 4 ' 


SERVER_ID = 1

onWgetAsync modbus_received 'set the event handler function
'MODBUS.SetupRTURX_pin, TX_pin, RE_DE_pin, ["BBBB,P,D,S"]
modbus.setupRTU 3,1,22,"19200,N,8,1" ' by default is 9600,N,8,1

do
'Format : modbus.requestRTU Token Server_ID, Read_xxxx_Register Modbusadr , 16bit=1 oder 32Bit=2
modbus.requestRTU 4267, SERVER_ID, READ_HOLD_REGISTER, 4267 , 2
pause 100
modbus.requestRTU 4269, SERVER_ID, READ_HOLD_REGISTER, 4269 , 2
pause 100
modbus.requestRTU 4271, SERVER_ID, READ_HOLD_REGISTER, 4271 , 2
pause 100
modbus.requestRTU 4157, SERVER_ID, READ_HOLD_REGISTER, 4157 , 2
pause 100
modbus.requestRTU 4131, SERVER_ID, READ_HOLD_REGISTER, 4131 , 2
pause 100
'wlog "1"
  pause 1000
loop

end 

modbus_received:

r$ = WGETRESULT$
token$ = word$(r$, 1) 'extract the token (first word)

if (token$ ="4267") then  ' if the token correspond to the read request
  if (word$(r$, 2) <> "Error") then  'if is not an error
    'assemble the 4 bytes in one 32 bits word
    value$ = "&h"+ word$(r$, 5) + word$(r$, 6) + word$(r$, 7) + word$(r$, 8)
    Ergebnis1= val(value$)
    Ergebnis =  ( CONVERT.FROM_IEEE754(Ergebnis1) )    
    Ergebnis4267$ = str$(Ergebnis,"%3.2f") + " V Spannung L1"
    wlog Ergebnis4267$
  end if
endif


if (token$ ="4269") then  ' if the token correspond to the read request
  if (word$(r$, 2) <> "Error") then  'if is not an error
    'assemble the 4 bytes in one 32 bits word
    value$ = "&h"+ word$(r$, 5) + word$(r$, 6) + word$(r$, 7) + word$(r$, 8)
    Ergebnis1= val(value$)
    Ergebnis =  ( CONVERT.FROM_IEEE754(Ergebnis1) )    
    Ergebnis4269$ = str$(Ergebnis,"%3.2f") + " V Spannung L2"
    wlog Ergebnis4269$
  end if
endif

if (token$ ="4271") then  ' if the token correspond to the read request
  if (word$(r$, 2) <> "Error") then  'if is not an error
    'assemble the 4 bytes in one 32 bits word
    value$ = "&h"+ word$(r$, 5) + word$(r$, 6) + word$(r$, 7) + word$(r$, 8)
    Ergebnis1= val(value$)
    Ergebnis =  ( CONVERT.FROM_IEEE754(Ergebnis1) )    
    Ergebnis4271$ = str$(Ergebnis,"%3.2f") + " V Spannung L3"
    wlog Ergebnis4271$
  end if
endif
if (token$ ="4157") then  ' if the token correspond to the read request
  if (word$(r$, 2) <> "Error") then  'if is not an error
    'assemble the 4 bytes in one 32 bits word
    value$ = "&h"+ word$(r$, 5) + word$(r$, 6) + word$(r$, 7) + word$(r$, 8)
    Ergebnis1= val(value$)
    Ergebnis =  ( CONVERT.FROM_IEEE754(Ergebnis1) )    
    Ergebnis4157$ = str$(Ergebnis,"%5.1f") + " W Momentleistung"
    wlog Ergebnis4157$
    'i want it as json Put string, but nothing....
    'a$ = WPOST$ ( "192.168.0.104/" ,  { "total": Ergebnis4157$ } , 80 )
  end if
endif
if (token$ ="4131") then  ' if the token correspond to the read request
  if (word$(r$, 2) <> "Error") then  'if is not an error
    'assemble the 4 bytes in one 32 bits word
    value$ = "&h"+ word$(r$, 5) + word$(r$, 6) + word$(r$, 7) + word$(r$, 8)
    Ergebnis1= val(value$)
    Ergebnis =  ( CONVERT.FROM_IEEE754(Ergebnis1) )    
    Ergebnis4131$ = str$((Ergebnis/1000),"%6.1f") + " kWh Gesamtleistung"
    wlog Ergebnis4131$
  end if
endif
return
Post Reply