diff --git a/plugins/modbus/modbus_req.c b/plugins/modbus/modbus_req.c index 7dad194d..152b02fe 100644 --- a/plugins/modbus/modbus_req.c +++ b/plugins/modbus/modbus_req.c @@ -482,8 +482,7 @@ int modbus_value_handle_test(neu_plugin_t *plugin, void *req, } switch (point->type) { - case NEU_TYPE_UINT16: - case NEU_TYPE_INT16: { + case NEU_TYPE_UINT16: { jtype = NEU_JSON_INT; uint16_t tmp_val16; memcpy(&tmp_val16, jvalue.val_bytes.bytes, sizeof(uint16_t)); @@ -491,6 +490,14 @@ int modbus_value_handle_test(neu_plugin_t *plugin, void *req, jvalue.val_int = ntohs(tmp_val16); break; } + case NEU_TYPE_INT16: { + jtype = NEU_JSON_INT; + int16_t tmp_val16; + memcpy(&tmp_val16, jvalue.val_bytes.bytes, sizeof(int16_t)); + free(jvalue.val_bytes.bytes); + jvalue.val_int = (int16_t) ntohs(tmp_val16); + break; + } case NEU_TYPE_FLOAT: { uint32_t tmp_valf; memcpy(&tmp_valf, jvalue.val_bytes.bytes, sizeof(uint32_t)); @@ -499,12 +506,19 @@ int modbus_value_handle_test(neu_plugin_t *plugin, void *req, jvalue.val_int = ntohl(tmp_valf); break; } - case NEU_TYPE_INT32: + case NEU_TYPE_INT32: { + jtype = NEU_JSON_INT; + int32_t tmp_val32; + memcpy(&tmp_val32, jvalue.val_bytes.bytes, sizeof(int32_t)); + free(jvalue.val_bytes.bytes); + jvalue.val_int = (int32_t) ntohl(tmp_val32); + break; + } case NEU_TYPE_UINT32: { + jtype = NEU_JSON_INT; uint32_t tmp_val32; memcpy(&tmp_val32, jvalue.val_bytes.bytes, sizeof(uint32_t)); free(jvalue.val_bytes.bytes); - jtype = NEU_JSON_INT; jvalue.val_int = ntohl(tmp_val32); break; } @@ -516,12 +530,19 @@ int modbus_value_handle_test(neu_plugin_t *plugin, void *req, jvalue.val_int = neu_ntohll(tmp_vald); break; } - case NEU_TYPE_INT64: + case NEU_TYPE_INT64: { + jtype = NEU_JSON_INT; + int64_t tmp_val64; + memcpy(&tmp_val64, jvalue.val_bytes.bytes, sizeof(int64_t)); + free(jvalue.val_bytes.bytes); + jvalue.val_int = (int64_t) neu_ntohll(tmp_val64); + break; + } case NEU_TYPE_UINT64: { + jtype = NEU_JSON_INT; uint64_t tmp_val64; memcpy(&tmp_val64, jvalue.val_bytes.bytes, sizeof(uint64_t)); free(jvalue.val_bytes.bytes); - jtype = NEU_JSON_INT; jvalue.val_int = neu_ntohll(tmp_val64); break; } diff --git a/tests/ft/driver/test_modbus.py b/tests/ft/driver/test_modbus.py index aaacfa27..0b30e701 100644 --- a/tests/ft/driver/test_modbus.py +++ b/tests/ft/driver/test_modbus.py @@ -744,6 +744,27 @@ class TestModbus: assert 0 == api.read_tag( node=param[0], group='group', tag=input_bit_5[0]['name']) + @description(given="created modbus node", when="test read tag", then="read tcp success and read rtu failed") + def test_modbus_test_read_tag(self, param): + body = { + "driver": param[0], + "group": "group", + "tag": "tag", + "address": "1!400001", + "attribute": config.NEU_TAG_ATTRIBUTE_RW, + "type": config.NEU_TYPE_INT16, + "precision": 0, + "decimal": 0, + "bias": 0.0 + } + if param[0] == 'modbus-tcp': + time.sleep(1) + response = api.test_read_tag(json=body) + assert response.json()["value"] == -123 + else: + response = api.test_read_tag(json=body) + assert response.json()["error"] == 3022 + @description(given="created modbus node and tags", when="read tags fuzz", then="should return correct result") def test_read_tags_fuzz(self, param): response = api.read_tags( diff --git a/tests/ft/neuron/api.py b/tests/ft/neuron/api.py index 137b2b20..70f7663e 100644 --- a/tests/ft/neuron/api.py +++ b/tests/ft/neuron/api.py @@ -163,6 +163,9 @@ def read_tags_paginate(node, group, sync=False, query=None): body["query"] = query return requests.post(url=config.BASE_URL + "/api/v2/read/paginate", headers={"Authorization": config.default_jwt}, json=body) +def test_read_tag(json): + return requests.post(url=config.BASE_URL + '/api/v2/read/test', headers={"Authorization": config.default_jwt}, json=json) + def read_tag(node, group, tag, sync=False): response = read_tags(node, group, sync, query={"name": tag}) assert 200 == response.status_code