Modbus support UDP

This commit is contained in:
hxy7yx
2023-05-09 10:42:57 +08:00
parent 3f2ecd86fc
commit ac6d5286c3
5 changed files with 137 additions and 59 deletions
+30 -1
View File
@@ -57,7 +57,7 @@
"value": 0
},
{
"key": "TCP",
"key": "Ethernet",
"value": 1
}
]
@@ -301,6 +301,31 @@
"value": 1
}
},
"transport_mode": {
"name": "Transport Mode",
"name_zh": "传输模式",
"description": "Neuron as the client, or as the server",
"description_zh": "TCP 传输或 UDP 传输",
"attribute": "required",
"type": "map",
"default": 0,
"valid": {
"map": [
{
"key": "TCP",
"value": 0
},
{
"key": "UDP",
"value": 1
}
]
},
"condition": {
"field": "link",
"value": 1
}
},
"connection_mode": {
"name": "Connection Mode",
"name_zh": "连接模式",
@@ -324,6 +349,10 @@
"condition": {
"field": "link",
"value": 1
},
"condition": {
"field": "transport_mode",
"value": 0
}
}
}
+25
View File
@@ -42,6 +42,27 @@
}
],
"group_interval": 1000,
"transport_mode": {
"name": "Transport Mode",
"name_zh": "传输模式",
"description": "Neuron as the client, or as the server",
"description_zh": "TCP 传输或 UDP 传输",
"attribute": "required",
"type": "map",
"default": 0,
"valid": {
"map": [
{
"key": "TCP",
"value": 0
},
{
"key": "UDP",
"value": 1
}
]
}
},
"connection_mode": {
"name": "Connection Mode",
"name_zh": "连接模式",
@@ -61,6 +82,10 @@
"value": 1
}
]
},
"condition": {
"field": "transport_mode",
"value": 0
}
},
"interval": {
+3 -4
View File
@@ -97,8 +97,8 @@ static int driver_init(neu_plugin_t *plugin)
plugin->protocol = MODBUS_PROTOCOL_TCP;
plugin->events = neu_event_new();
plugin->stack = modbus_stack_create((void *) plugin, MODBUS_PROTOCOL_TCP,
modbus_send_msg, modbus_value_handle,
modbus_write_resp);
modbus_send_msg, modbus_value_handle,
modbus_write_resp);
return 0;
}
@@ -135,7 +135,7 @@ static int driver_stop(neu_plugin_t *plugin)
static int driver_config(neu_plugin_t *plugin, const char *config)
{
int ret = 0;
char * err_param = NULL;
char *err_param = NULL;
neu_json_elem_t port = { .name = "port", .t = NEU_JSON_INT };
neu_json_elem_t timeout = { .name = "timeout", .t = NEU_JSON_INT };
neu_json_elem_t host = { .name = "host", .t = NEU_JSON_STR };
@@ -168,7 +168,6 @@ static int driver_config(neu_plugin_t *plugin, const char *config)
param.params.tcp_client.timeout = timeout.v.val_int;
plugin->is_server = false;
}
plog_info(plugin, "config: host: %s, port: %" PRId64 ", mode: %" PRId64 "",
host.v.val_str, port.v.val_int, mode.v.val_int);
+42 -31
View File
@@ -63,13 +63,13 @@ const neu_plugin_module_t neu_plugin_module = {
.module_name = "Modbus RTU",
.module_descr =
"This plugin is used to connect devices using modbus rtu protocol. "
"Support serial and TCP communication methods. When using TCP "
"Support serial,TCP and UDP communication methods. When using TCP "
"communication, it is necessary to use the pass-through function of "
"DTU to convert serial data into IP data and transmit it through "
"the network. Selecting client/server is also supported.",
.module_descr_zh =
"该插件用于连接使用 modbus rtu 协议的设备。支持串口和 TCP "
"两种通信方式。使用 TCP 通信时,需要使用 DTU "
"该插件用于连接使用 modbus rtu 协议的设备。支持串口,TCP 和 UDP "
"三种通信方式。使用 TCP 通信时,需要使用 DTU "
"的透传功能,将串口数据转为 IP "
"数据并通过网络传输。也支持选择客户端/服务端。",
.intf_funs = &plugin_intf_funs,
@@ -100,8 +100,8 @@ static int driver_init(neu_plugin_t *plugin)
plugin->protocol = MODBUS_PROTOCOL_RTU;
plugin->events = neu_event_new();
plugin->stack = modbus_stack_create((void *) plugin, MODBUS_PROTOCOL_RTU,
modbus_send_msg, modbus_value_handle,
modbus_write_resp);
modbus_send_msg, modbus_value_handle,
modbus_write_resp);
return 0;
}
@@ -138,16 +138,17 @@ static int driver_stop(neu_plugin_t *plugin)
static int driver_config(neu_plugin_t *plugin, const char *config)
{
int ret = 0;
char * err_param = NULL;
char *err_param = NULL;
neu_conn_param_t param = { 0 };
neu_json_elem_t link = { .name = "link", .t = NEU_JSON_INT };
neu_json_elem_t timeout = { .name = "timeout", .t = NEU_JSON_INT };
neu_json_elem_t interval = { .name = "interval", .t = NEU_JSON_INT };
neu_json_elem_t mode = { .name = "connection_mode", .t = NEU_JSON_INT };
neu_json_elem_t host = { .name = "host", .t = NEU_JSON_STR };
neu_json_elem_t port = { .name = "port", .t = NEU_JSON_INT };
neu_json_elem_t mode = { .name = "connection_mode", .t = NEU_JSON_INT };
neu_json_elem_t tmode = { .name = "transport_mode", .t = NEU_JSON_INT };
neu_json_elem_t host = { .name = "host", .t = NEU_JSON_STR };
neu_json_elem_t port = { .name = "port", .t = NEU_JSON_INT };
neu_json_elem_t device = { .name = "device", .t = NEU_JSON_STR };
neu_json_elem_t stop = { .name = "stop", .t = NEU_JSON_INT };
@@ -197,23 +198,33 @@ static int driver_config(neu_plugin_t *plugin, const char *config)
device.v.val_str, baud.v.val_int, data.v.val_int,
parity.v.val_int, stop.v.val_int);
} else {
if (mode.v.val_int == 1) {
param.type = NEU_CONN_TCP_SERVER;
param.params.tcp_server.ip = host.v.val_str;
param.params.tcp_server.port = port.v.val_int;
param.params.tcp_server.start_listen = modbus_tcp_server_listen;
param.params.tcp_server.stop_listen = modbus_tcp_server_stop;
param.params.tcp_server.timeout = timeout.v.val_int;
param.params.tcp_server.max_link = 1;
plugin->is_server = true;
if (tmode.v.val_int == 1) {
param.type = NEU_CONN_UDP;
param.params.udp.timeout = 0;
param.params.udp.src_ip = "0.0.0.0";
param.params.udp.src_port = 502;
param.params.udp.dst_ip = host.v.val_str;
param.params.udp.dst_port = port.v.val_int;
plugin->is_server = false;
} else {
param.type = NEU_CONN_TCP_CLIENT;
param.params.tcp_client.ip = host.v.val_str;
param.params.tcp_client.port = port.v.val_int;
param.params.tcp_client.timeout = timeout.v.val_int;
plugin->is_server = false;
if (mode.v.val_int == 1) {
param.type = NEU_CONN_TCP_SERVER;
param.params.tcp_server.ip = host.v.val_str;
param.params.tcp_server.port = port.v.val_int;
param.params.tcp_server.start_listen = modbus_tcp_server_listen;
param.params.tcp_server.stop_listen = modbus_tcp_server_stop;
param.params.tcp_server.timeout = timeout.v.val_int;
param.params.tcp_server.max_link = 1;
plugin->is_server = true;
}
if (mode.v.val_int == 0) {
param.type = NEU_CONN_TCP_CLIENT;
param.params.tcp_client.ip = host.v.val_str;
param.params.tcp_client.port = port.v.val_int;
param.params.tcp_client.timeout = timeout.v.val_int;
plugin->is_server = false;
}
}
plog_info(plugin,
"config: host: %s, port: %" PRId64 ", mode: %" PRId64 "",
host.v.val_str, port.v.val_int, mode.v.val_int);
@@ -252,13 +263,13 @@ static int driver_validate_tag(neu_plugin_t *plugin, neu_datatag_t *tag)
int ret = modbus_tag_to_point(tag, &point);
if (ret == 0) {
plog_debug(
plugin,
"validate tag success, name: %s, address: %s, type: %d, slave id: "
"%d, start address: %d, n register: %d, area: %s",
tag->name, tag->address, tag->type, point.slave_id,
point.start_address, point.n_register,
modbus_area_to_str(point.area));
plog_debug(plugin,
"validate tag success, name: %s, address: %s, type: %d, "
"slave id: "
"%d, start address: %d, n register: %d, area: %s",
tag->name, tag->address, tag->type, point.slave_id,
point.start_address, point.n_register,
modbus_area_to_str(point.area));
}
return ret;
+37 -23
View File
@@ -62,10 +62,12 @@ const neu_plugin_module_t neu_plugin_module = {
.schema = "modbus-tcp",
.module_name = "Modbus TCP",
.module_descr =
"The modbus tcp plugin is an enhanced version of the modbus tcp "
"plugin, users can choose to connect as a client or a server.",
.module_descr_zh = "该插件是 modbus tcp "
"插件的加强版,用户可选择作为客户端连接,还是服务端连接",
"This plugin is used to connect devices using the modbus TCP protocol "
"Users can choose to connect as a client or a server. Support TCP and "
"UDP communication methods.",
.module_descr_zh =
"该插件用于连接使用 modbus tcp 协议的设备。"
"用户可选择作为客户端连接,或是服务端连接,支持 TCP 和 UDP 通信方式。",
.intf_funs = &plugin_intf_funs,
.kind = NEU_PLUGIN_KIND_SYSTEM,
.type = NEU_NA_TYPE_DRIVER,
@@ -94,8 +96,8 @@ static int driver_init(neu_plugin_t *plugin)
plugin->protocol = MODBUS_PROTOCOL_TCP;
plugin->events = neu_event_new();
plugin->stack = modbus_stack_create((void *) plugin, MODBUS_PROTOCOL_TCP,
modbus_send_msg, modbus_value_handle,
modbus_write_resp);
modbus_send_msg, modbus_value_handle,
modbus_write_resp);
return 0;
}
@@ -132,12 +134,13 @@ static int driver_stop(neu_plugin_t *plugin)
static int driver_config(neu_plugin_t *plugin, const char *config)
{
int ret = 0;
char * err_param = NULL;
char *err_param = NULL;
neu_json_elem_t port = { .name = "port", .t = NEU_JSON_INT };
neu_json_elem_t timeout = { .name = "timeout", .t = NEU_JSON_INT };
neu_json_elem_t host = { .name = "host", .t = NEU_JSON_STR };
neu_json_elem_t interval = { .name = "interval", .t = NEU_JSON_INT };
neu_json_elem_t mode = { .name = "connection_mode", .t = NEU_JSON_INT };
neu_json_elem_t tmode = { .name = "transport_mode", .t = NEU_JSON_INT };
neu_conn_param_t param = { 0 };
ret = neu_parse_param((char *) config, &err_param, 5, &port, &host, &mode,
@@ -152,23 +155,34 @@ static int driver_config(neu_plugin_t *plugin, const char *config)
param.log = plugin->common.log;
plugin->interval = interval.v.val_int;
if (mode.v.val_int == 1) {
param.type = NEU_CONN_TCP_SERVER;
param.params.tcp_server.ip = host.v.val_str;
param.params.tcp_server.port = port.v.val_int;
param.params.tcp_server.start_listen = modbus_tcp_server_listen;
param.params.tcp_server.stop_listen = modbus_tcp_server_stop;
param.params.tcp_server.timeout = timeout.v.val_int;
param.params.tcp_server.max_link = 1;
plugin->is_server = true;
} else {
param.type = NEU_CONN_TCP_CLIENT;
param.params.tcp_client.ip = host.v.val_str;
param.params.tcp_client.port = port.v.val_int;
param.params.tcp_client.timeout = timeout.v.val_int;
plugin->is_server = false;
}
if (tmode.v.val_int == 1) {
param.type = NEU_CONN_UDP;
param.params.udp.timeout = 0;
param.params.udp.src_ip = "0.0.0.0";
param.params.udp.src_port = 502;
param.params.udp.dst_ip = host.v.val_str;
param.params.udp.dst_port = port.v.val_int;
plugin->is_server = false;
} else {
if (mode.v.val_int == 1) {
param.type = NEU_CONN_TCP_SERVER;
param.params.tcp_server.ip = host.v.val_str;
param.params.tcp_server.port = port.v.val_int;
param.params.tcp_server.start_listen = modbus_tcp_server_listen;
param.params.tcp_server.stop_listen = modbus_tcp_server_stop;
param.params.tcp_server.timeout = timeout.v.val_int;
param.params.tcp_server.max_link = 1;
plugin->is_server = true;
}
if (mode.v.val_int == 0) {
param.type = NEU_CONN_TCP_CLIENT;
param.params.tcp_client.ip = host.v.val_str;
param.params.tcp_client.port = port.v.val_int;
param.params.tcp_client.timeout = timeout.v.val_int;
plugin->is_server = false;
}
}
plog_info(plugin, "config: host: %s, port: %" PRId64 ", mode: %" PRId64 "",
host.v.val_str, port.v.val_int, mode.v.val_int);