b' ISO : An error occurred during recv TCP : Connection timed out' #388
Replies: 6 comments 1 reply
-
To much code - won't read everything. For the start, I wouldn't use asynchroun functions/methods, since this would be the next step (especially if you want to profit from it on Python side...) about handling async PLC-requests with asynchrounus Python. So stay there on synchrounus side first. We got some examples mostly below every function, like: Line 366 in 38d6576 Are you excepting such problems? Have you traced the package flow (e.g. with Wireshark)? |
Beta Was this translation helpful? Give feedback.
-
Thank you for your reply. ‘The error is happening in different occasions : Again much appreciated. |
Beta Was this translation helpful? Give feedback.
-
Hi Momben, Well to debug your code I would cut the problem in small pieces. Try to reproduce errors in small pieces of your code.
|
Beta Was this translation helpful? Give feedback.
-
Hi All, THank you again |
Beta Was this translation helpful? Give feedback.
-
Use "try" and "except" to avoid your code stops when an error occur |
Beta Was this translation helpful? Give feedback.
-
I have the same problem with the error: ISO : An error occurred during recv TCP : Connection timed out -----------------------------------------------import snap7 plc = snap7.logo.Logo() ------------------------------------------------The host is a Raspberry uname: Linux PI-4 6.1.57-v7+ #1688 SMP Thu Oct 12 15:09:45 BST 2023 armv7l GNU/Linux The connection is possible with the S7 module in "fhem" which is said to be based on settimino (http://settimino.sourceforge.net). The serial Communication is based on a libnodave portation. Does anybody has an idea whats going wrong? |
Beta Was this translation helpful? Give feedback.
-
HI ALL,
Im using a Pi4 with snap7 python to read data from plc cyclicly.
I encountered this issue while i'm reading constantly values of the PLC in every 0.65 sec then doing some calculations.
Im a begginner on python and i would like to have some support understanding the error or guide me with documentations, examples. When the error occur i lose communication with PLC and i have to run py again.
im attaching the code of the app.
"""
"""
import snap7
from snap7 import types
from snap7.types import WordLen, S7Object, param_types
from snap7.common import ipv4, check_error, load_library
from snap7.util import*
import tkinter.filedialog
from time import sleep
import tkinter
import time
from datetime import datetime
from pywhatkit import send_mail
from credential import google_p
from snap7.exceptions import Snap7Exception
import ctypes
import gc
import logging
import struct
import time
import unittest
import platform
from datetime import datetime, timedelta, date
from multiprocessing import Process
from unittest import mock
import snap7
from snap7 import util
from snap7.common import check_error
from snap7.server import mainloop
from snap7.types import S7AreaDB, S7DataItem, S7SZL, S7SZLList, buffer_type, buffer_size, S7Object, Areas, WordLen
#Snap 7 functions parameters
db_number = 127
start_offset = 4004
bit_offset = 1
value = 1 # 1 = true | 0 = false
start_address = 13024 # starting address
length = 4 # double word
#variable init
sum = 0.0
test_completed = False
count = 0
hold_1 = 0
results_received = False
seconds = 0.0
volume_1 = 0.0
volume = 0.0
chassis_nr = ''
connect = False
call snap7 client function
plc = snap7.client.Client(lib_location='/usr/local/lib/libsnap7.so')
IP address, rack, slot (from HW settings)
plc.connect('10.19.123.66', 0, 1)
#Get plc is connected true or flase
plc_connected = plc.get_connected()
read plc state run/stop/error
state = plc.get_cpu_state()
print plc state
print(f'State :{state}')
def test_wait_as_completion_timeouted(self, timeout=0, tries=500):
# Cli_WaitAsCompletion
# prepare Server
area = Areas.DB
dbnumber = 1
size = 1
start = 1
data = bytearray(size)
wordlen, data = self.client._prepare_as_read_area(area, size)
pdata = ctypes.byref(data)
self.client.write_area(area, dbnumber, start, bytearray(data))
# start as_request and wait for zero seconds to try trigger timeout
for i in range(tries):
self.client.as_read_area(area, dbnumber, start, size, wordlen, pdata)
res = None
try:
res = self.client.wait_as_completion(timeout)
check_error(res)
except RuntimeError as s7_err:
if not s7_err.args[0] == b'CLI : Job Timeout':
self.fail(f"While waiting another error appeared: {s7_err}")
# Wait for a thread to finish
time.sleep(0.1)
return
except BaseException:
self.fail(f"While waiting another error appeared:>>>>>>>> {res}")
def test_check_as_completion(self, timeout=5):
# Cli_CheckAsCompletion
check_status = ctypes.c_int(-1)
pending_checked = False
# preparing Server values
data = bytearray(b'\x01\xFF')
size = len(data)
area = Areas.DB
db = 1
start = 1
self.client.write_area(area, db, start, data)
def readBool(db_number, start_offset, bit_offset):
reading = plc.db_read(db_number, start_offset, 1)
a = snap7.util.get_bool(reading, 0, bit_offset)
#print('DB Number: ' + str(db_number) + ' Bit: ' + str(start_offset) + '.' + str(bit_offset) + ' Value: ' + str(a))
def readMemory(start_address,length):
def writeMemory(start_address,length,value):
plc.mb_write(start_address, length, bytearray(struct.pack('>f', value))) # big-endian
plc.disconnect()
print('Volume to PLC:' + str(value))
def append_new_line(file_name, text_to_append):
"""Append given text as a new line at the end of file"""
# Open the file in append & read mode ('a+')
with open(file_name, "a+") as file_object:
# Move read cursor to the start of file.
file_object.seek(0)
# If file is not empty then append '\n'
data = file_object.read(100)
if len(data) > 0:
file_object.write("\n")
# Append text at the end of file
file_object.write(text_to_append)
def tic():
global _start_time
_start_time = time.time()
def tac():
t_sec = round(time.time() - _start_time)
(t_min, t_sec) = divmod(t_sec,60)
(t_hour,t_min) = divmod(t_min,60)
print('Time passed: {}hour:{}min:{}sec'.format(t_hour,t_min,t_sec))
def sendmail(message_to_send):
def disconnect(self) -> int:
"""Disconnect a client.
Returns:
Error code from snap7 library.
"""
logger.info("disconnecting snap7 client")
result = self.library.Cli_Disconnect(self.pointer)
check_error(result, context="client")
return result
Write results to a file.
file_name = tkinter.filedialog.askopenfilename()
#Write reject results to a file
reject_filename = tkinter.filedialog.askopenfilename()
while(True):
Error :
b' ISO : An error occurred during recv TCP : Connection timed out'
Traceback (most recent call last):
File "/home/momo3434/Desktop/PyProject/primer/python/snap7_/zone4_primer_1.py", line 231, in
read_start_req = readBool(127,4004,1)
File "/home/momo3434/Desktop/PyProject/primer/python/snap7_/zone4_primer_1.py", line 136, in readBool
reading = plc.db_read(db_number, start_offset, 1)
File "/usr/local/lib/python3.9/dist-packages/snap7/client.py", line 226, in db_read
check_error(result, context="client")
File "/usr/local/lib/python3.9/dist-packages/snap7/common.py", line 89, in check_error
raise RuntimeError(error)
RuntimeError: b' ISO : An error occurred during recv TCP : Connection timed out'
Beta Was this translation helpful? Give feedback.
All reactions