| | |
| | | return |
| | | } |
| | | |
| | | readRequest, err := connection.ReadRequestBuilder().AddTagAddress("count", "holding-register:1230:DWORD").Build() |
| | | // 读模式 |
| | | readRequest, err := connection.ReadRequestBuilder().AddTagAddress("count", "holding-register:1230:DINT[20]").Build() |
| | | if err != nil { |
| | | fmt.Printf("Error preparing read-request:%s\n", err.Error()) |
| | | return |
| | | } |
| | | |
| | | // 执行 |
| | | readResult := <-readRequest.Execute() |
| | | if err := readResult.GetErr(); err != nil { |
| | | fmt.Printf("Error execting read-request:%s\n", err.Error()) |
| | | return |
| | | } |
| | | |
| | | // Do something with the response |
| | | // 判断响应码是否正确 |
| | | if readResult.GetResponse().GetResponseCode("count") != apiModel.PlcResponseCode_OK { |
| | | fmt.Printf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode("tag").GetName()) |
| | | fmt.Printf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode("count").GetName()) |
| | | return |
| | | } |
| | | |
| | | // 测试获取变量名称 |
| | | //names := readResult.GetResponse().GetTagNames() |
| | | //fmt.Printf("Got tag names %+v", names) |
| | | |
| | | value := readResult.GetResponse().GetValue("count") |
| | | fmt.Printf("Got result %d\n", value.GetUint32()) |
| | | fmt.Printf("Got result length %d\n", value.GetLength()) |
| | | |
| | | // 按字符串打印变量 |
| | | fmt.Printf("val data: %s\n", value.GetRaw()) |
| | | for _, val := range value.GetList() { |
| | | fmt.Printf("%d ", val.GetUint16()) |
| | | } |
| | | |
| | | } |