Skip to content

opcua 写值失败的问题 #84

@alongL

Description

@alongL

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions