def __init__(self, host, sim=True, rate=50): """ :param host: Contains the IP adress of the modbus server :type host: string :param rate: How often the registers on the modbusserver should be read per second :type rate: float :param reset_registers: Defines if the holding registers should be reset to 0 after they have been read. Only possible if they are writeable :type reset_registers: bool """ print( "Use the appropriate Cognex Insight Project to enable the Modbus Server" ) self.sim = sim ModbusWrapperClient.__init__(self, host, 502, rate, reset_registers=False) # All registers self.write_start = 50000 self.setReadingRegisters(30010, 30127 - 30010) # Use only a few registers for writing self.setWritingRegisters(self.write_start, 2) # Note: Listener not started as reading is requested manually, but a heartbeat is required to keep the modbus server alive self.startHeartbeat()
def __init__(self, host, port=502, rate=50, reset_registers=True): """ :param host: Contains the IP adress of the modbus server :type host: string :param rate: How often the registers on the modbusserver should be read per second :type rate: float :param reset_registers: Defines if the holding registers should be reset to 0 after they have been read. Only possible if they are writeable :type reset_registers: bool """ # print("Use the appropriate Step7 Project to enable the Modbus Server on your Siemens S1200 PLC") ModbusWrapperClient.__init__(self, host, port, rate, reset_registers) self.setReadingRegisters(0, 8) self.setWritingRegisters(8, 9)
def __init__(self,host,port=502,rate=50,reset_registers=True): """ :param host: Contains the IP adress of the modbus server :type host: string :param rate: How often the registers on the modbusserver should be read per second :type rate: float :param reset_registers: Defines if the holding registers should be reset to 0 after they have been read. Only possible if they are writeable :type reset_registers: bool """ print("Use the appropriate Step7 Project to enable the Modbus Server on your Siemens S1200 PLC") ModbusWrapperClient.__init__(self,host,port,rate,reset_registers) self.setReadingRegisters(0,8) self.setWritingRegisters(8,6) self.startListening()
def __init__(self,host,sim=True,rate=50): """ :param host: Contains the IP adress of the modbus server :type host: string :param rate: How often the registers on the modbusserver should be read per second :type rate: float :param reset_registers: Defines if the holding registers should be reset to 0 after they have been read. Only possible if they are writeable :type reset_registers: bool """ print("Use the appropriate Cognex Insight Project to enable the Modbus Server") self.sim = sim ModbusWrapperClient.__init__(self,host,502,rate,reset_registers=False) # All registers self.write_start = 50000 self.setReadingRegisters(30010,30127-30010) # Use only a few registers for writing self.setWritingRegisters(self.write_start,2) # Note: Listener not started as reading is requested manually, but a heartbeat is required to keep the modbus server alive self.startHeartbeat()
def __init__(self, host, port=502, rate=50, reset_registers=True, disable_topics=False): """ :param host: Contains the IP adress of the modbus server :type host: string :param rate: How often the registers on the modbusserver should be read per second :type rate: float :param reset_registers: Defines if the holding registers should be reset to 0 after they have been read. Only possible if they are writeable :type reset_registers: bool :param disable_topics: Disables ROS subscriber/publisher if True, disabling automatic reading and writing. Voids startListening and stopListening, will raise exception if called. :type disable_topics: bool """ # print("Use the appropriate Step7 Project to enable the Modbus Server on your Siemens S1200 PLC") ModbusWrapperClient.__init__(self, host, port, rate, reset_registers, disable_topics) self.setReadingRegisters(0, 8) self.setWritingRegisters(8, 9)
if rospy.has_param("~ip"): host = rospy.get_param("~ip") else: rospy.loginfo( "For not using the default IP %s, add an arg e.g.: '_ip:=\"192.168.0.199\"'", host) if rospy.has_param("~port"): port = rospy.get_param("~port") else: rospy.loginfo( "For not using the default port %d, add an arg e.g.: '_port:=1234'", port) # setup modbus client modclient = ModbusWrapperClient(host, port=port, rate=50, reset_registers=False, sub_topic="modbus_wrapper/output", pub_topic="modbus_wrapper/input") modclient.setReadingRegisters(ADDRESS_READ_START, NUM_REGISTERS) modclient.setWritingRegisters(ADDRESS_WRITE_START, NUM_REGISTERS) rospy.loginfo("Setup complete") # start listening to modbus and publish changes to the rostopic modclient.startListening() rospy.loginfo("Listener started") ################# # Example 1 # Sets an individual register using the python interface, which can automatically be reset, if a timeout is given. register = 40020 value = 1
sub_topic = rospy.get_param('~sub_topic', 'output') pub_topic = rospy.get_param('~pub_topic', 'input') address_read_start = int(rospy.get_param('~address_read_start', 0)) address_write_start = int(rospy.get_param('~address_write_start', 0)) num_registers = int(rospy.get_param('~num_registers', 20)) rospy.loginfo('Modbus server: {}:{}'.format(host, port)) rospy.loginfo('Modbus id: {}'.format(str(unit))) rospy.loginfo('address_read_start: {}'.format(address_read_start)) rospy.loginfo('address_write_start: {}'.format(address_write_start)) rospy.loginfo('num_registers: {}'.format(num_registers)) nh = ModbusWrapperClient( host, port=port, unit=unit, reset_registers=False, sub_topic=sub_topic, pub_topic=pub_topic ) nh.setReadingRegisters(address_read_start, num_registers) nh.setWritingRegisters(address_write_start, num_registers) rospy.loginfo('Setup complete') # start listening to modbus and publish changes to the rostopic nh.startListening() rospy.loginfo('Listener started') rospy.spin() # modclient.stopListening()
To see the read registers of the modbus server use: rostopic echo /modbus_wrapper/input To see sent something to the modbus use a publisher on the topic /modbus_wrapper/output This file contains a sample publisher. """) host = "192.168.0.123" port = 502 if rospy.has_param("~ip"): host = rospy.get_param("~ip") else: rospy.loginfo("For not using the default IP %s, add an arg e.g.: '_ip:=\"192.168.0.199\"'",host) if rospy.has_param("~port"): port = rospy.get_param("~port") else: rospy.loginfo("For not using the default port %d, add an arg e.g.: '_port:=1234'",port) # setup modbus client modclient = ModbusWrapperClient(host,port=port,rate=50,reset_registers=False,sub_topic="modbus_wrapper/output",pub_topic="modbus_wrapper/input") modclient.setReadingRegisters(ADDRESS_READ_START,NUM_REGISTERS) modclient.setWritingRegisters(ADDRESS_WRITE_START,NUM_REGISTERS) rospy.loginfo("Setup complete") # start listening to modbus and publish changes to the rostopic modclient.startListening() rospy.loginfo("Listener started") ################# # Example 1 # Sets an individual register using the python interface, which can automatically be reset, if a timeout is given. register = 40020 value = 1 timeout = 0.5 modclient.setOutput(register,value,timeout)
rospy.init_node("modbus_client") host = "172.16.7.4" port = 502 if rospy.has_param("~ip"): host = rospy.get_param("~ip") else: rospy.loginfo("For not using the default IP %s, add an arg e.g.: '_ip:=\"192.168.0.199\"'",host) if rospy.has_param("~port"): port = rospy.get_param("~port") else: rospy.loginfo("For not using the default port %d, add an arg e.g.: '_port:=1234'",port) # setup modbus client modclient = ModbusWrapperClient(host, port=port, rate=1, reset_registers=False, sub_topic="modbus_wrapper/output", pub_topic="modbus_wrapper/input") modclient.setReadingRegisters(ADDRESS_READ_START, NUM_REGISTERS) modclient.setWritingRegisters(ADDRESS_WRITE_START, NUM_REGISTERS) rospy.loginfo("Setup complete") # start listening to modbus and publish changes to the rostopic modclient.startListening() rospy.loginfo("Listener started") # Create a listener that show us a message if anything on the readable modbus registers change rospy.loginfo("All done. Listening to inputs... Terminate by Ctrl+c") sub_fanuc = rospy.Subscriber("modbus_wrapper/input", HoldingRegister, showUpdatedRegisters, queue_size=500) sub_robotiq = rospy.Subscriber("SModelRobotInput", inputMsg.SModel_robot_input, statusInterpreter, queue_size=500) # Spins ROS