Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Snap7 Python数据读/写至PLC期间的作业挂起异常_Python_Io_Plc_S7 1200_Snap7 - Fatal编程技术网

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

Snap7 Python数据读/写至PLC期间的作业挂起异常,python,io,plc,s7-1200,snap7,Python,Io,Plc,S7 1200,Snap7,在使用Python-Snap7向西门子s7 1200 PLC读取和写入数据的过程中,我遇到如下异常: 线程2中的异常: 回溯(最近一次呼叫最后一次): 文件“C:\Users\MDoganli\AppData\Local\Programs\Python\Python37-32\Lib\threading.py”,第917行,位于\u bootstrap\u内部 self.run() 文件“C:\Users\MDoganli\AppData\Local\Programs\Python\Python

在使用Python-Snap7向西门子s7 1200 PLC读取和写入数据的过程中,我遇到如下异常:

线程2中的异常: 回溯(最近一次呼叫最后一次): 文件“C:\Users\MDoganli\AppData\Local\Programs\Python\Python37-32\Lib\threading.py”,第917行,位于\u bootstrap\u内部 self.run() 文件“C:\Users\MDoganli\AppData\Local\Programs\Python\Python37-32\Lib\threading.py”,第865行,正在运行 自我目标(*自我参数,**自我参数) 文件“C:\companys\Personal\deneme\deneme\u iterasyonlar\plcman.py”,第59行,读取数据 扭矩=可编程逻辑控制器读取面积(面积['DB'],110,80,24) 文件“C:\Users\MDoganli\AppData\Local\Programs\Python37-32\lib\site packages\snap7\client.py”,第256行,位于读取区域 检查错误(结果,context=“client”) 文件“C:\Users\MDoganli\AppData\Local\Programs\Python37-32\lib\site packages\snap7\common.py”,第65行,检查错误 引发Snap7异常(错误) snap7.snap7异常。snap7异常: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()


提前感谢。

读写应具有不同的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个实例。在读写过程中,将不会收到“作业挂起”