Skip to content

Commit

Permalink
unexport modbus opcodes and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
simonvetter committed Aug 6, 2020
1 parent 24c2d31 commit 83adf31
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 83 deletions.
24 changes: 12 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ func (mc *ModbusClient) WriteCoil(addr uint16, value bool) (err error) {

// create and fill in the request object
req = &pdu{
unitId: mc.unitId,
functionCode: FC_WRITE_SINGLE_COIL,
unitId: mc.unitId,
functionCode: fcWriteSingleCoil,
}

// coil address
Expand Down Expand Up @@ -495,8 +495,8 @@ func (mc *ModbusClient) WriteCoils(addr uint16, values []bool) (err error) {

// create and fill in the request object
req = &pdu{
unitId: mc.unitId,
functionCode: FC_WRITE_MULTIPLE_COILS,
unitId: mc.unitId,
functionCode: fcWriteMultipleCoils,
}

// start address
Expand Down Expand Up @@ -553,8 +553,8 @@ func (mc *ModbusClient) WriteRegister(addr uint16, value uint16) (err error) {

// create and fill in the request object
req = &pdu{
unitId: mc.unitId,
functionCode: FC_WRITE_SINGLE_REGISTER,
unitId: mc.unitId,
functionCode: fcWriteSingleRegister,
}

// register address
Expand Down Expand Up @@ -730,9 +730,9 @@ func (mc *ModbusClient) readBools(addr uint16, quantity uint16, di bool) (values
}

if di {
req.functionCode = FC_READ_DISCRETE_INPUTS
req.functionCode = fcReadDiscreteInputs
} else {
req.functionCode = FC_READ_COILS
req.functionCode = fcReadCoils
}

// start address
Expand Down Expand Up @@ -802,8 +802,8 @@ func (mc *ModbusClient) readRegisters(addr uint16, quantity uint16, regType RegT
}

switch regType {
case HOLDING_REGISTER: req.functionCode = FC_READ_HOLDING_REGISTERS
case INPUT_REGISTER: req.functionCode = FC_READ_INPUT_REGISTERS
case HOLDING_REGISTER: req.functionCode = fcReadHoldingRegisters
case INPUT_REGISTER: req.functionCode = fcReadInputRegisters
default:
err = ErrUnexpectedParameters
mc.logger.Errorf("unexpected register type (%v)", regType)
Expand Down Expand Up @@ -907,8 +907,8 @@ func (mc *ModbusClient) writeRegisters(addr uint16, values []byte) (err error) {

// create and fill in the request object
req = &pdu{
unitId: mc.unitId,
functionCode: FC_WRITE_MULTIPLE_REGISTERS,
unitId: mc.unitId,
functionCode: fcWriteMultipleRegisters,
}

// base address
Expand Down
86 changes: 42 additions & 44 deletions modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,36 @@ func (me Error) Error() (s string) {

const (
// coils
FC_READ_COILS uint8 = 0x01
FC_WRITE_SINGLE_COIL uint8 = 0x05
FC_WRITE_MULTIPLE_COILS uint8 = 0x0f
fcReadCoils uint8 = 0x01
fcWriteSingleCoil uint8 = 0x05
fcWriteMultipleCoils uint8 = 0x0f

// discrete inputs
FC_READ_DISCRETE_INPUTS uint8 = 0x02
fcReadDiscreteInputs uint8 = 0x02

// 16-bit input/holding registers
FC_READ_HOLDING_REGISTERS uint8 = 0x03
FC_READ_INPUT_REGISTERS uint8 = 0x04
FC_WRITE_SINGLE_REGISTER uint8 = 0x06
FC_WRITE_MULTIPLE_REGISTERS uint8 = 0x10
FC_MASK_WRITE_REGISTER uint8 = 0x16
FC_READ_WRITE_MULTILE_REGISTERS uint8 = 0x17
FC_READ_FIFO_QUEUE uint8 = 0x18
fcReadHoldingRegisters uint8 = 0x03
fcReadInputRegisters uint8 = 0x04
fcWriteSingleRegister uint8 = 0x06
fcWriteMultipleRegisters uint8 = 0x10
fcMaskWriteRegister uint8 = 0x16
fcReadWriteMultipleRegisters uint8 = 0x17
fcReadFifoQueue uint8 = 0x18

// file access
FC_READ_FILE_RECORD uint8 = 0x14
FC_WRITE_FILE_RECORD uint8 = 0x15
fcReadFileRecord uint8 = 0x14
fcWriteFileRecord uint8 = 0x15

// exception codes
EX_ILLEGAL_FUNCTION uint8 = 0x01
EX_ILLEGAL_DATA_ADDRESS uint8 = 0x02
EX_ILLEGAL_DATA_VALUE uint8 = 0x03
EX_SERVER_DEVICE_FAILURE uint8 = 0x04
EX_ACKNOWLEDGE uint8 = 0x05
EX_SERVER_DEVICE_BUSY uint8 = 0x06
EX_MEMORY_PARITY_ERROR uint8 = 0x08
EX_GW_PATH_UNAVAILABLE uint8 = 0x0a
EX_GW_TARGET_FAILED_TO_RESPOND uint8 = 0x0b
)
exIllegalFunction uint8 = 0x01
exIllegalDataAddress uint8 = 0x02
exIllegalDataValue uint8 = 0x03
exServerDeviceFailure uint8 = 0x04
exAcknowledge uint8 = 0x05
exServerDeviceBusy uint8 = 0x06
exMemoryParityError uint8 = 0x08
exGWPathUnavailable uint8 = 0x0a
exGWTargetFailedToRespond uint8 = 0x0b

// errors
ErrConfigurationError Error = "configuration error"
Expand All @@ -76,17 +75,17 @@ const (
// mapExceptionCodeToError turns a modbus exception code into a higher level Error object.
func mapExceptionCodeToError(exceptionCode uint8) (err error) {
switch exceptionCode {
case EX_ILLEGAL_FUNCTION: err = ErrIllegalFunction
case EX_ILLEGAL_DATA_ADDRESS: err = ErrIllegalDataAddress
case EX_ILLEGAL_DATA_VALUE: err = ErrIllegalDataValue
case EX_SERVER_DEVICE_FAILURE: err = ErrServerDeviceFailure
case EX_ACKNOWLEDGE: err = ErrAcknowledge
case EX_MEMORY_PARITY_ERROR: err = ErrMemoryParityError
case EX_SERVER_DEVICE_BUSY: err = ErrServerDeviceBusy
case EX_GW_PATH_UNAVAILABLE: err = ErrGWPathUnavailable
case EX_GW_TARGET_FAILED_TO_RESPOND: err = ErrGWTargetFailedToRespond
case exIllegalFunction: err = ErrIllegalFunction
case exIllegalDataAddress: err = ErrIllegalDataAddress
case exIllegalDataValue: err = ErrIllegalDataValue
case exServerDeviceFailure: err = ErrServerDeviceFailure
case exAcknowledge: err = ErrAcknowledge
case exMemoryParityError: err = ErrMemoryParityError
case exServerDeviceBusy: err = ErrServerDeviceBusy
case exGWPathUnavailable: err = ErrGWPathUnavailable
case exGWTargetFailedToRespond: err = ErrGWTargetFailedToRespond
default:
err = fmt.Errorf("unsupported exception code (%v)", exceptionCode)
err = fmt.Errorf("unknown exception code (%v)", exceptionCode)
}

return
Expand All @@ -95,18 +94,17 @@ func mapExceptionCodeToError(exceptionCode uint8) (err error) {
// mapErrorToExceptionCode turns an Error object into a modbus exception code.
func mapErrorToExceptionCode(err error) (exceptionCode uint8) {
switch err {
case ErrIllegalFunction: exceptionCode = EX_ILLEGAL_FUNCTION
case ErrIllegalDataAddress: exceptionCode = EX_ILLEGAL_DATA_ADDRESS
case ErrIllegalDataValue: exceptionCode = EX_ILLEGAL_DATA_VALUE
case ErrServerDeviceFailure: exceptionCode = EX_SERVER_DEVICE_FAILURE
case ErrAcknowledge: exceptionCode = EX_ACKNOWLEDGE
case ErrMemoryParityError: exceptionCode = EX_MEMORY_PARITY_ERROR
case ErrServerDeviceBusy: exceptionCode = EX_SERVER_DEVICE_BUSY
case ErrGWPathUnavailable: exceptionCode = EX_GW_PATH_UNAVAILABLE
case ErrGWTargetFailedToRespond:
exceptionCode = EX_GW_TARGET_FAILED_TO_RESPOND
case ErrIllegalFunction: exceptionCode = exIllegalFunction
case ErrIllegalDataAddress: exceptionCode = exIllegalDataAddress
case ErrIllegalDataValue: exceptionCode = exIllegalDataValue
case ErrServerDeviceFailure: exceptionCode = exServerDeviceFailure
case ErrAcknowledge: exceptionCode = exAcknowledge
case ErrMemoryParityError: exceptionCode = exMemoryParityError
case ErrServerDeviceBusy: exceptionCode = exServerDeviceBusy
case ErrGWPathUnavailable: exceptionCode = exGWPathUnavailable
case ErrGWTargetFailedToRespond: exceptionCode = exGWTargetFailedToRespond
default:
exceptionCode = EX_SERVER_DEVICE_FAILURE
exceptionCode = exServerDeviceFailure
}

return
Expand Down
36 changes: 18 additions & 18 deletions rtu_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,24 @@ func (rt *rtuTransport) assembleRTUFrame(p *pdu) (adu []byte) {
// Computes the expected length of a modbus RTU response.
func expectedResponseLenth(responseCode uint8, responseLength uint8) (byteCount int, err error) {
switch responseCode {
case FC_READ_HOLDING_REGISTERS,
FC_READ_INPUT_REGISTERS,
FC_READ_COILS,
FC_READ_DISCRETE_INPUTS: byteCount = int(responseLength)
case FC_WRITE_SINGLE_REGISTER,
FC_WRITE_MULTIPLE_REGISTERS,
FC_WRITE_SINGLE_COIL,
FC_WRITE_MULTIPLE_COILS: byteCount = 3
case FC_MASK_WRITE_REGISTER: byteCount = 5
case FC_READ_HOLDING_REGISTERS | 0x80,
FC_READ_INPUT_REGISTERS | 0x80,
FC_READ_COILS | 0x80,
FC_READ_DISCRETE_INPUTS | 0x80,
FC_WRITE_SINGLE_REGISTER | 0x80,
FC_WRITE_MULTIPLE_REGISTERS | 0x80,
FC_WRITE_SINGLE_COIL | 0x80,
FC_WRITE_MULTIPLE_COILS | 0x80,
FC_MASK_WRITE_REGISTER | 0x80: byteCount = 0
case fcReadHoldingRegisters,
fcReadInputRegisters,
fcReadCoils,
fcReadDiscreteInputs: byteCount = int(responseLength)
case fcWriteSingleRegister,
fcWriteMultipleRegisters,
fcWriteSingleCoil,
fcWriteMultipleCoils: byteCount = 3
case fcMaskWriteRegister: byteCount = 5
case fcReadHoldingRegisters | 0x80,
fcReadInputRegisters | 0x80,
fcReadCoils | 0x80,
fcReadDiscreteInputs | 0x80,
fcWriteSingleRegister | 0x80,
fcWriteMultipleRegisters | 0x80,
fcWriteSingleCoil | 0x80,
fcWriteMultipleCoils | 0x80,
fcMaskWriteRegister | 0x80: byteCount = 0
default: err = fmt.Errorf("unexpected response code (%v)", responseCode)
}

Expand Down
18 changes: 9 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
}

switch req.functionCode {
case FC_READ_COILS, FC_READ_DISCRETE_INPUTS:
case fcReadCoils, fcReadDiscreteInputs:
var coils []bool
var resCount int

Expand All @@ -332,7 +332,7 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
}

// invoke the appropriate handler
if req.functionCode == FC_READ_COILS {
if req.functionCode == fcReadCoils {
coils, err = ms.handler.HandleCoils(&CoilsRequest{
ClientAddr: clientAddr,
UnitId: req.unitId,
Expand Down Expand Up @@ -380,7 +380,7 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
// coil values
res.payload = append(res.payload, encodeBools(coils)...)

case FC_WRITE_SINGLE_COIL:
case fcWriteSingleCoil:
if len(req.payload) != 4 {
err = ErrProtocolError
break
Expand Down Expand Up @@ -422,7 +422,7 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
res.payload = append(res.payload,
req.payload[2], req.payload[3])

case FC_WRITE_MULTIPLE_COILS:
case fcWriteMultipleCoils:
var expectedLen int

if len(req.payload) < 6 {
Expand Down Expand Up @@ -488,7 +488,7 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
res.payload = append(res.payload,
uint16ToBytes(BIG_ENDIAN, quantity)...)

case FC_READ_HOLDING_REGISTERS, FC_READ_INPUT_REGISTERS:
case fcReadHoldingRegisters, fcReadInputRegisters:
var regs []uint16
var resCount int

Expand All @@ -513,7 +513,7 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
}

// invoke the appropriate handler
if req.functionCode == FC_READ_HOLDING_REGISTERS {
if req.functionCode == fcReadHoldingRegisters {
regs, err = ms.handler.HandleHoldingRegisters(
&HoldingRegistersRequest{
ClientAddr: clientAddr,
Expand Down Expand Up @@ -560,7 +560,7 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
res.payload = append(res.payload,
uint16sToBytes(BIG_ENDIAN, regs)...)

case FC_WRITE_SINGLE_REGISTER:
case fcWriteSingleRegister:
var value uint16

if len(req.payload) != 4 {
Expand Down Expand Up @@ -599,7 +599,7 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
res.payload = append(res.payload,
uint16ToBytes(BIG_ENDIAN, value)...)

case FC_WRITE_MULTIPLE_REGISTERS:
case fcWriteMultipleRegisters:
var expectedLen int

if len(req.payload) < 6 {
Expand Down Expand Up @@ -670,7 +670,7 @@ func (ms *ModbusServer) handleTransport(t transport, clientAddr string) {
functionCode: (0x80 | req.functionCode),
// set the exception code to illegal function to indicate that
// the server does not know how to handle this function code.
payload: []byte{EX_ILLEGAL_FUNCTION},
payload: []byte{exIllegalFunction},
}
}

Expand Down

0 comments on commit 83adf31

Please sign in to comment.