在使用Python-Snap7向西门子s7 1200 PLC读取和写入数据期间,我得到一个异常,如下所示:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:UsersMDoganliAppDataLocalProgramsPythonPython37-32Libthreading.py", line 917, in _bootstrap_inner
self.run()
File "C:UsersMDoganliAppDataLocalProgramsPythonPython37-32Libthreading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:CompaniesPersonaldenemedeneme_iterasyonlarplcman.py", line 59, in read_data
torque=plc.read_area(areas['DB'],110,80,24)
File "C:UsersMDoganliAppDataLocalProgramsPythonPython37-32libsite-packagessnap7client.py", line 256, in read_area
check_error(result, context="client")
File "C:UsersMDoganliAppDataLocalProgramsPythonPython37-32libsite-packagessnap7common.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()
提前谢谢。
读写应该有不同的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 个实例。在读写期间,不会收到"作业挂起">