#!/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()
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 ??
--
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.
I mean read / write one or more registers (from addr. 40001 that should be %MW0)
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.