Snap7-Python数据读/写到PLC期间作业挂起异常

时间:2019-09-07 12:43:58

标签: python io plc snap7

在使用Python- Snap7读写数据到Siemens s7 1200 PLC的过程中,出现如下异常:

  • 线程Thread-2中的异常: 追溯(最近一次通话): _bootstrap_inner中的第917行“ C:\ Users \ MDoganli \ AppData \ Local \ Programs \ Python \ Python37-32 \ Lib \ threading.py”文件 self.run() 运行中的文件“ C:\ Users \ MDoganli \ AppData \ Local \ Programs \ Python \ Python37-32 \ Lib \ threading.py”,行865 self._target(* self._args,** self._kwargs) 在read_data中的第59行,文件“ C:\ Companies \ Personal \ deneme \ deneme_iterasyonlar \ plcman.py” 扭矩= plc.read_area(area ['DB'],110,80,24) 文件“ C:\ Users \ MDoganli \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ snap7 \ client.py”,行256,在read_area中 check_error(result,context =“ client”) 文件“ C:\ Users \ MDoganli \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ snap7 \ common.py”,第65行,在check_error中 引发Snap7Exception(错误) snap7.snap7exceptions.Snap7Exception:b'CLI:作业待处理'

我在单通道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()

谢谢。

1 个答案:

答案 0 :(得分: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个实例。在读写过程中将不会收到“待处理的作业”