Configure Modbus TCP Slave Channels with Python

stgarcia
2022-01-27
2022-02-08
  • stgarcia - 2022-01-27

    Hello,

    I'm trying to add and configure a modbus TCP slave device using a python script, but have not been able to figure out how to add the slave channels.

    I've gotten to the point where I can add a new device and change the existing parameters (IP address and port, for example), but I am not seeing a way to add a channel.

    Can someone point me in the right direction on how to do this through a python script?

    Thanks

     
  • stgarcia - 2022-01-27

    Here's the script I'm using for reference:

    from scriptengine import *
    
    
    def main():
        proj = projects.primary
    
        deviceName = "SLAVE_TO_ADD"
    
        masterdevice = proj.find('Modbus_TCP_Master', recursive = True)[0]  
    
        try:
            #add the slave device if it doesn't already exist
            slaveDevice = masterdevice.add(name=deviceName, type=89, id='0000 0005' , version='3.5.16.0')
            slaveDevice = proj.find(deviceName, recursive = True)[0]
        except:
            #it already exists; we'll just configure it instead
            slaveDevice = proj.find(deviceName, recursive = True)[0]
    
        #print(slaveDevice.connectors)
        for connector in slaveDevice.connectors:
            print(connector.interface_name)
            for MBslaveParam in connector.host_parameters:
                print(MBslaveParam.id, MBslaveParam.name, MBslaveParam.description, MBslaveParam.io_mapping, MBslaveParam.type_string, MBslaveParam.channel_type)
                try:
                    print(MBslaveParam.value)
                except:
                    print('no val')
    
                if MBslaveParam.name == 'IPAddress':
                    MBslaveParam.value = '[123, 45, 67, 89]'
    
    
    if __name__ == "__main__":
        main()
    
     

    Last edit: stgarcia 2022-01-27
  • stgarcia - 2022-02-08

    Update to the void: I still don't think this is possible, after looking closer at the CODESYS documentation, it says:

    property host_parameters
    Get the host parameter set of this connector.
    The returned list is read-only which means you can’t add, insert or remove parameters or clear it. The list of available parameters is defined in the device description.

    which implies to me that channels cannot be added here and must be done in the UI. I'm still confused that you can modify values (as in the IP address script above), but not add or remove.

    Hopefully this helps someone in the future.

     

    Last edit: stgarcia 2022-02-08

Log in to post a comment.