2

我之前尝试过相同的代码,运行完美。后来我使用“LOGO!Soft Comfort 8.2”对我的“Siemens LOGO 12/24 rce”进行了编程,即便如此,它也能完美运行。

但是当我使用 Python 重新编程时,它显示: 发生异常:ConnectionResetError [WinError 10054] 现有连接被远程主机强行关闭

代码:

from __future__ import print_function

import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_tcp, hooks
import logging
import time

def main():
    """main"""
    logger = modbus_tk.utils.create_logger("console", level=logging.DEBUG)
try:

        def on_before_connect(args):
            master = args[0]
            logger.debug("on_before_connect {0} {1}".format(master._host, master._port))

        hooks.install_hook("modbus_tcp.TcpMaster.before_connect", on_before_connect)

        def on_after_recv(args):
            response = args[1]
            logger.debug("on_after_recv {0} bytes received".format(len(response)))

            print(type(master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 64)))
        hooks.install_hook("modbus_tcp.TcpMaster.after_recv", on_after_recv)

        # Connect to the slave
        master = modbus_tcp.TcpMaster()
        master._host = "192.168.0.150"
        master._port = 502
        master.set_timeout(5.0)
        logger.info("connected")
        while(True):

            logger.info(master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 64))
            time.sleep(0.2)


    except modbus_tk.modbus.ModbusError as exc:
        logger.error("%s- Code=%d", exc, exc.get_exception_code())

if __name__ == "__main__":
    main()

错误:

'c:\Users\Admin\Documents\tcpmaster_PLC.py'
2019-01-25 10:15:16,072 INFO    tcpmaster_PLC.main      MainThread      connected
    2019-01-25 10:15:16,074 DEBUG   tcpmaster_PLC.on_before_connect MainThread      on_before_connect 192.168.0.150 502
Traceback (most recent call last):
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\ptvsd_launcher.py", line 45, in <module>
    main(ptvsdArgs)
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 265, in main
    wait=args.wait)
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 258, in handle_args
    debug_main(addr, name, kind, *extra, **kwargs)
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 45, in debug_main
    run_file(address, name, *extra, **kwargs)
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 79, in run_file
    run(argv, addr, **kwargs)
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 140, in _run
    _pydevd.main()
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1925, in main
    debugger.connect(host, port)
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "c:\Users\Admin\Documents\tcpmaster_PLC.py", line 78, in <module>
    main()
  File "c:\Users\Admin\Documents\tcpmaster_PLC.py", line 63, in main
    logger.info(master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 64)) #type ----> Tuple
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\modbus_tk\utils.py", line 39, in new
    raise excpt
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\modbus_tk\utils.py", line 37, in new
    ret = fcn(*args, **kwargs)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\modbus_tk\modbus.py", line 298, in execute
    response = self._recv(expected_length)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\modbus_tk\modbus_tcp.py", line 216, in _recv
    rcv_byte = self._sock.recv(1)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
Exception ignored in: <bound method Master.__del__ of <modbus_tk.modbus_tcp.TcpMaster object at 0x00000253FD7043C8>>
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\modbus_tk\modbus.py", line 90, in __del__
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\modbus_tk\modbus.py", line 105, in close
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\modbus_tk\modbus_tcp.py", line 179, in _do_close
TypeError: 'NoneType' object is not callable

请帮助解决此问题....在此先感谢

4

1 回答 1

0

首先,我建议您检查代码中的缩进级别,因为它现在看起来有点乱。

然后,请使用以下方式连接到您的设备:

master = modbus_tcp.TcpMaster("192.168.0.150")

我从来没有像你这样用过它。是不是_host_port某种私有变量,所以你没有正确连接到设备?

于 2020-04-26T17:10:42.210 回答