S Andrew
S Andrew

Reputation: 7208

FreeOPCUA python client example

I am using freeOpcua to write a simple client example. I first tested this in my laptop by running its server code and then running client code from linux in raspberry pi3. So I was able to connect to server .

Now I have a PLC which is running B&R Server. I need to connect to this server and get the attributes value but I everytime I try to connect it shows connection refused error. The server is running fine because I have tested it using client software on windows but not connecting in linux. I am using freeopcua client example to connect to server.

Do I need to enable port on which I am trying to communicate.?

Does anyone have any experience in this. Please help. Thanks.

EDIT:

The IP address is 192.168.1.21 and the port number is 135. I am doing something like below:

client = Client("opc.tcp://192.168.1.21:135/")
client.connect()
#This gives error of connection refused.

I opened the client.connect() and found out that it has below functions:

connect_socket()
send_hello()
open_secure_channel()
create_session()
activate_session()

So instead of doing client.connect() I did client.connect_socket() So it went ok and didnt gave any error. Then I print(client.get_root_node()) it showed me Node(TwoByteNodeId(i=84)) and print(client.get_server_node()) it shows Node(FourByteNodeId(i=2253)). What are these values. Can I consider that I am able to connect to the server by doing client.connect_socket().

When I tried to get the endpoints by using client.get_endpoints() it gave me below error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "opcua/client/client.py", line 299, in get_endpoints
    return self.uaclient.get_endpoints(params)
  File "opcua/client/ua_client.py", line 323, in get_endpoints
    data = self._uasocket.send_request(request)
  File "opcua/client/ua_client.py", line 76, in send_request
    data = future.result(self.timeout)
  File "/usr/local/lib/python2.7/dist-packages/concurrent/futures/_base.py", 
line 431, in result
    raise TimeoutError()
concurrent.futures._base.TimeoutError

Also when I tried to do open_secure_channel() or create_session() or any other it gave me above error.

Upvotes: 2

Views: 2969

Answers (1)

joeb
joeb

Reputation: 36

I had this same problem. I found out that the Hello message was setting the MaxMessageSize and MaxChunkCount to 0. I changed the freeopcua code so that it set to the same values used by UaExpert, and it worked.

So in the freeopcua code, in freeopcua/ua/uaprotocol_hand.py, I changed the init of the Hello class:

class Hello(uatypes.FrozenClass):

    def __init__(self):
        self.ProtocolVersion = 0
        self.ReceiveBufferSize = 65536
        self.SendBufferSize = 65536
        self.MaxMessageSize = 16777216
        self.MaxChunkCount = 5000
        self.EndpointUrl = ""
        self._freeze = True

If you don't know how to edit a python package, mine was located here: C:\Users\username\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\opcua\ua

Basically your python_directory\Lib\site-packages\opcua\ua

Edit: My open_secure_channel() was working before I made this change, so you may have a different problem. Before this change my create_sessions() and get_endpoints() would always fail.

Upvotes: 2

Related Questions