OPC UA 최소 읽기 및 쓰기

6301 단어 Python3OPC-UA

개요


이 기사에서는 OPC UA 서버 측 My Object>My Bariable에 대한 쓰기, 읽기 값을 보여 줍니다.
OPC UA 서버에서 데이터를 쓰고 읽는 데 필요한 최소한의 OPC UA 그룹을 나타내는 매개 변수입니다.
UaExpert를 통해 Python용 OPC UA Server 라이브러리를 사용하는 서버와 연결, 읽기, 쓰기를 위한 통신 데이터 패키지를 가져옵니다.
그 데이터 패키지의 일부를 변경해서 서버에 보내서 정상적으로 통신하게 했다.이때 메모는 다음과 같습니다.
※ 주의: OPC UA의 규격을 확인한 것이 아니라 개인 분석이기 때문에 본 기사의 내용은 모든 OPC UA 서버에 적용되지 않습니다.
설명 절차는 다음과 같다.
  • 서버 측 준비
  • 세션 설정(클라이언트)
  • 쓰기(클라이언트)
  • 읽기(클라이언트)
  • 환경


    OPC UA Server : https://github.com/FreeOpcUa/python-opcua
    Python3 : Python 3.6.9
    OPC UA 서버측 IP 주소: 192.168.1.35
    OPC UA 클라이언트 IP 주소: 192.168.1.104

    서버 측 준비


    서버 프로그램


    서버 프로그램을 실행합니다.
    sango@example:~/opcua$ python3 serv.py
    Endpoints other than open requested but private key and certificate are not set.
    Listening on 0.0.0.0:4840
    
    다음은 소스 코드입니다.
    import sys
    sys.path.insert(0, "..")
    import time
    
    from opcua import ua, Server
    
    if __name__ == "__main__":
    
        # setup our server
        server = Server()
        server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
    
        # setup our own namespace, not really necessary but should as spec
        uri = "http://examples.freeopcua.github.io"
        idx = server.register_namespace(uri)
    
        # get Objects node, this is where we should put our nodes
        objects = server.get_objects_node()
    
        # populating our address space
        myobj = objects.add_object(idx, "MyObject")
        myvar = myobj.add_variable(idx, "MyVariable", 3535)
        myvar.set_writable()    # Set MyVariable to be writable by clients
    
        # start
        server.start()
    
        try:
            while True:
                time.sleep(1)
        finally:
            #close connection, remove subcsriptions, etc
            server.stop()
    

    (참조) 이 예제의 UaExpert 서버 모양새 방법



    세션 설정 (클라이언트)


    Hello message & Ack

  • Hello message
  • Acknowledge message
  • Open Secure Channel Request & Response

  • Request

    요청할 때 Sequence Number를 설정하고 나중에 요청할 때마다 1씩 증가합니다.
    또한 이후 Sequence Number의 이름은 Security Sequence Number로 변경됩니다.
    이 예에서는 SequenceNumber(Security SequenceNumber)로 설정합니다. 51.
  • Response

    SecureChannel Id 및 Secure Token Id를 기록해 둡니다.
    이 예에서는 SecureChannel Id:7, Secure Token Id:14입니다.
    나중에 클라이언트 측에서 요청을 보낼 때 이 두 Id를 머리 부분에 설정해야 합니다.
  • Create Session Request & Response

  • Request

    이 예에서 방금 기록한 Secure Channel Id:7, Secure Token Id:14를 설정하고 요청을 보냅니다.
    이 예에서 보안 시퀀스 번호: 52.
  • Response
  • Activate Session Request & Response

  • Request

    이 예에서 방금 기록한 Secure Channel Id:7, Secure Token Id:14를 설정하고 요청을 보냅니다.
    이 예에서는 보안 시퀀스 번호:53으로 설정합니다.
  • Response
  • 쓰기 (클라이언트)

  • Request

    위에서 말한 바와 같이 OPC UA 서버가 시작될 때 My Object>My Bariable 저장소는 3535입니다.
    이 예에서 MyObject>Mybariable에 4047(0x0fcf)을 기록합니다.
  • 작성할 Namespace 인덱스 지정, Identifier Numeric
    이 예에서 Namespace Index:2, Identifier Numeric:2를 지정했는데 이것은 My Object>My Bariable을 가리킨다.
    (확인되지 않음) EncodingMask에서 has value(0x01)를 지정해야 할 수 있습니다.
    메시지 크기를 올바르게 설정하지 않으면 서버 측에서 OPC UA 그룹을 올바르게 식별할 수 없습니다.
    메시지 크기의 범위는 WireShark의 OpcUa Binary Protocol을 가리킨다.
  • Response

  • 읽기 (클라이언트)

  • Request

    읽을 Namespace 인덱스 지정, Identifier Numeric
    이 예에서 Namespace Index:2, Identifier Numeric:2를 지정했는데 이것은 My Object>My Bariable을 가리킨다.
    읽을 속성 지정 Id
    이 예에서는 값(0x000000d)을 지정합니다.
  • Response

    이 예에서 방금 설정한 My Object>My Bariable에서 4047(0x0fcf)을 읽습니다.
  • 총결산

  • 클라이언트가 요청할 때 Sequence Number를 설정하고 나중에 요청할 때마다 1씩 증가합니다.
  • 세션을 만들 때 클라이언트가 요청할 때 서버 측에서 지정한 SecureChannel Id, Secure Token Id를 머리에 설정해야 합니다.
  • 클라이언트 측의 쓰기 요청 시 쓰고 싶은 Namespace Index, Identifier Numeric를 지정해야 합니다.
  • 클라이언트 측의 읽기 요청 시 읽으려는 Namespace 인덱스, Identifier Numeric, AttributeId를 지정해야 합니다.
  • 좋은 웹페이지 즐겨찾기