Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opcua 写值失败的问题 #84

Open
alongL opened this issue Nov 29, 2022 · 1 comment
Open

opcua 写值失败的问题 #84

alongL opened this issue Nov 29, 2022 · 1 comment

Comments

@alongL
Copy link
Contributor

alongL commented Nov 29, 2022

mappers\opcua\driver\client.go
Set的时候所写的opcua节点的属性必须是String类型的才可以成功。
如果opcua节点的是Int32的,此时Set会失败。
参考:gopcua/opcua#310

func (c *OPCUAClient) Set(nodeID string, value string) (results string, err error) {
    ///...

	v, err := ua.NewVariant(value) //type of value is string
	if err != nil {
		klog.Errorf("invalid value: %v", err)
		return "", errors.New("Invalid value")
	}

	req := &ua.WriteRequest{
		NodesToWrite: []*ua.WriteValue{  // WriteValue failed if the type of node is not string.
			{
				NodeID:      id,
				AttributeID: ua.AttributeIDValue,
				Value: &ua.DataValue{
					EncodingMask: ua.DataValueValue,
					Value:        v,
				},
			},
		},
	}

	resp, err := c.Client.Write(req)

此处想写一个Int32的Node必须这样写:

func (c *OPCUAClient) Set(nodeID string, value string) (results string, err error) {
   ....
       // convert string to int32
	value64, err := strconv.ParseInt(value, 10, 32)
	if err != nil {
		klog.Errorf("value convert failed : %v", err)
	}
        var val32 int32
	val32 = int32(value64)
	//v, err := ua.NewVariant(value)
	v, err := ua.NewVariant(val32)  
      ...

如果运行gopcua自带的demo,写一个Int32型变量,也是一样的问题

go run examples/write/write.go -endpoint opc.tcp://192.168.1.228:4840  -node 'ns=1;s=start' -value 0
The value supplied for the attribute is not of the same type as the attribute's value. StatusBadTypeMismatch (0x80740000)
@sailorvii
Copy link
Collaborator

sailorvii commented Feb 1, 2023

Yes, you're right. The value transfer could be added at https://github.com/kubeedge/mappers-go/blob/b03f2300763bf4beb0c81ce2829052ffacea9129/mappers/opcua/device/device.go line 41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants