Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Device Identification #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ void modbusGet(void) {
modbusExchangeRegisters(holdingRegisters,0,4);
}
break;
case fcEncapsulatedInterfaceTransport:
{
modbusHandleDeviceId();
}

default: {
modbusSendException(ecIllegalFunction);
Expand Down
28 changes: 28 additions & 0 deletions yaMBSiavr.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,31 @@ uint8_t modbusExchangeBits(volatile uint8_t *ptrToInArray, uint16_t startAddress
return 0;
}
}

uint8_t modbusCopyMeiObj(volatile unsigned char* buff, uint8_t obj_id, const __flash char* obj_str, uint8_t obj_len)
{
obj_len--; // modbus ignores the null-termination
*buff++ = obj_id;
*buff++ = obj_len;
for(uint8_t i = 0; i < obj_len; i++)
{
*buff++ = *obj_str++;
}
return obj_len + 2;
}

void modbusHandleDeviceId()
{
rxbuffer[4] = 0x01; // conformity level
rxbuffer[5] = 0x00; // more follows: no
rxbuffer[6] = 0x00; // next object id: none
rxbuffer[7] = 0x03; // number of objects

uint8_t offset = 8;

offset += modbusCopyMeiObj(&rxbuffer[offset], 0, modbus_dev_vendor, sizeof(modbus_dev_vendor));
offset += modbusCopyMeiObj(&rxbuffer[offset], 1, modbus_dev_product, sizeof(modbus_dev_product));
offset += modbusCopyMeiObj(&rxbuffer[offset], 2, modbus_dev_version, sizeof(modbus_dev_version));

modbusSendMessage(offset - 1);
}
10 changes: 10 additions & 0 deletions yaMBSiavr.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ THE POSSIBILITY OF SUCH DAMAGE.

************************************************************************/
#include <avr/io.h>
#include <avr/pgmspace.h>
/**
* @code #include <yaMBSIavr.h> @endcode
*
Expand All @@ -55,6 +56,10 @@ THE POSSIBILITY OF SUCH DAMAGE.
#define BAUD 38400L
#endif

#define MODBUS_DEV_VENDOR "Your Company"
#define MODBUS_DEV_PRODUCT "Your Device Name"
#define MODBUS_DEV_VERSION "V0.1beta"

/*
* Definitions for transceiver enable pin.
*/
Expand Down Expand Up @@ -256,6 +261,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
#define fcForceMultipleCoils 15 //write multiple bits
#define fcPresetMultipleRegisters 16 //write multiple analog output registers (2 Bytes each)
#define fcReportSlaveID 17 //read device description, run status and other device specific information
#define fcEncapsulatedInterfaceTransport 43 //device identification

/**
* @brief Modbus Exception Codes
Expand Down Expand Up @@ -381,6 +387,8 @@ extern uint8_t modbusExchangeBits(volatile uint8_t *ptrToInArray, uint16_t start
*/
extern uint8_t modbusExchangeRegisters(volatile uint16_t *ptrToInArray, uint16_t startAddress, uint16_t size);

uint8_t modbusExchangeString(volatile char* str, uint8_t size);

/* @brief: returns 1 if data location adr is touched by current command
*
* Arguments: - adr: address of the data object
Expand All @@ -398,4 +406,6 @@ extern uint8_t modbusIsRangeInRange(uint16_t startAdr, uint16_t lastAdr);

extern volatile uint16_t modbusDataAmount;
extern volatile uint16_t modbusDataLocation;

void modbusHandleDeviceId();
#endif