forked from espressif/esp-modbus
feature modbus tcp master slave v2
This commit is contained in:
1 parent
d4345f8cd7
commit
2e2be467f6
109 files changed
+4454
-2319
No files matched your search
@@ -1,7 +1,7 @@
|
||||
dependencies:
|
||||
idf: ">=4.1"
|
||||
espressif/esp-modbus:
|
||||
version: "^2.0"
|
||||
version: "^2.0.0-beta"
|
||||
override_path: "../../../../"
|
||||
mb_example_common:
|
||||
path: "../../../mb_example_common"
|
||||
@@ -44,8 +44,13 @@
|
||||
#define TEST_INPUT_REG_START(field) (INPUT_OFFSET(field) >> 1)
|
||||
#define TEST_INPUT_REG_SIZE(field) (sizeof(((input_reg_params_t *)0)->field) >> 1)
|
||||
|
||||
#define TEST_VALUE 12345 // default test value
|
||||
#define TEST_ASCII_BIN 0xAAAAAAAA
|
||||
#define TEST_VALUE (12345) // default test value
|
||||
#define TEST_ASCII_BIN (0xAAAAAAAA)
|
||||
#define TEST_ARR_REG_SZ (58)
|
||||
#define TEST_HUMI_MIN (-40)
|
||||
#define TEST_HUMI_MAX (50)
|
||||
#define TEST_TEMP_MIN (0)
|
||||
#define TEST_TEMP_MAX (100)
|
||||
|
||||
// Options can be used as bit masks or parameter limits
|
||||
#define OPTS(min_val, max_val, step_val) { .opt1 = min_val, .opt2 = max_val, .opt3 = step_val }
|
||||
@@ -107,31 +112,31 @@ const mb_parameter_descriptor_t device_parameters[] = {
|
||||
{ CID_INP_DATA_0, STR("Data_channel_0"), STR("Volts"), MB_DEVICE_ADDR1, MB_PARAM_INPUT,
|
||||
TEST_INPUT_REG_START(input_data0), TEST_INPUT_REG_SIZE(input_data0),
|
||||
INPUT_OFFSET(input_data0), PARAM_TYPE_FLOAT, 4,
|
||||
OPTS( 0, 100, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
OPTS( TEST_TEMP_MIN, TEST_TEMP_MAX, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
{ CID_HOLD_DATA_0, STR("Humidity_1"), STR("%rH"), MB_DEVICE_ADDR1, MB_PARAM_HOLDING,
|
||||
TEST_HOLD_REG_START(holding_data0), TEST_HOLD_REG_SIZE(holding_data0),
|
||||
HOLD_OFFSET(holding_data0), PARAM_TYPE_FLOAT, 4,
|
||||
OPTS( 0, 100, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
OPTS( TEST_HUMI_MIN, TEST_HUMI_MAX, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
{ CID_INP_DATA_1, STR("Temperature_1"), STR("C"), MB_DEVICE_ADDR1, MB_PARAM_INPUT,
|
||||
TEST_INPUT_REG_START(input_data1), TEST_INPUT_REG_SIZE(input_data1),
|
||||
INPUT_OFFSET(input_data1), PARAM_TYPE_FLOAT, 4,
|
||||
OPTS( -40, 100, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
OPTS( TEST_TEMP_MIN, TEST_TEMP_MAX, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
{ CID_HOLD_DATA_1, STR("Humidity_2"), STR("%rH"), MB_DEVICE_ADDR1, MB_PARAM_HOLDING,
|
||||
TEST_HOLD_REG_START(holding_data1), TEST_HOLD_REG_SIZE(holding_data1),
|
||||
HOLD_OFFSET(holding_data1), PARAM_TYPE_FLOAT, 4,
|
||||
OPTS( 0, 100, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
OPTS( TEST_HUMI_MIN, TEST_HUMI_MAX, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
{ CID_INP_DATA_2, STR("Temperature_2"), STR("C"), MB_DEVICE_ADDR1, MB_PARAM_INPUT,
|
||||
TEST_INPUT_REG_START(input_data2), TEST_INPUT_REG_SIZE(input_data2),
|
||||
INPUT_OFFSET(input_data2), PARAM_TYPE_FLOAT, 4,
|
||||
OPTS( -40, 100, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
OPTS( TEST_TEMP_MIN, TEST_TEMP_MAX, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
{ CID_HOLD_DATA_2, STR("Humidity_3"), STR("%rH"), MB_DEVICE_ADDR1, MB_PARAM_HOLDING,
|
||||
TEST_HOLD_REG_START(holding_data2), TEST_HOLD_REG_SIZE(holding_data2),
|
||||
HOLD_OFFSET(holding_data2), PARAM_TYPE_FLOAT, 4,
|
||||
OPTS( 0, 100, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
OPTS( TEST_HUMI_MIN, TEST_HUMI_MAX, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
{ CID_HOLD_TEST_REG, STR("Test_regs"), STR("__"), MB_DEVICE_ADDR1, MB_PARAM_HOLDING,
|
||||
TEST_HOLD_REG_START(test_regs), 58,
|
||||
HOLD_OFFSET(test_regs), PARAM_TYPE_ASCII, 116,
|
||||
OPTS( 0, 100, TEST_ASCII_BIN ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
TEST_HOLD_REG_START(test_regs), TEST_ARR_REG_SZ,
|
||||
HOLD_OFFSET(test_regs), PARAM_TYPE_ASCII, (TEST_ARR_REG_SZ * 2),
|
||||
OPTS( TEST_TEMP_MIN, TEST_TEMP_MAX, TEST_ASCII_BIN ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
{ CID_RELAY_P1, STR("RelayP1"), STR("on/off"), MB_DEVICE_ADDR1, MB_PARAM_COIL, 2, 6,
|
||||
COIL_OFFSET(coils_port0), PARAM_TYPE_U8, 1,
|
||||
OPTS( 0xAA, 0x15, 0 ), PAR_PERMS_READ_WRITE_TRIGGER },
|
||||
@@ -427,10 +432,9 @@ static void master_operation_func(void *arg)
|
||||
}
|
||||
|
||||
if (alarm_state) {
|
||||
ESP_LOGI(TAG, "Alarm triggered by cid #%d.",
|
||||
(int)param_descriptor->cid);
|
||||
ESP_LOGI(TAG, "Alarm triggered by cid #%u.", param_descriptor->cid);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Alarm is not triggered after %d retries.",
|
||||
ESP_LOGE(TAG, "Alarm is not triggered after %u retries.",
|
||||
MASTER_MAX_RETRY);
|
||||
}
|
||||
ESP_LOGI(TAG, "Destroy master...");
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
# Modbus Slave Example
|
||||
|
||||
This example demonstrates using the port of the FreeModbus stack on an ESP32 target where the ESP32 target is operating as a network slave. The example allows an external Modbus host to read/write device parameters on the ESP32 target using the Modbus protocol. The parameters accessible through Modbus are located in `mb_example_common/modbus_params.h\c` source/header files that users can update to add/remove their own custom parameters.
|
||||
This example demonstrates using the port of the esp-modbus stack on an ESP32 target where the ESP32 target is operating as a network slave. The example allows an external Modbus host to read/write device parameters on the ESP32 target using the Modbus protocol. The parameters accessible through Modbus are located in `mb_example_common/modbus_params.h\c` source/header files that users can update to add/remove their own custom parameters.
|
||||
These are represented in structures `holding_reg_params`, `input_reg_params`, `coil_reg_params`, `discrete_reg_params` for holding registers, input parameters, coils and discrete inputs accordingly. The app_main application demonstrates how to setup Modbus stack and use notifications about parameters change from host system.
|
||||
The FreeModbus stack located in `components/freemodbus` folder and contains the `/port` folder where the stack's port to the ESP32 is situated. There are some parameters of the port that can be configured in KConfig file to start stack correctly (See description below for more information).
|
||||
The FreeModbus stack located in `modbus` folder and supports the ESP32 targets. There are some parameters of the port that can be configured in KConfig file to start stack correctly (See description below for more information).
|
||||
|
||||
The slave example uses shared parameter structures defined in `examples/protocols/modbus/mb_example_common` folder.
|
||||
|
||||
@@ -59,7 +59,7 @@ Note: Each target chip has different GPIO pins available for UART connection. Pl
|
||||
|
||||
Define the ```Modbus communiction mode``` for slave in Kconfig - CONFIG_MB_COMM_MODE (must be the same for master and slave application).
|
||||
Set ```Modbus slave address``` for the example application (by default for example script is set to 1).
|
||||
The communication parameters of freemodbus stack (Component config->Modbus configuration) allow to configure it appropriately but usually it is enough to use default settings.
|
||||
The communication parameters of esp-modbus stack (Component config->Modbus configuration) allow to configure it appropriately but usually it is enough to use default settings.
|
||||
See the help strings of parameters for more information.
|
||||
|
||||
### Setup external Modbus master software
|
||||
|
||||
@@ -98,7 +98,7 @@ menu "Modbus Example Configuration"
|
||||
|
||||
config MB_SLAVE_ADDR
|
||||
int "Modbus slave address"
|
||||
range 1 127
|
||||
range 1 247
|
||||
default 1
|
||||
help
|
||||
This is the Modbus slave address in the network.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
dependencies:
|
||||
idf: ">=4.1"
|
||||
espressif/esp-modbus:
|
||||
version: "^2.0"
|
||||
version: "^2.0.0-beta"
|
||||
override_path: "../../../../"
|
||||
mb_example_common:
|
||||
path: "../../../mb_example_common"
|
||||
@@ -35,7 +35,7 @@
|
||||
|
||||
#define MB_PAR_INFO_GET_TOUT (10) // Timeout for get parameter info
|
||||
#define MB_CHAN_DATA_MAX_VAL (6)
|
||||
#define MB_CHAN_DATA_OFFSET (0.2f)
|
||||
#define MB_CHAN_DATA_OFFSET (1.2f)
|
||||
#define MB_READ_MASK (MB_EVENT_INPUT_REG_RD \
|
||||
| MB_EVENT_HOLDING_REG_RD \
|
||||
| MB_EVENT_DISCRETE_RD \
|
||||
@@ -124,7 +124,7 @@ static void setup_reg_data(void)
|
||||
input_reg_params.input_data7 = 4.78;
|
||||
}
|
||||
|
||||
// An example application of Modbus slave. It is based on freemodbus stack.
|
||||
// An example application of Modbus slave. It is based on esp-modbus stack.
|
||||
// See deviceparams.h file for more information about assigned Modbus parameters.
|
||||
// These parameters can be accessed from main application and also can be changed
|
||||
// by external Modbus master host.
|
||||
|
||||
@@ -63,4 +63,9 @@ def test_modbus_serial_communication(config: str, dut: Tuple[ModbusTestDut, Modb
|
||||
dut_master.dut_test_start(dictionary=pattern_dict_master)
|
||||
|
||||
dut_slave.dut_check_errors()
|
||||
dut_master.dut_check_errors()
|
||||
dut_master.dut_check_errors()
|
||||
|
||||
@pytest.mark.multi_dut_modbus_generic
|
||||
@pytest.mark.parametrize('config', ['dummy_config'])
|
||||
def test_modbus_serial_generic() -> None:
|
||||
print('The generic serial example tests are not provided yet.')
|
||||
Reference in new issue
Block a user