【问题标题】:Keep automatically searching for connection snap7继续自动搜索连接 snap7
【发布时间】:2017-07-27 08:50:42
【问题描述】:

我们想通过 wifi 与 PLC 自动连接。当覆盆子启动并自动运行他的程序时。它应该是一个独立的树莓派,所以我们没有键盘或任何东西。我们通过 snap7 发送数据。 这可行,但如果 wifi 断开连接,我会收到此错误:“ISO:recv TCP 期间发生错误:连接超时” 有时在程序开始时我们会收到这个错误:“Snap7Exception: TCP : Unreachable peer”

我的程序停止了,但我们应该有一些东西,所以我们的 wifi 会在不停止程序的情况下再次重新连接。我想我们需要一些东西来捕捉我们的错误并在程序中使用它来尝试或其他东西。

我此时的程序:

import snap7
import struct
import time
from snap7.snap7exceptions import Snap7Exception
import re
from ctypes import c_int, c_char_p, byref, sizeof, c_uint16, c_int32, c_byte
from ctypes import c_void_p

client = snap7.client.Client()

db_number = 109

print('Press Ctrl-C to quit.')

while True:

    if client.get_connected() == False: 
        client.connect('192.168.5.2', 0, 1) #('IP-address', rack, slot)
        print('not connected')
        time.sleep(0.2)
    else:
        print('connected')

【问题讨论】:

    标签: python tcp raspberry-pi


    【解决方案1】:

    在 python 中,您可以使用 try-except 语句捕获错误。

    您可以尝试以下方法:

    while True:
    
        if client.get_connected() == False:
            try:
                client.connect('192.168.5.2', 0, 1) #('IP-address', rack, slot)
                print('not connected')
                time.sleep(0.2)
            except Snap7Exception as e:
                continue
        else:
            print('connected')
    

    【讨论】:

      【解决方案2】:

      您可以使用try-exception来连接和读取PLC,如下代码:

      from time import sleep
      from snap7 import client as s7
      
      
      def plc_connect(ip, rack, slot, stop_tries, freq):
          plc = s7.Client()
          tries = 1
          while tries < stop_tries and not plc.get_connected():
              try:
                  print('trying for connecting to PLC ...')
                  sleep(freq)
                  plc.connect(ip, rack, slot)
                  return True
      
              except Exception as e:
                  logger.error("warning in PLC connection >>{}".format(e))
                  sleep(freq)
      
                  if tries == (stop_tries - 1):
                      print('error in plc connection')
                      return False
      
              tries += 1
          return False
      
      
      def data_reader():
          plc = s7.Client()
          try:
              result, data_items = plc.read_multi_vars(data_items)
              return result, data_items
          except Exception as e:
              print('warning:>>{}'.format(e))
      

      【讨论】:

        【解决方案3】:
        import time
        
        import snap7
        from snap7.exceptions import Snap7Exception
        
        # client = snap7.client.Client()
        connect = False
        
        
        def dataPLC(client_):
            print(client_.db_read(100, 10, 4))
            time.sleep(2)
        
        
        while True:
        
            if not connect:
                try:
                    client = snap7.client.Client()
                    client.connect('172.20.255.200', 0, 2)
                    time.sleep(1)
                    connect = client.get_connected()
                    print('plc connect ', connect)
                except Snap7Exception as e:
                    connect = False
                    continue
            else:
                try:
                    if connect:
                        print('connection')
                        dataPLC(client)
        
                except Snap7Exception as e:
                    client.destroy()
                    print('error ', e)
                    connect = False
                    continue
        

        【讨论】:

        • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
        猜你喜欢
        • 2022-11-17
        • 1970-01-01
        • 1970-01-01
        • 2012-11-18
        • 1970-01-01
        • 2013-01-20
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多