0

在使用 Python-Snap7 向 Siemens s7 1200 PLC 读取和写入数据期间,我收到如下异常:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Users\MDoganli\AppData\Local\Programs\Python\Python37-32\Lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\MDoganli\AppData\Local\Programs\Python\Python37-32\Lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Companies\Personal\deneme\deneme_iterasyonlar\plcman.py", line 59, in read_data
    torque=plc.read_area(areas['DB'],110,80,24)
  File "C:\Users\MDoganli\AppData\Local\Programs\Python\Python37-32\lib\site-packages\snap7\client.py", line 256, in read_area
    check_error(result, context="client")
  File "C:\Users\MDoganli\AppData\Local\Programs\Python\Python37-32\lib\site-packages\snap7\common.py", line 65, in check_error
    raise Snap7Exception(error)
snap7.snap7exceptions.Snap7Exception: b'CLI : Job pending'

我在单通道 db_read/db_write 期间没有遇到此问题,但在其他读取或写入通道处于活动状态时会发生此问题。

我尝试了 area_read & area_write 和 db_read 和 db_write 选项,但收到了类似的错误。

主要代码:

   plc=plcman.PLC_Controller('192.168.30.100',0,1)
   plc.connect()
   time.sleep(1)
   plc.start_thread2()
   time.sleep(1)
   plc.start_thread()

PLC数据-读写代码

class PLC_Controller:

    plc=c.Client()
    def __init__(self, address, rack, slot):

        self.address = address
        self.rack = rack
        self.slot = slot


    def connect(self):

        count = 0

        if  plc.get_connected() == False:
            print("Try " + str(count) + " - Connecting to PLC: " +
                    self.address + ", Rack: " + str(self.rack) + ", Slot: " + str(self.slot))
            try:
                plc.connect(self.address, self.rack, self.slot) #('IP-address', rack, slot)
            except Exception as e:
                print(e)

        if  plc.get_connected() == True:
            return plc.get_connected() == True    

    def get_word(self,_bytearray, byte_index):
        data = _bytearray[byte_index:byte_index + 2]
        data=data[::-1]
        dword = struct.unpack('H', struct.pack('2B', *data))[0]
        return dword


    def read_data(self):

            torque=plc.read_area(areas['DB'],110,80,24)
            data1=self.get_word(torque,0)

            time.sleep(0.8)
            self.read_data()

    def start_thread(self):
        thread = threading.Thread(target=self.read_data, args=())
        thread.daemon = True
        thread.start()


    def set_word(self,_bytearray, byte_index, word):
        word=int(word)

        _bytes =  struct.pack('H', word)
        _bytes=_bytes[::-1]

        for i, b in enumerate(_bytes):
            time.sleep(1)

            _bytearray[byte_index + i] = b

         res=plc.write_area(areas['DB'],110,24,_bytearray)


    def start_thread2(self):

        thread = threading.Thread(target=self.stoprun, args=())
        thread.daemon = True
        thread.start()

    def stoprun(self):

        Lamp=4
        torque=plc.read_area(areas['DB'],110,80,24)
        val1=self.set_word(torque, 0, 8)
        self.stoprun()

提前致谢。

4

1 回答 1

0

读写应该有不同的PLC连接实例。修改后的连接将是:

 plc=plcman.PLC_Controller('192.168.30.100',0,1)   # for reading use plc.read_area()
 plc.connect()  
 plc2=plcman.PLC_Controller('192.168.30.100',0,1)
 plc2.connect()  #for writing use plc2.write_area() 

最多允许 3 个实例。在读写期间不会收到“作业挂起”

于 2020-04-30T12:24:32.030 回答