package modbusx import ( "encoding/binary" ) func Read(ipAddr string, address uint16, quantity uint16) (data []byte, err error) { address-- cli := getModbusConnection(ipAddr) data, err = cli.ReadHoldingRegisters(address, quantity) dealErr(err, ipAddr) if err != nil { cli = getModbusConnection(ipAddr) data, err = cli.ReadHoldingRegisters(address, quantity) dealErr(err, ipAddr) } return } func Write(ipAddr string, address uint16, value int, endian string, length int) (err error) { address-- var bytesVal []byte bytesVal = intToBytes(value, endian, length) cli := getModbusConnection(ipAddr) _, err = cli.WriteMultipleRegisters(address, uint16(len(bytesVal)), bytesVal) dealErr(err, ipAddr) return err } func intToBytes(value int, endian string, length int) (data []byte) { if endian == "mix" { data = make([]byte, 0, 4) highVal := value / (1 << 16) highBts := make([]byte, 2) binary.BigEndian.PutUint16(highBts, uint16(highVal)) lowValue := value % (1 << 16) lowBts := make([]byte, 2) binary.BigEndian.PutUint16(lowBts, uint16(lowValue)) data = append(data, lowBts...) data = append(data, highBts...) } else { if length == 1 { data = make([]byte, 2) if endian == "big" { binary.BigEndian.PutUint16(data, uint16(value)) } else { binary.LittleEndian.PutUint16(data, uint16(value)) } } else { data = make([]byte, 4) if endian == "big" { binary.BigEndian.PutUint32(data, uint32(value)) } else { binary.LittleEndian.PutUint32(data, uint32(value)) } } } return } func dealErr(err error, ipAddr string) { if err != nil { unsetModbusConnection(ipAddr) //失败则删除缓存的连接 } }