kubeedge / mappers-go

KubeEdge Device Mappers written in go
Apache License 2.0
45 stars 64 forks source link

opcua 写值失败的问题 #84

Open alongL opened 1 year ago

alongL commented 1 year ago

mappers\opcua\driver\client.go Set的时候所写的opcua节点的属性必须是String类型的才可以成功。 如果opcua节点的是Int32的,此时Set会失败。 参考:https://github.com/gopcua/opcua/issues/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 commented 1 year ago

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