pyads通知模块无异常抛出但程序停滞的问题求助
问题描述
我开发了一款信号处理工具,正常运行状态符合预期,仅当出现可处理异常时程序才会终止。我已使用try语句捕获并记录所有可能出现的异常,但目前程序出现了无异常抛出却直接停滞的情况。我原本考虑实现一个超时定时器,当任务执行超时触发异常,但或许有更简便的方案?
经排查发现,当ADS服务器断开连接时,pyads的通知模块并未抛出异常。
相关代码
import pyads import ctypes from time import sleep import numpy as np import pandas as pd import logging from os import getcwd class VibrationData: def __init__(self): self.plc = pyads.Connection('127.0.0.1.1.1', pyads.PORT_TC3PLC1, ip_address='127.0.1.1') self.plc.open() self.n_channels = self.plc.read_by_name("GVL_Constants.nMaxChannels", plc_datatype=pyads.PLCTYPE_UDINT) self.signal_length = self.plc.read_by_name("GVL_Constants.nSignalLength", plc_datatype=pyads.PLCTYPE_UDINT) self.sample_rate = self.plc.read_by_name("GVL_Constants.fSampleRate", plc_datatype=pyads.PLCTYPE_LREAL) c_Measurement_Range_Acc = self.plc.read_by_name("GVL_Constants.fMeasurementRangeAcc", plc_datatype=pyads.PLCTYPE_LREAL) c_Resolution = self.plc.read_by_name("GVL_Constants.fResolutionAcc", plc_datatype=pyads.PLCTYPE_LREAL) c_Gravitational_Acc = 9.81 c_Sensitivity_Acc = 0.1 self.scale = c_Measurement_Range_Acc*c_Gravitational_Acc/(c_Sensitivity_Acc*c_Resolution) self.buffer_factor = self.plc.read_by_name("GVL_Constants.nBufferFactor", plc_datatype=pyads.PLCTYPE_UDINT) self.buffer_length = int(self.signal_length/self.buffer_factor) self.sample_idx = 0 def get_structsize(self): raw_acc_tuple = list() for i in range(self.n_channels): raw_acc_tuple.append(tuple(('Iepe'+str(i),pyads.PLCTYPE_DINT,self.buffer_length))) self.structure = tuple(raw_acc_tuple) self.size_of_struct = int(pyads.size_of_structure(self.structure)) self.attr = pyads.NotificationAttrib(self.size_of_struct) def scale_raw_data(self, raw_data): for i in range(self.n_channels): ch_name = 'Iepe'+str(i) self.raw_acc[i,int((self.sample_idx)*self.buffer_length):int((self.sample_idx+1)*self.buffer_length)] = raw_data[ch_name] #initializing of the exception logger logging.basicConfig(format='%(asctime)s %(levelname)s %(name)s %(message)s', filename='errorlog.txt', level=logging.DEBUG) #initialize control variables for signal processing init_state = True while True: try: if init_state == True: init_state = False x = VibrationData() data_arrived = False acquisition_active = False sample_idx = 1 @x.plc.notification(ctypes.c_ubyte * x.size_of_struct) def callback(handle, name, timestamp, value): global data_arrived values = pyads.dict_from_bytes(value, x.structure) x.scale_raw_data(values) x.sample_idx += 1 if x.sample_idx == x.signal_length/x.buffer_length: data_arrived = True x.sample_idx = 1 #print("Init done!") if acquisition_active == False: notification = x.plc.add_device_notification('MAIN_IEPE.anBufferPython',x.attr,callback) acquisition_active = True if data_arrived == True: print('data arrived!') acquisition_active = False data_arrived = False x.plc.del_device_notification(notification[0],notification[1]) except: logging.exception('Exception occured.') init_state = True x.plc.del_device_notification(notification[0],notification[1]) sleep(5)
内容的提问来源于stack exchange,提问作者Maik_FLS
相关产品推荐
相关产品推荐

