Skip to content

Commit

Permalink
Allow to distinguish different function codes when writing modbus reg…
Browse files Browse the repository at this point in the history
…isters
  • Loading branch information
andig committed Mar 10, 2023
1 parent 364e71b commit 76f3ce6
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ type DiscreteInputsRequest struct {

// Request object passed to the holding register handler.
type HoldingRegistersRequest struct {
ClientAddr string // the source (client) IP address
UnitId uint8 // the requested unit id (slave id)
Addr uint16 // the base register address requested
Quantity uint16 // the number of consecutive registers covered by this request
IsWrite bool // true if the request is a write, false if a read
Args []uint16 // a slice of register values to be set, ordered from
WriteFuncCode uint8 // the function code of the write request
ClientAddr string // the source (client) IP address
UnitId uint8 // the requested unit id (slave id)
Addr uint16 // the base register address requested
Quantity uint16 // the number of consecutive registers covered by this request
IsWrite bool // true if the request is a write, false if a read
Args []uint16 // a slice of register values to be set, ordered from
// Addr to Addr + Quantity - 1 (for writes only)
}

Expand Down Expand Up @@ -531,12 +532,13 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
// invoke the handler
_, err = ms.handler.HandleHoldingRegisters(
&HoldingRegistersRequest{
ClientAddr: clientAddr,
UnitId: req.unitId,
Addr: addr,
Quantity: 1, // request for a single register
IsWrite: true, // request is a write
Args: []uint16{value},
WriteFuncCode: fcWriteSingleRegister,
ClientAddr: clientAddr,
UnitId: req.unitId,
Addr: addr,
Quantity: 1, // request for a single register
IsWrite: true, // request is a write
Args: []uint16{value},
})

if err != nil {
Expand Down Expand Up @@ -591,12 +593,13 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
// invoke the holding register handler
_, err = ms.handler.HandleHoldingRegisters(
&HoldingRegistersRequest{
ClientAddr: clientAddr,
UnitId: req.unitId,
Addr: addr,
Quantity: quantity,
IsWrite: true, // this is a write request
Args: bytesToUint16(binary.BigEndian, req.payload[5:]),
WriteFuncCode: fcWriteMultipleRegisters,
ClientAddr: clientAddr,
UnitId: req.unitId,
Addr: addr,
Quantity: quantity,
IsWrite: true, // this is a write request
Args: bytesToUint16(binary.BigEndian, req.payload[5:]),
})
if err != nil {
break
Expand Down

0 comments on commit 76f3ce6

Please sign in to comment.