Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Modbus TCP Tester by Gilles Pignatta & Antonio Villanueva
- import tkinter as tk
- from tkinter import ttk, Frame
- #import crcmod
- import sys
- import codecs
- import socket
- import time
- def callback(eventObject):
- """ callback for combobox function selection """
- if (combo.current()==0): #Read Coil Status (FC=01)
- mbrequest.set(slave_inputport.get()+"01"+address_inputport.get()+value_inputport.get())
- if (combo.current()==1): #Read Input Status (FC=02)
- mbrequest.set(slave_inputport.get()+"02"+address_inputport.get()+value_inputport.get())
- if (combo.current()==2): #Read Holding Registers (FC=03)
- mbrequest.set(slave_inputport.get()+"03"+address_inputport.get()+value_inputport.get())
- if (combo.current()==3): #Read Input Registers (FC=04)
- mbrequest.set(slave_inputport.get()+"04"+address_inputport.get()+value_inputport.get())
- if (combo.current()==4): #Force Single Coil (FC=05) ON
- mbrequest.set(slave_inputport.get()+"05"+address_inputport.get()+"FF00")
- if (combo.current()==5): #Force Single Coil (FC=05) OFF
- mbrequest.set(slave_inputport.get()+"05"+address_inputport.get()+"0000")
- if (combo.current()==6): #Preset Single Register (FC=06)
- mbrequest.set(slave_inputport.get()+"06"+address_inputport.get()+value_inputport.get())
- if (combo.current()==7): #Force Multiple Coils (FC=15)
- mbrequest.set(slave_inputport.get()+"0F"+address_inputport.get()+"001002"+value_inputport.get())
- if (combo.current()==8): #Preset Multiple Registers (FC=16)
- mbrequest.set(slave_inputport.get()+"10"+address_inputport.get()+"000102"+value_inputport.get())
- def pimodbus_rpolling( pm_msg, pm_ipaddress, pm_port ):
- """ poll modbus func """
- # f=crcmod.mkCrcFun( 0x18005, initCrc=-1 )
- rp_msg=codecs.decode( pm_msg, "hex")
- rp_mbap=codecs.decode( "00010000", "hex" )+(len( rp_msg )).to_bytes( 2, byteorder='big')
- binarystr=rp_mbap+rp_msg
- # crcstr=f(binarystr)
- # binarystr=binarystr+crcstr.to_bytes(2, byteorder='little')
- # print( "Master request = "+(binarystr ).hex() )
- with socket.socket( socket.AF_INET, socket.SOCK_STREAM ) as s:
- s.connect((pm_ipaddress, int(pm_port)))
- s.settimeout(3)
- s.sendall( binarystr )
- time.sleep(0.2)
- value=s.recv(1024)
- mbmsgrequest.set((binarystr).hex()[12:])
- response.set((value).hex()[12:])
- # print( "Slave answer = "+value )
- pmwin = tk.Tk()
- pmwin.title( "Modbus TCP Client")
- pmwin.geometry( '240x280')
- pmwin.resizable( False, False )
- frame1=Frame(pmwin)
- frame1.pack()
- #IP
- ip_label = ttk.Label( frame1,text="IP:")
- ip_label.pack( side="left",fill=tk.X, padx=5, pady=5)
- mb_ipaddress = tk.StringVar()
- mb_ipaddress.set( "192.168.6.69")
- input3 = tk.Entry( frame1, textvariable=mb_ipaddress, width=16 )
- input3.pack(side="left")
- #Port modbus
- mb_port = tk.StringVar()
- mb_port.set( "502" )
- mb_inputport = tk.Entry( frame1, textvariable=mb_port, width=8 )
- mb_inputport.pack(side="right")
- frame2=Frame(pmwin);
- frame2.pack()
- #Slave
- slave_label = ttk.Label( frame2,text="slave")
- slave_label.pack( side="left",fill=tk.X, padx=5, pady=5)
- slave=tk.StringVar()
- slave.set("01")
- slave_inputport=tk.Entry( frame2, textvariable=slave, width=3 )
- slave_inputport.pack(side="left")
- slave_inputport.bind ('<Leave>',callback)
- #Address
- address_label = ttk.Label( frame2,text="add")
- address_label.pack( side="left",fill=tk.X, padx=5, pady=5)
- address=tk.StringVar()
- address.set("0000")
- address_inputport=tk.Entry( frame2, textvariable=address, width=5 )
- address_inputport.pack(side="left")
- #address_inputport.bind ('<FocusOut>',callback)
- address_inputport.bind ('<Leave>',callback)
- #Data value
- value_label = ttk.Label( frame2,text="val")
- value_label.pack( side="left",fill=tk.X, padx=5, pady=5)
- value=tk.StringVar()
- value.set("0001")
- value_inputport=tk.Entry( frame2, textvariable=value, width=5 )
- value_inputport.pack(side="left")
- #value_inputport.bind ('<FocusOut>',callback)
- value_inputport.bind ('<Leave>',callback)
- #Combobox selection
- values=["Read Coil Status (FC=01)","Read Input Status (FC=02)",
- "Read Holding Registers (FC=03)","Read Input Registers (FC=04)",
- "Force Single Coil (FC=05) ON","Force Single Coil (FC=05) OFF","Preset Single Register (FC=06)",
- "Force Multiple Coils(2) (FC=15)","Preset Multiple Registers(2) (FC=16)"]
- combo=ttk.Combobox(values=values,state="readonly",width=30)
- combo.current(2)
- combo.pack();
- combo.bind ('<<ComboboxSelected>>', callback) #Bind to callback function
- #imput manual modbus request
- aciihex_label = ttk.Label( text="ASCIIHEX :")
- aciihex_label.pack( fill=tk.X, padx=5, pady=5 )
- mbrequest = tk.StringVar()
- if( len(sys.argv )<2 ):
- mbrequest.set( "010300000001")
- else:
- mbrequest.set( sys.argv[1] )
- input1 = tk.Entry( textvariable=mbrequest, width=30 )
- input1.pack(fill=tk.X, padx=5, pady=5 )
- mbmsgrequest = tk.StringVar()
- mbmsgrequest.set( "No msg")
- ttk.Label(pmwin, text='Sent:').pack( fill=tk.X, padx=5, pady=5 )
- request_input = tk.Entry( state="disable",textvariable=mbmsgrequest,width=30 )
- request_input.config( {"background": "Yellow"})
- request_input.pack()
- response = tk.StringVar()
- response.set( "No msg")
- ttk.Label( text='Received:').pack( fill=tk.X, padx=5, pady=5 )
- response_input = tk.Entry( state="disable", textvariable=response, width=30 )
- response_input.config({"background": "Yellow"})
- response_input.pack()
- #Send Button
- send_button = tk.Button( bg="green",text="Send Request", command=lambda:pimodbus_rpolling( mbrequest.get(), mb_ipaddress.get(), mb_inputport.get() ))
- send_button.pack( fill=tk.X, padx=5, pady=5 )
- pmwin.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement