From aca1423056df3cf50c244336e8fe5357067c8d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Esdstr=C3=B6m?= Date: Thu, 21 Jan 2021 22:47:02 +0100 Subject: [PATCH] Preliminary support for Device Identity modbus function --- api.go | 3 +++ client.go | 30 ++++++++++++++++++++++++++++++ modbus.go | 3 +++ 3 files changed, 36 insertions(+) diff --git a/api.go b/api.go index fe06ab0..936aa55 100644 --- a/api.go +++ b/api.go @@ -46,4 +46,7 @@ type Client interface { //ReadFIFOQueue reads the contents of a First-In-First-Out (FIFO) queue // of register in a remote device and returns FIFO value register. ReadFIFOQueue(address uint16) (results []byte, err error) + + // Device Identification + ReadIndividualDeviceIdentification(objectID uint8) (results []byte, err error) } diff --git a/client.go b/client.go index ac3ee2e..c0add02 100644 --- a/client.go +++ b/client.go @@ -432,6 +432,36 @@ func (mb *client) ReadFIFOQueue(address uint16) (results []byte, err error) { return } +// Request: +// Function code : 1 byte (0x2B) - Encapsulated Interface Transport, escape code for expanding into custom functionality +// MEI type : 1 byte (0x0E) - Either 0x0E for Modbus or 0x0D for CANopen +// Read device id code : 1 byte (0x04) - individual object access(4) OR stream basic (1), regular(2) or extended (3) +// Object id : 1 byte - first object ID 0x00-0x7F specified or reserved, 0x80-0xFF are product specific +// Response: - if function is supported, otherwise just a plain 'illegal function' is returned +// Function code : 1 byte (0x2B)(+ 0x80 if a malformated request was made) +// MEI type : 1 byte (0x0E) +// Read device id code : 1 byte (0x04) +// Conformity level : 1 byte (0x01-0x04)(+ 0x80 if individual access is supported) +// More follows : 1 byte (0x00) must be zero for individual access, 0xFF if more batches can be fetched +// Next object id : 1 byte (0x00) must be zero for individual access, start of next batch otherwise +// Number of objects : 1 byte (0x01) must be 0x01 for individual access, otherwise how many objects in current batch +// Repeated for each object: +// Object id : 1 byte - object ID 0x00-0x7F specified or reserved, 0x80-0xFF are product specific +// Object length : 1 byte - bytes of value +// Object value : Object - length byte(s) +func (mb *client) ReadIndividualDeviceIdentification(objectID uint8) (results []byte, err error) { + request := ProtocolDataUnit{ + FunctionCode: FuncCodeDeviceIdentification, + Data: []byte{0x0E, 0x04, objectID}, + } + response, err := mb.send(&request) + if err != nil { + return + } + results = response.Data[3:] // Strip of function code, MEI type and Read Device Id Code + return +} + // Helpers // send sends request and checks possible exception in the response. diff --git a/modbus.go b/modbus.go index 869473c..1be5bcd 100644 --- a/modbus.go +++ b/modbus.go @@ -26,6 +26,9 @@ const ( FuncCodeReadWriteMultipleRegisters = 23 FuncCodeMaskWriteRegister = 22 FuncCodeReadFIFOQueue = 24 + + // Device identification + FuncCodeDeviceIdentification = 43 ) const (