首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Snap7期间作业挂起异常-Python数据读/写到PLC

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

Stack Overflow用户
提问于 2019-09-07 20:43:58
回答 1查看 932关注 1票数 0

在使用Python- Snap7向西门子s7 1200PLC读取和写入数据时,我得到一个异常,如下所示:

代码语言:javascript
复制
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选项,但收到了类似的错误。

主代码:

代码语言:javascript
复制
   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数据读写代码

代码语言:javascript
复制
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()

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-04-30 20:24:32

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

代码语言:javascript
复制
 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个实例。在读写过程中,将不会收到"job pending“

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57833811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档