python读写kepware数据

import sys

sys.path.insert(0, "..")
import logging

from opcua import ua, Client
from time import sleep




if __name__ == "__main__":
    # from IPython import embed
    logging.basicConfig(level=logging.WARNING)
    client = Client("opc.tcp://192.168.136.206:49320")

    try:
        client.connect()

        sfc = client.get_node('ns=2;s=GK.SFC.sfcCode')

        a_start_trigger = client.get_node('ns=2;s=GK.A.start.trigger')
        a_start_success = client.get_node('ns=2;s=GK.A.start.success')
        a_report_trigger = client.get_node('ns=2;s=GK.A.report.trigger')
        a_report_success = client.get_node('ns=2;s=GK.A.report.success')
        a_verify_trigger = client.get_node('ns=2;s=GK.A.verify.trigger')
        a_verify_success = client.get_node('ns=2;s=GK.A.verify.success')
        a_transfer_trigger = client.get_node('ns=2;s=GK.A.transfer.trigger')
        a_transfer_success = client.get_node('ns=2;s=GK.A.transfer.success')

        while True:
            sleep(1)
            logging.warning('本次操作SFC:【' + str(sfc.get_value()) + '】')

            a_s_t = a_start_trigger.get_value()
            a_s_s = a_start_success.get_value()
            a_r_t = a_report_trigger.get_value()
            a_r_s = a_report_success.get_value()
            a_v_t = a_verify_trigger.get_value()
            a_v_s = a_verify_success.get_value()
            a_t_t = a_transfer_trigger.get_value()
            a_t_s = a_transfer_success.get_value()

            if a_s_t is True and a_r_t is False and a_v_t is False and a_t_t is False:
                logging.warning('开工')
                try:
                    sleep(10)
                    if a_start_success.get_value() is True:
                        logging.warning('开工成功,重置返回')
                        client.get_node(a_start_success).set_value(ua.DataValue(ua.Variant(False, ua.VariantType.Boolean)))
                except Exception as e:
                    logging.error('开工重置失败:', e)
                finally:
                    logging.warning('重置开工信号')
                    client.get_node(a_start_trigger).set_value(ua.DataValue(ua.Variant(False, ua.VariantType.Boolean)))
            if a_s_t is False and a_r_t is True and a_v_t is False and a_t_t is False:
                logging.warning('报工')
                try:
                    sleep(10)
                    if a_report_success.get_value() is True:
                        logging.warning('报工成功,重置返回')
                        client.get_node(a_report_success).set_value(ua.DataValue(ua.Variant(False, ua.VariantType.Boolean)))
                except Exception as e:
                    logging.error('报工重置失败:', e)
                finally:
                    logging.warning('重置报工信号')
                    client.get_node(a_report_trigger).set_value(ua.DataValue(ua.Variant(False, ua.VariantType.Boolean)))
            if a_s_t is False and a_r_t is False and a_v_t is True and a_t_t is False:
                logging.warning('判定')
                try:
                    sleep(10)
                    if a_verify_success.get_value() is True:
                        logging.warning('判定成功,重置返回')
                        client.get_node(a_verify_success).set_value(ua.DataValue(ua.Variant(False, ua.VariantType.Boolean)))
                except Exception as e:
                    logging.error('判定重置失败:', e)
                finally:
                    logging.warning('重置判定信号')
                    client.get_node(a_verify_trigger).set_value(ua.DataValue(ua.Variant(False, ua.VariantType.Boolean)))
            if a_s_t is False and a_r_t is False and a_v_t is False and a_t_t is True:
                logging.warning('转序')
                try:
                    sleep(10)
                    if a_transfer_success.get_value() is True:
                        logging.warning('转序成功,重置返回')
                        client.get_node(a_transfer_success).set_value(ua.DataValue(ua.Variant(False, ua.VariantType.Boolean)))
                except Exception as e:
                    logging.error('转序重置失败:', e)
                finally:
                    logging.warning('重置转序信号')
                    client.get_node(a_transfer_trigger).set_value(ua.DataValue(ua.Variant(False, ua.VariantType.Boolean)))

    finally:
        pass

 

你可能感兴趣的:(Python)