davidLin7692 / pymodbus

Automatically exported from code.google.com/p/pymodbus
0 stars 0 forks source link

A server can't be used to hold mroe than one slave #10

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I've noticed that in this library a server is mapped to a slave (using
Modbus terminology). Can't a server hold more than one slave?

I've seen this in modbutk http://code.google.com/p/modbus-tk/(a simulator)

Am I wrong?

Original issue reported on code.google.com by jjconti@gmail.com on 16 Apr 2010 at 4:24

GoogleCodeExporter commented 9 years ago
No you are correct.  This was a feature I was going to add but I never got 
around to 
it.  It should defiantly be added which I can do when I get some free time 
unless 
someone else wants to take up the torch ;D.

Original comment by Bashw...@gmail.com on 17 Apr 2010 at 8:22

GoogleCodeExporter commented 9 years ago
I solved this with a simple way some days ago.

I'm instantiating the factory with a dict of contexts. Keys are slaves ids:

ModbusServerFactory(stores)

Then, I modified ModbusProtocol.execute like this:

    def execute(self, request):
        '''
        Executes the request and returns the result
        @param request The decoded request message
        '''
        slave = request.unit_id
        try:
            ctx = self.factory.stores[slave]
            response = request.execute(ctx)
        except Exception, ex:
            ...

Do you think it's a good idea? It works for me. If you want I can provide a 
patch.

Original comment by jjconti@gmail.com on 14 May 2010 at 2:27

GoogleCodeExporter commented 9 years ago
Actually yeah that is actually a good solution.  I would like it to be 
formalized a 
bit more though. Maybe the ModbusServerContext could become a 
ModbusClientContext 
and the ModbusServcerContext could be a wrapper around a dict of 
ModbusClientContext.

Regardless, if you have some code I will definately add it.

Original comment by Bashw...@gmail.com on 14 May 2010 at 4:38

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r57.

Original comment by Bashw...@gmail.com on 16 May 2010 at 1:50

GoogleCodeExporter commented 9 years ago
Excelent. I updated my copy of pymodbus, changed ModbusServerContext for
ModbusSlaveContext and all worked just fine.

I may say that ModbusSlaveContext is a much better nama than 
ModbusClientContext. In
modbus ip terminology a client is the one that poll a server. And the server
represents many slaves.

Line 316 of datastore.py has a reminisce of this wrong concept. In
ModbusServerContext.__init__ docstring you should change 'clients' for 'slaves'.

Original comment by jjconti@gmail.com on 19 May 2010 at 4:42