Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Example: Read txt file and create Modbus configuration

TimvH
2011-05-20
2016-07-13
  • TimvH

    TimvH - 2011-05-20

    For the CUC I created a script which reads a .txt file containing information about the Modbus configuration and creates this in the active project.

    Save the following in a txt file:
    Modbus_COM (COM)
    Master (ModbusMaster)
    ModbusSlave1 (ModbusSlave)
    ModbusSlave2 (ModbusSlave)

    Then create a .py file with the following code.

    print "Current project."
    proj = projects.primary
    \#Search for all top-level devices in project
    for dev in proj.get_children():
       if dev.is_device:
          f = open("C:\_d\Python\ModbusImport\ModbusConfiguratie.txt", "r")
          for line in f:
             tok = line.split(' ')
             print tok[1], tok[0]
             if "(COM)" in tok[1]:
                dev.add(tok[0], DeviceID(92, "0000 0001", "3.4.0.0"))
                subnodes = dev.get_children()
                master = subnodes[len(subnodes) - 1]
             elif "(ModbusMaster)" in tok[1]:
                master.add(tok[0], DeviceID(90, "0000 0002", "3.4.3.0"))
                subnodes = master.get_children()
                coupler = subnodes[len(subnodes) - 1]
             elif "(ModbusSlave)" in tok[1]:
                coupler.add(tok[0], DeviceID(91, "0000 0001", "3.4.0.0"))
    print "script finished."
    

    Open a project, add your device (PLC) and run the script

     

    Related

    Talk.ru: 1

  • AndreyS - 2016-07-13

    Thanks for the example. I have another question about modifying Modbus configuration from Python. Is it possible to fill Modbus Slave configuration. I mean is it possible to add Modbus Slave Channels and fill ModbusGenericSerialSlave I/O Mapping in the Modbus Slave?

     
  • mkeller - 2016-07-13

    Hi AndreyS.

    AndreyS hat geschrieben:
    Thanks for the example. I have another question about modifying Modbus configuration from Python. Is it possible to fill Modbus Slave configuration. I mean is it possible to add Modbus Slave Channels and fill ModbusGenericSerialSlave I/O Mapping in the Modbus Slave?

    Have you checked the device parameters of the ModBus Slave device. You can access them through the property "device_paremeters" of the device object.

    BR
    Martin

     

Log in to post a comment.