package modbusx
|
|
import (
|
"encoding/json"
|
)
|
|
func Read(ipAddr string, address uint16, quantity uint16) (data []byte, err error) {
|
cli := getModbusConnection(ipAddr)
|
data, err = cli.ReadHoldingRegisters(address, quantity)
|
return
|
}
|
|
func Write(ipAddr string, address uint16, value interface{}) (err error) {
|
bytesVal, err := json.Marshal(value)
|
if err != nil {
|
return err
|
}
|
cli := getModbusConnection(ipAddr)
|
_, err = cli.WriteMultipleRegisters(address, uint16(len(bytesVal)), bytesVal)
|
if err != nil {
|
return err
|
}
|
//time.Sleep(time.Second)
|
//// 校验写入的数据
|
//result, err := cli.ReadHoldingRegisters(address, uint16(len(bytesVal)))
|
//if err != nil {
|
// return err
|
//}
|
//resultVal := int(binary.BigEndian.Uint16(result))
|
//valueInt := cast.ToInt(value)
|
//if resultVal != valueInt {
|
// return errors.New("write result not equal read result")
|
//}
|
return nil
|
}
|