integration from ser.port="/dev/ttyUSB0" to modbus TCP server

92 views
Skip to first unread message

arcan...@gmail.com

unread,
Nov 6, 2016, 3:34:16 PM11/6/16
to modbus-tk
Hi everyone,

I'm very new to the Modbus protocol.

My question is, is it possible with Modbus-tk to read data from ser.port="/dev/ttyUSB0" and forwarding it to the Modbus TCP or RTU (have a USB to RS485) with own register numbers.

Over the ser.port="/dev/ttyUSB0" I'm reading smart meter ( electronic device that records consumption of electric energy in intervals (here in Netherlands)) it is a TTL protocol (I use cable RJ45 to USB)

Now I have a python script running with 4 values I use, and this 4 values I wont to forward to Modbus.
See below;

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# DSMR P1 uitlezer
# (c) 10-2012 - GJ - gratis te kopieren en te plakken
versie = "1.0"
import sys
import serial
################
#Error display #
################
def show_error():
    ft = sys.exc_info()[0]
    fv = sys.exc_info()[1]
    print("Fout type: %s" % ft )
    print("Fout waarde: %s" % fv )
    return

################################################################################################################################################
#Main program
################################################################################################################################################
print ("Slimme Meter Uitlezer")
print ("Control-C om te stoppen")
print ("")


#Set COM port config
ser = serial.Serial()
ser.baudrate = 9600
ser.bytesize=serial.SEVENBITS
ser.parity=serial.PARITY_EVEN
ser.stopbits=serial.STOPBITS_ONE
ser.xonxoff=0
ser.rtscts=0
ser.timeout=None
ser.port="/dev/ttyUSB0"

#Open COM port
try:
    ser.open()
except:
    sys.exit ("Fout bij het openen van %s. Programma afgebroken."  % ser.name)     


#Initialize
# stack is mijn list met de 20 regeltjes.
p1_teller=0
stack=[]

while p1_teller < 20:
    p1_line=''
#Read 1 line
    try:
        p1_raw = ser.readline()
    except:
        sys.exit ("Seriele poort %s kan niet gelezen worden. Programma afgebroken." % ser.name )     
    p1_str=str(p1_raw)
    #p1_str=str(p1_raw, "utf-8")
    p1_line=p1_str.strip()
    stack.append(p1_line)
# als je alles wil zien moet je de volgende line uncommenten
#    print (p1_line)
    p1_teller = p1_teller +1

#Initialize
# stack_teller is mijn tellertje voor de 20 weer door te lopen. Waarschijnlijk mag ik die p1_teller ook gebruiken
stack_teller=0

while stack_teller < 15:
   if stack[stack_teller][0:9] == "1-0:1.8.1":
 print "Dagstand     ", stack[stack_teller][10:15], "kWh"
   elif stack[stack_teller][0:9] == "1-0:1.8.2":
 print "Nachtstand   ", stack[stack_teller][10:15], "kWh"
   elif stack[stack_teller][0:9] == "1-0:1.7.0":
    print "Akt. Verbruik  ", int(float(stack[stack_teller][10:17])*1000), "W"
   elif stack[stack_teller][0:11] == "0-0:96.14.0":
 print "Dag=1/Nacht=2    ", stack[stack_teller][15:16],""
   else:
 pass
   stack_teller = stack_teller +1

#print (stack, "\n")
   
#Close port and show status
try:
    ser.close()
except:
    sys.exit ("Oops %s. Programma afgebroken." % ser.name )   


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Without any script, just directly reading, values with refresh themselves every 10 sec.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I would really appreciate if there is some on who could help me a little bit :)

Auto Generated Inline Image 1
Auto Generated Inline Image 2

Luc JEAN

unread,
Nov 7, 2016, 4:20:48 AM11/7/16
to modb...@googlegroups.com
Hello,

If I understand correctly, you want to make a modbus slave which serves several values. These values are collected over an other USB protocol.

You can create a modbus slave with modbus_tk (look at simulator or to the examples). Then, you need a dedicated thread for collecting the USB data and updating the register values.

I hope it helps
Best
luc

--
You received this message because you are subscribed to the Google Groups "modbus-tk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modbus-tk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages