Advertisement
AntonioVillanueva

Modbus Tester

May 3rd, 2022 (edited)
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.52 KB | None | 0 0
  1. #Modbus TCP Tester by Gilles Pignatta & Antonio Villanueva
  2. import tkinter as tk
  3. from tkinter import ttk, Frame
  4. #import crcmod
  5. import sys
  6. import codecs
  7. import socket
  8. import time
  9.  
  10.  
  11. def callback(eventObject):
  12.     """ callback for combobox function selection """
  13.     if (combo.current()==0): #Read Coil Status (FC=01)
  14.         mbrequest.set(slave_inputport.get()+"01"+address_inputport.get()+value_inputport.get())
  15.     if (combo.current()==1): #Read Input Status (FC=02)
  16.         mbrequest.set(slave_inputport.get()+"02"+address_inputport.get()+value_inputport.get())
  17.     if (combo.current()==2): #Read Holding Registers (FC=03)
  18.         mbrequest.set(slave_inputport.get()+"03"+address_inputport.get()+value_inputport.get())
  19.     if (combo.current()==3): #Read Input Registers (FC=04)
  20.         mbrequest.set(slave_inputport.get()+"04"+address_inputport.get()+value_inputport.get())
  21.     if (combo.current()==4): #Force Single Coil (FC=05) ON
  22.         mbrequest.set(slave_inputport.get()+"05"+address_inputport.get()+"FF00")       
  23.     if (combo.current()==5): #Force Single Coil (FC=05) OFF
  24.         mbrequest.set(slave_inputport.get()+"05"+address_inputport.get()+"0000")               
  25.     if (combo.current()==6): #Preset Single Register (FC=06)
  26.         mbrequest.set(slave_inputport.get()+"06"+address_inputport.get()+value_inputport.get())    
  27.     if (combo.current()==7): #Force Multiple Coils (FC=15)
  28.         mbrequest.set(slave_inputport.get()+"0F"+address_inputport.get()+"001002"+value_inputport.get())
  29.     if (combo.current()==8): #Preset Multiple Registers (FC=16)
  30.         mbrequest.set(slave_inputport.get()+"10"+address_inputport.get()+"000102"+value_inputport.get())   
  31.  
  32. def pimodbus_rpolling( pm_msg, pm_ipaddress, pm_port ):
  33.     """ poll modbus func """
  34.     #    f=crcmod.mkCrcFun( 0x18005, initCrc=-1 )
  35.     rp_msg=codecs.decode( pm_msg, "hex")
  36.     rp_mbap=codecs.decode( "00010000", "hex" )+(len( rp_msg )).to_bytes( 2, byteorder='big')
  37.     binarystr=rp_mbap+rp_msg
  38.     #    crcstr=f(binarystr)
  39.     #    binarystr=binarystr+crcstr.to_bytes(2, byteorder='little')
  40.     #    print( "Master request = "+(binarystr ).hex() )
  41.    
  42.     with socket.socket( socket.AF_INET, socket.SOCK_STREAM ) as s:
  43.         s.connect((pm_ipaddress, int(pm_port)))
  44.         s.settimeout(3)
  45.         s.sendall( binarystr )
  46.         time.sleep(0.2)
  47.         value=s.recv(1024)
  48.    
  49.     mbmsgrequest.set((binarystr).hex()[12:])
  50.     response.set((value).hex()[12:])
  51. #    print( "Slave answer = "+value )
  52.  
  53. pmwin = tk.Tk()
  54.  
  55. pmwin.title( "Modbus TCP Client")
  56. pmwin.geometry( '240x280')
  57. pmwin.resizable( False, False )
  58.  
  59. frame1=Frame(pmwin)
  60. frame1.pack()
  61.  
  62. #IP
  63. ip_label = ttk.Label( frame1,text="IP:")
  64. ip_label.pack( side="left",fill=tk.X, padx=5, pady=5)
  65.  
  66. mb_ipaddress = tk.StringVar()
  67. mb_ipaddress.set( "192.168.6.69")
  68. input3 = tk.Entry( frame1, textvariable=mb_ipaddress, width=16 )
  69. input3.pack(side="left")
  70.  
  71. #Port modbus
  72. mb_port = tk.StringVar()
  73. mb_port.set( "502" )
  74. mb_inputport = tk.Entry( frame1, textvariable=mb_port, width=8 )
  75. mb_inputport.pack(side="right")
  76.  
  77. frame2=Frame(pmwin);
  78. frame2.pack()
  79.  
  80. #Slave
  81. slave_label = ttk.Label( frame2,text="slave")
  82. slave_label.pack( side="left",fill=tk.X, padx=5, pady=5)
  83.  
  84. slave=tk.StringVar()
  85. slave.set("01")
  86. slave_inputport=tk.Entry( frame2, textvariable=slave, width=3 )
  87. slave_inputport.pack(side="left")
  88. slave_inputport.bind ('<Leave>',callback)
  89.  
  90. #Address
  91. address_label = ttk.Label( frame2,text="add")
  92. address_label.pack( side="left",fill=tk.X, padx=5, pady=5)
  93.  
  94. address=tk.StringVar()
  95. address.set("0000")
  96. address_inputport=tk.Entry( frame2, textvariable=address, width=5 )
  97. address_inputport.pack(side="left")
  98. #address_inputport.bind ('<FocusOut>',callback)
  99. address_inputport.bind ('<Leave>',callback)
  100.  
  101. #Data value
  102. value_label = ttk.Label( frame2,text="val")
  103. value_label.pack( side="left",fill=tk.X, padx=5, pady=5)
  104.  
  105. value=tk.StringVar()
  106. value.set("0001")
  107. value_inputport=tk.Entry( frame2, textvariable=value, width=5 )
  108. value_inputport.pack(side="left")
  109. #value_inputport.bind ('<FocusOut>',callback)
  110. value_inputport.bind ('<Leave>',callback)
  111.  
  112. #Combobox selection
  113. values=["Read Coil Status (FC=01)","Read Input Status (FC=02)",
  114. "Read Holding Registers (FC=03)","Read Input Registers (FC=04)",
  115. "Force Single Coil (FC=05) ON","Force Single Coil (FC=05) OFF","Preset Single Register (FC=06)",
  116. "Force Multiple Coils(2) (FC=15)","Preset Multiple Registers(2) (FC=16)"]
  117. combo=ttk.Combobox(values=values,state="readonly",width=30)
  118. combo.current(2)
  119. combo.pack();
  120.  
  121. combo.bind ('<<ComboboxSelected>>', callback) #Bind to callback function
  122.  
  123. #imput manual modbus request
  124.  
  125. aciihex_label = ttk.Label( text="ASCIIHEX :")
  126. aciihex_label.pack( fill=tk.X, padx=5, pady=5 )
  127.  
  128. mbrequest = tk.StringVar()
  129.  
  130. if( len(sys.argv )<2 ):
  131.     mbrequest.set( "010300000001")
  132.  
  133. else:
  134.     mbrequest.set( sys.argv[1] )
  135.  
  136. input1 = tk.Entry( textvariable=mbrequest, width=30 )
  137. input1.pack(fill=tk.X, padx=5, pady=5 )
  138.  
  139. mbmsgrequest = tk.StringVar()
  140. mbmsgrequest.set( "No msg")
  141.  
  142. ttk.Label(pmwin, text='Sent:').pack( fill=tk.X, padx=5, pady=5 )
  143. request_input = tk.Entry( state="disable",textvariable=mbmsgrequest,width=30 )
  144. request_input.config( {"background": "Yellow"})
  145. request_input.pack()
  146.  
  147. response = tk.StringVar()
  148. response.set( "No msg")
  149. ttk.Label( text='Received:').pack( fill=tk.X, padx=5, pady=5 )
  150. response_input = tk.Entry( state="disable", textvariable=response, width=30 )
  151. response_input.config({"background": "Yellow"})
  152. response_input.pack()
  153.  
  154. #Send Button
  155. send_button = tk.Button(  bg="green",text="Send Request", command=lambda:pimodbus_rpolling( mbrequest.get(), mb_ipaddress.get(), mb_inputport.get() ))
  156. send_button.pack( fill=tk.X, padx=5, pady=5 )
  157.  
  158. pmwin.mainloop()
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement