device_params.c 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*======================================================================================
  2. * Description:
  3. * This C file contains user defined parameters for Modbus example and data dictionary
  4. * which describes each value (characteristic) and links it to modbus registers in
  5. * in corresponded slave device.
  6. *=====================================================================================*/
  7. #include "mbcontroller.h"
  8. #include "device_params.h"
  9. // Here are the user instances defined as structures for device parameters packed by 1 byte
  10. // These are keep the values that can be accessed from Modbus master
  11. holding_reg_params_t holding_reg_params = { 0 };
  12. input_reg_params_t input_reg_params = { 0 };
  13. coil_reg_params_t coil_reg_params = { 0 };
  14. discrete_reg_params_t discrete_reg_params = { 0 };
  15. #define HOLD_OFFSET(field) ((uint16_t)(offsetof(holding_reg_params_t, field) + 1))
  16. #define INPUT_OFFSET(field) ((uint16_t)(offsetof(input_reg_params_t, field) + 1))
  17. #define COIL_OFFSET(field) ((uint16_t)(offsetof(coil_reg_params_t, field) + 1))
  18. // Discrete offset macro (options can be used as bit masks)
  19. #define DISCR_OFFSET(field) ((uint16_t)(offsetof(discrete_reg_params_t, field) + 1))
  20. #define STR(fieldname) ((const char*)( fieldname ))
  21. #define OPTS(min_val, max_val, step_val) { .opt1 = min_val, .opt2 = max_val, .opt3 = step_val }
  22. // This table below defines the characteristics supported by this Modbus master device (Data dictionary).
  23. // These characteristics are linked to Modbus parameters of external slave devices in Modbus network.
  24. // For this example next devices are supported:
  25. // MB_DEVICE_ADDR1 : (CID_HUMIDITY_1, CID_TEMPERATURE_1) : Modbus sensor 1 (Humidity and Temperature)
  26. // MB_DEVICE_ADDR2 : (CID_HUMIDITY_2, CID_TEMPERATURE_2) : Modbus sensor 2 (Humidity and Temperature)
  27. // MB_DEVICE_ADDR3 : (CID_RELAY_P1, CID_RELAY_P2) : Modbus output device (Relay outputs on/off)
  28. // There are two options to define instance for parameter:
  29. // 1. Define offset (param_offset field) to the field in the parameter's structure (see example below)
  30. // Once set it will be used as to store parameter value.
  31. // 2. Set param_offset field in characteristics table to zero.
  32. // This will allow to allocate space for parameter storage during initialization and use it as a cache.
  33. // Example Data (Object) Dictionary for Modbus parameters
  34. const mb_parameter_descriptor_t device_parameters[] = {
  35. // { Cid, Param Name, Units, Modbus Slave Addr, Modbus Reg Type, Reg Start, Reg Size, Instance Offset, Data Type, Data Size, Parameter Options, Access Mode}
  36. // Parameter: Data channel 0 : Data channel 0 = Voltage
  37. { CID_DATA_CHAN_0, STR("Data_channel_0"), STR("Volts"), MB_DEVICE_ADDR1, MB_PARAM_INPUT, 0, 2,
  38. INPUT_OFFSET(data_chan0), PARAM_TYPE_FLOAT, 4, OPTS( -10, 10, 1 ), PAR_PERMS_READ_WRITE_TRIGGER },
  39. { CID_HUMIDITY_1, STR("Humidity_1"), STR("%rH"), MB_DEVICE_ADDR1, MB_PARAM_HOLDING, 0, 2,
  40. HOLD_OFFSET(mb_device1_humidity), PARAM_TYPE_FLOAT, 4, OPTS( 0, 100, 1 ), PAR_PERMS_READ_WRITE_TRIGGER },
  41. // Parameter: Temperature_2 : Temperature from device slave address = 1
  42. { CID_TEMPERATURE_1, STR("Temperature_1"), STR("°C"), MB_DEVICE_ADDR1, MB_PARAM_HOLDING, 2, 2,
  43. HOLD_OFFSET(mb_device1_temperature), PARAM_TYPE_FLOAT, 4, OPTS( -40, 80, 1 ), PAR_PERMS_READ_WRITE_TRIGGER },
  44. // Parameter: Humidity_2 : Humidity from device slave address = 2
  45. { CID_HUMIDITY_2, STR("Humidity_2"), STR("%rH"), MB_DEVICE_ADDR2, MB_PARAM_HOLDING, 0, 2,
  46. HOLD_OFFSET(mb_device2_humidity), PARAM_TYPE_FLOAT, 4, OPTS( 0, 100, 1 ), PAR_PERMS_READ_WRITE_TRIGGER },
  47. // Parameter: Temperature_2 : Temperature from device slave address = 2
  48. { CID_TEMPERATURE_2, STR("Temperature_2"), STR("°C"), MB_DEVICE_ADDR2, MB_PARAM_HOLDING, 2, 2,
  49. HOLD_OFFSET(mb_device2_temperature), PARAM_TYPE_FLOAT, 4, OPTS( -40, 80, 1 ), PAR_PERMS_READ_WRITE_TRIGGER },
  50. // Parameter: Relay P1 : Alarm on/off channel 1 : Output device 1
  51. { CID_RELAY_P1, STR("RelayP1"), STR("on/off"), MB_DEVICE_ADDR3, MB_PARAM_COIL, 0, 3,
  52. COIL_OFFSET(coils_port1), PARAM_TYPE_U16, 2, OPTS( BIT0 | BIT1 | BIT2, 0, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
  53. // Parameter: Relay P2 : Alarm on/off channel 2 : Output device 1
  54. { CID_RELAY_P2, STR("RelayP2"), STR("on/off"), MB_DEVICE_ADDR3, MB_PARAM_COIL, 3, 8,
  55. COIL_OFFSET(coils_port2), PARAM_TYPE_U16, 2, OPTS( BIT3, 0, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
  56. };
  57. // Calculate number of parameters in the table
  58. const uint16_t num_device_parameters = (sizeof(device_parameters)/sizeof(device_parameters[0]));