FreeOpcUa / python-opcua

LGPL Pure Python OPC-UA Client and Server
http://freeopcua.github.io/
GNU Lesser General Public License v3.0
1.36k stars 658 forks source link

Trouble writing 32 bit integers #662

Open johns10 opened 6 years ago

johns10 commented 6 years ago

First off, thanks for all the great work you've done on this module. I use it almost every day, and have developed a bunch of python scriptlets that accomplish various equipment integration tasks for my company. It's awesome to work with.

I've solved this with one of your other answers, but I'd really like to understand how to write a 32 bit integer correctly, and hopefully learn a bit in the process.

I'm working with kepware, and the tag I'm working with is a long. The response from datavalue = tag.get_data_value() is: DataValue(Value:Variant(val:3,type:VariantType.Int32), StatusCode:StatusCode(Goo d), SourceTimestamp:2018-07-26 21:46:20.195352) I've got the following code written: from opcua import Client, ua client = Client("opc.tcp://192.168.138.100:49320/") client.connect() tag = client.get_node("ns=2;s=FunnelCloud.Simulator.ST05.DowntimeReason") datavalue = ua.DataValue(ua.Variant(78, ua.VariantType.Int32)) tag.set_data_value(datavalue) OR from opcua import Client, ua from numpy import uint32 client = Client("opc.tcp://192.168.138.100:49320/") client.connect() tag = client.get_node("ns=2;s=FunnelCloud.Simulator.ST05.DowntimeReason") datavalue = ua.DataValue(ua.Variant(78, ua.VariantType.Int16)) tag.set_value(datavalue) The response to that code is opcua.ua.uaerrors._auto.BadTypeMismatch: The value supplied for the attribute is not of the same type as the attribute"s value.(BadTypeMismatch) However, if I run: from opcua import Client, ua client = Client("opc.tcp://192.168.138.100:49320/") client.connect() tag = client.get_node("ns=2;s=FunnelCloud.Simulator.ST05.DowntimeReason") datavalue = ua.DataValue(ua.Variant(78, ua.VariantType.UInt16)) tag.set_data_value(datavalue)

It succeeds. The only thing i can figure is that the python integer is an unsigned 16 bit integer (which doesn't seem to add up) out of the box, and it has to match the variant type? I got it to work from you answer here, but I'd really like to understand what's going on.

oroulet commented 6 years ago

If you read an int32 then you definitely need to write an int32. Does tag.set_value(4, ua.Int32) works? Next step is probably too record a session with Wireshark and see what get read and write. I would be very surprised if we had a bug here

oroulet commented 6 years ago

What returns tag.get_data_type()?

johns10 commented 6 years ago

Bear with me, I'm a bit green.
tag.set_value(4, ua.Int32) returns AttributeError: module 'opcua.ua' has no attribute 'Int32' Do I have a configuration/setup/import I'm missing? I'm using: from opcua import Client, ua tag.get_data_type() returns TwoByteNodeId(i=5) Which would explain why datavalue = ua.DataValue(ua.Variant(78, ua.VariantType.Int16)) Works.

oroulet commented 6 years ago

I meant tag.set_value(4, ua.VariantType.Int32) but now from your variant type it looks like you variable is In16 not Int32 so the strange thing is why the read gives us an Int32. Looks like you need wireshark...

johns10 commented 6 years ago

I'll look at it again on Monday, validate what I'm seeing on quick client and run wireshark.

johns10 commented 6 years ago

Also, I'm really interested in contributing to this project, or to the asyncio branch that cbergmiller is working on. Is there anything that an amateur/intermediate python programmer can do to help? I'd even be willing to throw in on documentation or testing, or whatever you guys need. I'm moving to the asyncio branch for my major project right now, and I'd love to do something to help. If you want to communicate about it over email, I'm johns10@gmail.com. I'm no expert, but I'm highly motivated to learn this stuff.

oroulet commented 6 years ago

@johns10 any help is welcome. I guess the best way to help is to use the library, find bugs and fix them or fix existing bugs. Go through all reported bugs, close those that should be closed and and fix those that are easy. Things like this one is very easy to fix: https://github.com/FreeOpcUa/python-opcua/issues/594 Concerning async it would be great to to merge the work of cbergmiller instead of maintaining a fork as he does. I am ready to drop python2 support and require 3.6 or 3.7 in master. Using decorator it should be possible to have pure async implementation and use some decorator to offer a sync interface for people wanting it. If you know python well this might be maybe the most usefull help for the time being...

johns10 commented 6 years ago

I've got the async branch running in my app now. It runs with aioamqp for comms to a webserver. I did have issues with several of the examples, specifically with using events. Where should I post bigs for the asyncio version? Here or on the branch? I didn't see an issues tab on the branch. I'll give the bug you referenced a shot. Just bear with me. It's still amatuer hour for me 😀

oroulet commented 6 years ago

@johns10 just testing, reportingand trying to fix examples and small things is allready plenty of help! I am trying to contact @cbergmiller and I will move/fork his repository to freeopcua group

cbergmiller commented 6 years ago

Hi @johns10, only a few examples have been refactored in the asyncio branch so far. I just pushed a few commits that contain the updated example for server-events: https://github.com/cbergmiller/python-opcua/blob/make/asyncio/examples/server-events.py You can give it a try if you want to.

oroulet commented 6 years ago

@cbergmiller what about making you a member of freeopcua and moving your repository here?

cbergmiller commented 6 years ago

@oroulet thanks for adding me to the team! I think it would be a great to move the asyncio version of python-opcua here. I would like to get most of the tests working first though.