我在一个 Python 脚本中有这个函数,该脚本已作为 Windows 服务与 NSSM 一起安装。当 Python 脚本正常运行(即不作为服务)时,将打印文件。但是,当它作为服务安装时,它不会打印。
打印是通过 opc-ua 外部触发的。
脚本作为服务安装时甚至可以打印吗?在作为服务安装时,我也没有遇到任何异常。有时我KeyboardInterrupt
在循环之间睡觉时遇到异常。
def CheckOpcuaNode(latestPDF):
client = Client("opc.tcp://192.168.202.90:4840/")
try:
client.connect()
opcuaNode = client.get_node("ns=6;s=::AsGlobalPV:g_saveParameters.bPrintNow")
Result = opcuaNode.get_value()
if Result == True:
print("print file: %s" % str(latestPDF))
os.startfile(latestPDF, "print")
try:
time.sleep(4)
except KeyboardInterrupt: # Ignore keyborditerruption
print("ERROR KeyboardInterrupt while printing: %s" % sys.exc_info()[0])
opcuaNode.set_attribute(ua.AttributeIds.Value, ua.DataValue(False))
except:
print("ERROR while checking if PDF shall be printed: %s" % sys.exc_info()[0])
finally:
client.disconnect()
def LatestPdf():
return 'path\to\PDF\file.pdf'
if __name__ == '__main__':
while True:
latestPdfFile = LatestPdf()
if latestPdfFile != '':
CheckOpcuaNode(latestPdfFile) # check If PLC has asked to print pdf file
try:
time.sleep(4)
except KeyboardInterrupt: # Ignore keyborditerruption
print("ERROR KeyboardInterrupt between loops: %s" % sys.exc_info()[0])