|
var modbus = import("modbus")
var fmt = import("fmt")
var binary = import("encoding/binary")
var time = import("time")
size = 200
discretes = make([]bool,size)
holdingRegisters= make([]int64,size)
ReadCoilsForSignalingBuffer = make([]bool,size)
func fillAm3() {
for{
for i=0; i<len(holdingRegisters);i++ {
holdingRegisters = i
}
for i=0; i<len(ReadCoilsForSignalingBuffer);i++ {
ReadCoilsForSignalingBuffer = i
}
time.Sleep(4*time.Second)
}
}
func ReadCoilsForSignaling (address, quantity){
address = toInt(address)
quantity = toInt(quantity)
return ReadCoilsForSignalingBuffer[address : address+quantity], nil
}
func ReadHoldingRegisters(address, quantity) {
address = toInt(address)
quantity = toInt(quantity)
return holdingRegisters[address : address+quantity], nil
}
func OnErrorImp(address, quantity) {
Print("error received: %v from req: %v\n", errRep, req)
}
serialConfig = new(modbus.Config)
serialConfig.Name="COM11"
serialConfig.Baud=115200
serialConfig.StopBits=modbus.StopBits1
serialConfig.Parity=modbus.ParityNone
go fillAm3()
s, err = modbus.OpenPort(serialConfig)
if err != nil {
Print("open serial error: %v\n", err)
}
com = modbus.NewSerialContext(s, 115200)
id, err = modbus.Uint64ToSlaveID(1)
if err != nil {
Print("set slaveID error: %v\n", err)
}
device = modbus.NewRTUServer(com, id)
HandlerFunc = new(modbus.SimpleHandler)
HandlerFunc.ReadHoldingRegisters = ReadHoldingRegisters
HandlerFunc.ReadCoils = ReadCoilsForSignaling
HandlerFunc.OnErrorImp = OnErrorImp
err = device.Serve(HandlerFunc)
if err != nil {
Print("serve error: %v\n", err)
}
|
|