Modbus-tk with Schneider TM221CE40R / Relay OUT

756 views
Skip to first unread message

Martin Chicoine

unread,
Jun 22, 2015, 10:49:45 AM6/22/15
to modb...@googlegroups.com



Hi, i am new with Modbus and PLC in general, but i know python a lot and i am working with raspberry PI for many years now, i am trying right now to built a web UI to monitor and control this PLC.

I am connected to it with USB to RS485 cable.

Here is my test code:








#!/usr/bin/env python


# -*- coding: utf_8 -*-


"""


 Modbus TestKit: Implementation of Modbus protocol in python



 (C)2009 - Apidev - http://www.apidev.fr


 This is distributed under GNU LGPL license, see license.txt


"""




import serial


import time


import modbus_tk


import modbus_tk.defines as cst


from modbus_tk import modbus_rtu






def main():


    """main"""


    logger = modbus_tk.utils.create_logger("console")




    try:


        #Connect to the slave


        master = modbus_rtu.RtuMaster(


            serial.Serial(port='/dev/ttyUSB0', baudrate=19200, bytesize=8, parity='E', stopbits=1, xonxoff=0)


        )


        master.set_timeout(0.5)


        #master.set_verbose(True)


        logger.info("connected")




        #logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 100, 3))




        #send some queries


        #print 'Reading coils:'


        #logger.info(master.execute(1, cst.READ_COILS, 0, 15))


        print 'Reading Discrete Inputs:'


        logger.info(master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 23))


#        logger.info(master.execute(1, cst.READ_INPUT_REGISTERS, 0, 23))


#        logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 15, 2))


#       for x in range(0,15):


#               print 'Enabling coil: {0}'.format(x)


#               logger.info(master.execute(1, cst.WRITE_SINGLE_COIL, 7, output_value=1))


#        logger.info(master.execute(1, cst.WRITE_SINGLE_REGISTER, 100, output_value=54))


#        logger.info(master.execute(1, cst.WRITE_MULTIPLE_COILS, 0, output_value=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))


        logger.info(master.execute(1, cst.WRITE_MULTIPLE_COILS, 0, output_value=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))


#        logger.info(master.execute(1, cst.WRITE_MULTIPLE_REGISTERS, 100, output_value=xrange(12)))




    except modbus_tk.modbus.ModbusError as exc:


        logger.error("%s- Code=%d", exc, exc.get_exception_code())




if __name__ == "__main__":


    main()



And here is the answer i got:

2015-06-22 14:22:30,754 INFO modbus_rtu.__init__ MainThread RtuMaster /dev/ttyUSB0 is opened

 

2015-06-22 14:22:30,755 INFO modbus-tk.main MainThread connected

Reading Discrete Inputs:

2015-06-22 14:22:31,258 INFO modbus-tk.main MainThread (0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1)

2015-06-22 14:22:31,761 INFO modbus-tk.main MainThread (0, 15)



Maybe there is something i don't understand, but since on the picture input 15 and 16 are ON, i was thiking that i should see it on on the read discret input.


And when i write multiple coils, right now i am trying to write them all to ON, 1 or 2 are lighting for a fraction of a second and shutdown after that!


Am i completely lost with that ??

alex chiosso

unread,
Jun 22, 2015, 11:12:31 AM6/22/15
to modb...@googlegroups.com
Hi Martin.

About the coils are you sure that the plc outputs are non resetted from the plc itself (user program or output settings) ?
Can you try with the register first ?
I mean read / write one or more registers (from addr. 40001 that should be %MW0) .
If the Modbus communication will work that means the problem is somewhere else . ;-)
I usually prefer to test the communication with a Modbus simulator.
So fist of all I check the Master Modbus-tk vs the Modbus simulator in slave mode and then the Modbus simulator in Master mode
vs the Slave I want to communicate with.
With this procedure I can identify where the problem is or is not.

Alex


--
You received this message because you are subscribed to the Google Groups "modbus-tk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modbus-tk+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Martin Chicoine

unread,
Jun 24, 2015, 1:13:49 PM6/24/15
to modb...@googlegroups.com
To be honest i am very new to modbus, i am a microcontroler guy so to be very honest i don't understand how this protocol is working, right now to my eyes, this is way to complicated for what it does, but it seems to be the way it is built in any PLC, very complicated for very limited feature. But i need to learn it to be able to complete this project.Is someone know if the Schneider M211 series of plc use a modified version of the modbus standard ? Because right now with the exact same USB to RS485 cable i have i can use SoMachine Basic and read the program on the PLC so i think my connection is good. I have tried this:
logger.info(master.execute(1, cst.READ_INPUT_REGISTERS, 40001, 23))
but it gave me:
2015-06-24 17:07:13,596 ERROR   modbus-tk.main  MainThread      Modbus Error: Exception code = 2- Code=2

Maybe we can communicate toghter via email or Hangout and i can give you remote access to the Raspberry PI that i use to communicate witht he PLC so you can take a look, you seems way more advanced with modbus that i will never be!!!

Thanx a lot!

Martin Chicoine

unread,
Jun 24, 2015, 11:04:03 PM6/24/15
to modb...@googlegroups.com
Just a little update, i have setup the Network port and tried the same command with modbus TCP instead of RTU and i got exact same response so my problem is not in my cable.

alex chiosso

unread,
Jun 25, 2015, 3:41:58 AM6/25/15
to modb...@googlegroups.com
Hi Martin.
Modbus Error Exception Code 2 ===> Illegal address
Take a look at this (Modbus RTU) and this (Modbus TCP/IP) .

When I wrote :

I mean read / write one or more registers (from addr. 40001 that should be %MW0)
I was talking about HOLDING REGISTERS and the starting address is 1 because HOLDING REGISTER Data Area is 4 and register number is 0001.
So I suggest to you to perform a WRITE_SINGLE_REGISTER function with the starting address 1 and a READ_HOLDING_REGISTER function with the starting address 1 and elements 10 .
If the connection is working you should be able to read back the value you're writing into.

I encourage you to learn more about Modbus communication protocol before.
On the internet you can find tons of ducumentation (also in French language of course) . ;-)

Alex

Jake

unread,
Jun 25, 2015, 9:27:00 AM6/25/15
to modb...@googlegroups.com

Im not at my computer right now, so i cant give you a source snippet.  But have you tried turning on the debug logging level.  Depending on which version of the code your using and how you have structured your program tree the imports are a little different. But based on the the newest github commits its something like this.
    from modbus_tk import LOGGER
    import logging
    logger = LOGGER(level=logging.DEBUG)
And then after you create a modbus connection object.
     connection_object.verbose = true.
That should allow you to see for sure if your communicating with the plc successfully. As it will output the data sent and recieved over  modbus.

Again the code snippets here are probably wrong, ill update once i get back to my desk. But between that and a full traceback/ console output,  plus your source code, and we may be able to help a little more.

Reply all
Reply to author
Forward
0 new messages