Bitcoin Murderous Maniac
Bitcoin Murderous Maniac

Reputation: 1478

Why does this AssertionError exception not continue

What I Have

I posted the correlated question Is there a method to help make this Python logic run faster a few days ago and I've extended on this logic for my testing needs after getting a confirmed solution.

For some reason I cannot figure out how to tell this Python as it's running if it gets an AssertionError to print out Assertion Error and then continue back with the logic again.

from cpppo.server.enip.get_attribute import proxy_simple
from datetime import datetime
import time, csv, sys

host = "<PLC IP Address>"
TagName = "Tag Name"
RootCsvFile = "X:\\Some\\Folder\\Path\\"
source = proxy_simple(host)
prev = None

def csvwrite(v):
    with open(v[2],"a",newline='') as file:
        csv_file = csv.writer(file)
        csv_file.writerow([v[0],v[1]])

def ExecLoop(pr):
    prev = pr   
    while True:

        for message in source.read(TagName):

            try:
                val = message[0]
                timestr = str(datetime.now())
                if val != prev:
                    prev = val
                    YYYYMMDD = datetime.today().strftime('%Y%m%d')
                    CsvFile = RootCsvFile + TagName + "_" + YYYYMMDD + ".csv"
                    print(timestr, val, CsvFile)
                    csvwrite([timestr, val, CsvFile])

            except AssertionError:
                print("Assertion Error")
                continue           
ExecLoop(prev)

The Problem

Since this is getting data from a PLC machine network via a VPN connection, whenever the VPN has a disruption and is not able to be accessed via the Python, the below error occurs and it seems like the except AssertionError: is not doing what I've asked it to do and just get an error:

Traceback (most recent call last):   File
"~\GetTag-ToSQLDB&CSVEFile.py", line 63, in <module>
      ExecLoop(prev)   File "~\GetTag-ToSQLDB&CSVEFile.py", line 46, in ExecLoop
      for message in source.read(TagName):   File "~\Python\Python36-32\lib\site-packages\cpppo\server\enip\get_attribute.py",
line 431, in read
      for val,(sts,(att,typ,uni)) in reader:   File "~\Python\Python36-32\lib\site-packages\cpppo\server\enip\get_attribute.py",
line 606, in read_details
       depth=self.depth, multiple=self.multiple, timeout=self.timeout )): File
"~\Python\Python36-32\lib\site-packages\cpppo\server\enip\client.py",
line 1393, in operate

  for idx,dsc,req,rpy,sts,val in harvested:   File "~\Python\Python36-32\lib\site-packages\cpppo\server\enip\client.py",
line 1270, in pipeline 
complete, requests ) 

AssertionError: Communication ceased before harvesting all pipeline responses:   0/  1

What I'd Like

I'd like for it to keep retrying whenever there is an AssertionError since typically when there is an issue with connectivity, it's a short few second blip or else a couple minutes for an automated weekly power cycle of a network device on the client side of the VPN which the Python is running.

My Assumptions

I'm assuming the problem is related to:


Other Details (Just In Case)

Upvotes: 1

Views: 665

Answers (2)

Mad Physicist
Mad Physicist

Reputation: 114518

The traceback says it all: your except block is not in a place where it can see the exception at all. Specifically, look at the line

for message in source.read(TagName): File "~\Python\Python36-32\lib\site-packages\cpppo\server\enip\get_attribute.py", line 431, in read

The error is in your for loop, but the try is inside the loop. You had the right idea wrapping everything in a while True. The solution is to move the for inside the try.

Upvotes: 1

TwistedSim
TwistedSim

Reputation: 2030

The AssertionError occured in the source.read(TagName) call. you need to wrap this with your try except block:

while True:
    try:
        for message in source.read(TagName):   # Note that this connection might be broken after the error. 
            val = message[0]
            timestr = str(datetime.now())
            if val != prev:
                prev = val
                YYYYMMDD = datetime.today().strftime('%Y%m%d')
                CsvFile = RootCsvFile + TagName + "_" + YYYYMMDD + ".csv"                       print(timestr, val, CsvFile)
                csvwrite([timestr, val, CsvFile])
     except AssertionError:
         print("Assertion Error")
         continue  

Upvotes: 1

Related Questions