| | |
| | | |
| | | // 根据设置的地址查询数据,上报 |
| | | plcResponse.Data = make(map[int][]byte, 0) |
| | | plcResponse.Message = "" |
| | | for _, addr := range dev.Address { |
| | | result, err := ReadHoldingRegister(conn, addr) |
| | | if err != nil { |
| | | logger.Warn("plc device Read Holding Register error, %s", err.Error()) |
| | | plcResponse.Message = err.Error() |
| | | } else { |
| | | plcResponse.Data[addr] = result |
| | | } |
| | | } |
| | | |
| | | msg.SendDeviceLiveData(&plcResponse) |
| | | if plcResponse.Message != "" { |
| | | return |
| | | } |
| | | |
| | | // 间隔时间 |
| | | time.Sleep(time.Duration(dev.Interval) * time.Second) |
| | | } |
| | |
| | | // 读模式 |
| | | readRequest, err := connection.ReadRequestBuilder().AddTagAddress("tag", tagAddress).Build() |
| | | if err != nil { |
| | | fmt.Printf("Error preparing read-request:%s\n", err.Error()) |
| | | fmt.Printf("preparing read-request:%s\n", err.Error()) |
| | | return nil, err |
| | | } |
| | | |
| | | // 执行 |
| | | readResult := <-readRequest.Execute() |
| | | if err := readResult.GetErr(); err != nil { |
| | | fmt.Printf("Error execting read-request:%s\n", err.Error()) |
| | | fmt.Printf("execting read-request:%s\n", err.Error()) |
| | | return nil, err |
| | | } |
| | | |
| | | // 判断响应码是否正确 |
| | | if readResult.GetResponse().GetResponseCode("tag") != apiModel.PlcResponseCode_OK { |
| | | fmt.Printf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode("tag").GetName()) |
| | | return nil, err |
| | | return nil, nil |
| | | } |
| | | |
| | | value := readResult.GetResponse().GetValue("tag") |
| | |
| | | } |
| | | |
| | | type PLCResponse struct { |
| | | Id string |
| | | Name string |
| | | Ip string |
| | | Online bool |
| | | Data map[int][]byte |
| | | Id string |
| | | Name string |
| | | Ip string |
| | | Online bool |
| | | Message string |
| | | Data map[int][]byte |
| | | } |