在使用Python- Snap7向西门子s7 1200PLC读取和写入数据时,我得到一个异常,如下所示:
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()
提前谢谢。
发布于 2020-04-30 20:24:32
读和写应该有不同的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个实例。在读写过程中,将不会收到"job pending“
https://stackoverflow.com/questions/57833811
复制相似问题