Hello David,
I have to say at the beginning that I'm not a programmer. I found Snap7 in conjunction with Smarthome to read and write data from an S7-300 controller. The program works on the whole, but I constantly get error message.
2018-06-18 22:15:37 ERROR S7 read cycle slow b'CLI : Job pending'
2018-06-18 22:15:37 ERROR S7 read cycle slow Method S7 read cycle slow exception: b'CLI : Job pending'
Traceback (most recent call last):
File "/usr/local/smarthome/lib/scheduler.py", line 449, in _task
obj()
File "/usr/local/smarthome/plugins/s7/__init__.py", line 283, in _read_slow
result = self.client.db_read(int(ret_s7_num[0]), int(ret_s7_num[1]), 4) #Lese DB / Ab / Byte
File "/usr/local/lib/python3.5/dist-packages/snap7/client.py", line 142, in db_read
check_error(result, context="client")
File "/usr/local/lib/python3.5/dist-packages/snap7/common.py", line 65, in check_error
raise Snap7Exception(error)
snap7.snap7exceptions.Snap7Exception: b'CLI : Job pending'
Enclosed is the code from the Smarthome application.
importloggingimportthreadingimportstructimportbinasciiimportreimportsnap7fromsnap7.snap7exceptionsimportSnap7Exceptionfromsnap7.snap7typesimportS7AreaDB,S7WLBit,S7WLByte,S7WLWord,S7WLDWord,S7WLReal,S7DataItemfromsnap7.utilimport*importlib.connectiontcpport=102rack=0slot=2logger=logging.getLogger('')classS7(lib.connection.Client):def__init__(self,smarthome,time_ga=None,date_ga=None,read_cyl_fast=1,read_cyl_slow=10,busmonitor=False,host='127.0.0.1',port=102):self.client=snap7.client.Client()self.client.connect(host,rack,slot,tcpport)lib.connection.Client.__init__(self,host,port,monitor=False)self._sh=smarthomeself.gal={}self.gar={}self._init_ga=[]self._cache_ga=[]self.time_ga=time_gaself.date_ga=date_gaself._lock=threading.Lock()ifread_cyl_fast:self._sh.scheduler.add('S7 read cycle fast',self._read_fast,prio=5,cycle=int(read_cyl_fast))ifread_cyl_slow:self._sh.scheduler.add('S7 read cycle slow',self._read_slow,prio=5,cycle=int(read_cyl_slow))def_send(self,data):iflen(data)<2orlen(data)>0xffff:# 0xffff = 65535logger.debug('S7: Illegal data size: {}'.format(repr(data)))returnFalse# prepend data lengthsend=bytearray(len(data).to_bytes(2,byteorder='big'))send.extend(data)self.send(send)# ------------------------# Daten schreiben# ------------------------defgroupwrite(self,ga,payload,dpt,flag='write'):print("groupwrite")print("ga: "+ga)print("payload: "+str(payload))print("dpt: "+dpt)print("flag: "+flag)ret_s7_num=re.findall(r'\d+',ga)ifdpt=='1':#Schreibe Boolprint("Schreibe Bool")ret_val=self.client.db_read(int(ret_s7_num[0]),int(ret_s7_num[1]),1)#Lese DB / Ab / Bytewritevalue=payloadsnap7.util.set_bool(ret_val,0,int(ret_s7_num[2]),writevalue)#Schreibe Value aus Byte / Adresse self.client.db_write(int(ret_s7_num[0]),int(ret_s7_num[1]),ret_val)#Schreibe DB / Ab / Byteelifdpt=='5':# Schreibe Dezimalzahl#print("Schreibe Dezimalzahl")write_value_5=payloadwrite_bytes_5=bytearray(struct.pack('>h',write_value_5))self.client.db_write(int(ret_s7_num[0]),int(ret_s7_num[1]),write_bytes_5)#Schreibe DB / Ab / Byteelifdpt=='6':# Schreibe Gleitzahlprint("Schreibe Gleitzahl")write_value_6=payloadwrite_bytes_6=bytearray(struct.pack('>f',write_value_6))self.client.db_write(int(ret_s7_num[0]),int(ret_s7_num[1]),write_bytes_6)#Schreibe DB / Ab / Byte# ------------------------# Daten Lesen# ------------------------defgroupread(self,ga,dpt):print("groupread")print("ga: "+ga)print("dpt: "+dpt)ret_s7_num=re.findall(r'\d+',ga)val=0#Item-Valuesrc=ga#Source-Item (Quell-Adresse)dst=ga#Destination-Item (Ziel-Adresse)ifdpt=='1':#Lese Boolprint("Lese Bool")ret_val=self.client.db_read(int(ret_s7_num[0]),int(ret_s7_num[1]),1)#Lese DB / Ab / Byteval=snap7.util.get_bool(ret_val,0,int(ret_s7_num[2]))#Lese Value aus Byte / Adresse elifdpt=='5':# Lese Dezimalzahl#print("Lese Dezimalzahl")result=self.client.db_read(int(ret_s7_num[0]),int(ret_s7_num[1]),2)#Lese DB / Ab / Byte bytes=''.join([chr(x)forxinresult]).encode('utf-8')int_num=struct.unpack('>h',bytes)int_num2=re.findall(r'\d+',str(int_num))val=int_num2[0]elifdpt=='6':# Lese Gleitzahlprint("Lese Gleitzahl")result=self.client.db_read(int(ret_s7_num[0]),int(ret_s7_num[1]),4)#Lese DB / Ab / Byte int_num=struct.unpack('>f',result)[0]val=int_numitem(val,'S7',src,ga)print(str(val)+":"+str(val)+":"+str(src)+":"+str(ga))# --------------------------# Daten lesen Zyklus schnell# --------------------------def_read_fast(self):forgainself._init_ga:print("Daten lesen Zyklus schnell")print("ga: "+ga)val=0#Item-Valuesrc=ga#Source-Item (Quell-Adresse)dst=ga#Destination-Item (Ziel-Adresse)foriteminself.gal[dst]['items']:ret_s7_num=re.findall(r'\d+',dst)var_typ=len(ret_s7_num)dpt=item.conf['s7_dpt']print("dpt: "+dpt)ifdpt=='1':#Initialisiere Bool#print("Initialisiere Bool")ret_val=self.client.db_read(int(ret_s7_num[0]),int(ret_s7_num[1]),1)#Lese DB / Ab / Byteval=snap7.util.get_bool(ret_val,0,int(ret_s7_num[2]))#Lese Value aus Byte / Adresse elifdpt=='5':#Initialisiere Dezimalzahl#print("Initialisiere Dezimalzahl")result=self.client.db_read(int(ret_s7_num[0]),int(ret_s7_num[1]),2)#Lese DB / Ab / Byte bytes=''.join([chr(x)forxinresult]).encode('utf-8')int_num=struct.unpack('>h',bytes)int_num2=re.findall(r'\d+',str(int_num))val=int_num2[0]elifdpt=='6':#Initialisiere Gleitzahl#print("Initialisiere Gleitzahl")result=self.client.db_read(int(ret_s7_num[0]),int(ret_s7_num[1]),4)#Lese DB / Ab / Byte int_num=struct.unpack('>f',result)[0]val=int_numitem(val,'S7',src,ga)print(str(val)+":"+str(val)+":"+str(src)+":"+str(ga))# --------------------------# Daten lesen Zyklus langsam# --------------------------def_read_slow(self):forgainself._init_ga:print("Daten lesen Zyklus langsam")print("ga: "+ga)val=0#Item-Valuesrc=ga#Source-Item (Quell-Adresse)dst=ga#Destination-Item (Ziel-Adresse)foriteminself.gal[dst]['items']:ret_s7_num=re.findall(r'\d+',dst)var_typ=len(ret_s7_num)dpt=item.conf['s7_dpt']print("dpt: "+dpt)ifdpt=='1':#Initialisiere Bool#print("Initialisiere Bool")ret_val=self.client.db_read(int(ret_s7_num[0]),int(ret_s7_num[1]),1)#Lese DB / Ab / Byteval=snap7.util.get_bool(ret_val,0,int(ret_s7_num[2]))#Lese Value aus Byte / Adresse elifdpt=='5':#Initialisiere Dezimalzahl#print("Initialisiere Dezimalzahl")result=self.client.db_read(int(ret_s7_num[0]),int(ret_s7_num[1]),2)#Lese DB / Ab / Byte bytes=''.join([chr(x)forxinresult]).encode('utf-8')int_num=struct.unpack('>h',bytes)int_num2=re.findall(r'\d+',str(int_num))val=int_num2[0]elifdpt=='6':#Initialisiere Gleitzahl#print("Initialisiere Gleitzahl")result=self.client.db_read(int(ret_s7_num[0]),int(ret_s7_num[1]),4)#Lese DB / Ab / Byteint_num=struct.unpack('>f',result)[0]val=int_numitem(val,'S7',src,ga)print(str(val)+":"+str(val)+":"+str(src)+":"+str(ga))defrun(self):self.alive=Truedefstop(self):self.alive=Falseself.handle_close()defparse_item(self,item):if's7_dtp'initem.conf:logger.warning("S7: Ignoring {0}: please change s7_dtp to s7_dpt.".format(item))returnNoneif's7_dpt'initem.conf:dpt=item.conf['s7_dpt']else:returnNoneif's7_read_s'initem.conf:ga=item.conf['s7_read_s']logger.debug("S7: {0} listen on and init with {1}".format(item,ga))ifnotgainself.gal:self.gal[ga]={'dpt':dpt,'items':[item],'logics':[]}else:ifnotiteminself.gal[ga]['items']:self.gal[ga]['items'].append(item)self._init_ga.append(ga)if's7_read_f'initem.conf:ga=item.conf['s7_read_f']logger.debug("S7: {0} listen on and init with {1}".format(item,ga))ifnotgainself.gal:self.gal[ga]={'dpt':dpt,'items':[item],'logics':[]}else:ifnotiteminself.gal[ga]['items']:self.gal[ga]['items'].append(item)self._init_ga.append(ga)if's7_send'initem.conf:ifisinstance(item.conf['s7_send'],str):item.conf['s7_send']=[item.conf['s7_send'],]if's7_send'initem.conf:returnself.update_item_sendreturnNoneif's7_read'initem.conf:returnself.update_item_readreturnNoneif's7_listen'initem.conf:s7_listen=item.conf['s7_listen']ifisinstance(s7_listen,str):s7_listen=[s7_listen,]forgains7_listen:logger.debug("S7: {0} listen on {1}".format(item,ga))ifnotgainself.gal:self.gal[ga]={'dpt':dpt,'items':[item],'logics':[]}else:ifnotiteminself.gal[ga]['items']:self.gal[ga]['items'].append(item)defupdate_item_send(self,item,caller=None,source=None,dest=None):if's7_send'initem.conf:ifcaller!='s7':forgainitem.conf['s7_send']:self.groupwrite(ga,item(),item.conf['s7_dpt'],flag='write')defupdate_item_read(self,item,caller=None,source=None,dest=None):if's7_status'initem.conf:forgainitem.conf['s7_status']:# send status updateifga!=dest:self.groupread(ga,item.conf['s7_dpt'])
Can you give me a clue why this error occurs again and again? What can I do against it?
Thanks in advance
Markus
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
As I said many times, I don't know Python at all.
Anyway CLI : Job pending error is raised when you try to use the same client contemporary from two threads.
I.E. you call a read or write function when the client is already working (Job Pending means Job not complete)
two ways :
Either semaphorize the calls or use two different clients.
regards
Dave
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello David,
I have to say at the beginning that I'm not a programmer. I found Snap7 in conjunction with Smarthome to read and write data from an S7-300 controller. The program works on the whole, but I constantly get error message.
Enclosed is the code from the Smarthome application.
Can you give me a clue why this error occurs again and again? What can I do against it?
Thanks in advance
Markus
Hi,
As I said many times, I don't know Python at all.
Anyway CLI : Job pending error is raised when you try to use the same client contemporary from two threads.
I.E. you call a read or write function when the client is already working (Job Pending means Job not complete)
two ways :
Either semaphorize the calls or use two different clients.
regards
Dave