Skip to content

Commit 72cab4a

Browse files
authored
Merge pull request #2491 from hxy7yx/v2.9-1
[v2.9]feat(modbus):support endianess of 8-Byte data
2 parents 3d81d07 + c21df74 commit 72cab4a

15 files changed

Lines changed: 249 additions & 37 deletions

File tree

‎.github/workflows/check.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99

1010
jobs:
1111
clang_format_check:
12-
runs-on: ubuntu-20.04
12+
runs-on: ubuntu-24.04
1313

1414
steps:
1515
# checkout

‎.github/workflows/ci.yml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ env:
1414

1515
jobs:
1616
cross-build:
17-
runs-on: ubuntu-20.04
17+
runs-on: ubuntu-24.04
1818
strategy:
1919
matrix:
2020
arch:
@@ -52,14 +52,14 @@ jobs:
5252
run: |
5353
./package-sdk.sh -p ${{ matrix.arch[2] }} -n neuron-sdk-$VERSION
5454
55-
- uses: actions/upload-artifact@v3
55+
- uses: actions/upload-artifact@v4
5656
with:
57-
name: neuron-sdk
57+
name: neuron-sdk-${{ matrix.arch[2] }}
5858
path: |
5959
neuron-sdk-*.tar.gz
6060
6161
build-debug-sdk:
62-
runs-on: ubuntu-20.04
62+
runs-on: ubuntu-24.04
6363
strategy:
6464
matrix:
6565
arch:
@@ -83,7 +83,7 @@ jobs:
8383
run: |
8484
./package-sdk.sh -p ${{ matrix.arch[2] }} -n neuron-sdk-debug-$VERSION
8585
86-
- uses: actions/upload-artifact@v3
86+
- uses: actions/upload-artifact@v4
8787
with:
8888
name: neuron-sdk
8989
path: |
@@ -102,7 +102,7 @@ jobs:
102102
env:
103103
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
104104

105-
- uses: actions/download-artifact@v3
105+
- uses: actions/download-artifact@v4
106106
with:
107107
name: neuron-sdk
108108

‎include/neuron/tag.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ typedef enum {
4444
NEU_DATATAG_ENDIAN_BL32 = 5, // #BL 2,1,4,3
4545
NEU_DATATAG_ENDIAN_L64 = 6, // #L 8,7,6,5,4,3,2,1
4646
NEU_DATATAG_ENDIAN_B64 = 7, // #B 1,2,3,4,5,6,7,8
47+
NEU_DATATAG_ENDIAN_LL64 = 8,
48+
NEU_DATATAG_ENDIAN_LB64 = 9,
49+
NEU_DATATAG_ENDIAN_BB64 = 10,
50+
NEU_DATATAG_ENDIAN_BL64 = 11,
4751
} neu_datatag_endian_e;
4852

4953
typedef enum {
@@ -63,6 +67,7 @@ typedef union {
6367
} value32;
6468
struct {
6569
neu_datatag_endian_e endian;
70+
bool is_default;
6671
} value64;
6772
struct {
6873
uint16_t length;

‎include/neuron/type.h‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,42 @@ static inline uint64_t neu_htonll(uint64_t u)
738738
return ret;
739739
}
740740

741+
static inline uint64_t neu_htonlb(uint64_t u)
742+
{
743+
uint8_t *in = (uint8_t *) &u;
744+
uint64_t out = 0;
745+
uint8_t *out_bytes = (uint8_t *) &out;
746+
747+
out_bytes[0] = in[1];
748+
out_bytes[1] = in[0];
749+
out_bytes[2] = in[3];
750+
out_bytes[3] = in[2];
751+
out_bytes[4] = in[5];
752+
out_bytes[5] = in[4];
753+
out_bytes[6] = in[7];
754+
out_bytes[7] = in[6];
755+
756+
return out;
757+
}
758+
759+
static inline uint64_t neu_htonbl(uint64_t u)
760+
{
761+
uint8_t *in = (uint8_t *) &u;
762+
uint64_t out = 0;
763+
uint8_t *out_bytes = (uint8_t *) &out;
764+
765+
out_bytes[0] = in[6];
766+
out_bytes[1] = in[7];
767+
out_bytes[2] = in[4];
768+
out_bytes[3] = in[5];
769+
out_bytes[4] = in[2];
770+
out_bytes[5] = in[3];
771+
out_bytes[6] = in[0];
772+
out_bytes[7] = in[1];
773+
774+
return out;
775+
}
776+
741777
static inline uint64_t neu_ntohll(uint64_t u)
742778
{
743779
return neu_htonll(u);

‎plugins/modbus/modbus-rtu.json‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818
},
1919
{
2020
"type": 7,
21-
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|)$"
21+
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
2222
},
2323
{
2424
"type": 8,
25-
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|)$"
25+
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
2626
},
2727
{
2828
"type": 9,
2929
"regex": "^[0-9]+![3-4][0-9]+(#BB|#BL|#LL|#LB|)$"
3030
},
3131
{
3232
"type": 10,
33-
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|)$"
33+
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
3434
},
3535
{
3636
"type": 11,
@@ -106,6 +106,35 @@
106106
"max": 10000
107107
}
108108
},
109+
"endianess_64": {
110+
"name": "Endianess of 8-Byte Data",
111+
"name_zh": "8 字节数据字节序",
112+
"description": "Tag byte order, applicable to int64, uint64 and double types",
113+
"description_zh": "点位字节序,适用于 int64,uint64 和 double 类型",
114+
"attribute": "required",
115+
"type": "map",
116+
"default": 1,
117+
"valid": {
118+
"map": [
119+
{
120+
"key": "12 34 56 78",
121+
"value": 1
122+
},
123+
{
124+
"key": "21 43 65 87",
125+
"value": 2
126+
},
127+
{
128+
"key": "87 65 43 21",
129+
"value": 3
130+
},
131+
{
132+
"key": "78 56 34 12",
133+
"value": 4
134+
}
135+
]
136+
}
137+
},
109138
"interval": {
110139
"name": "Send Interval",
111140
"name_zh": "指令发送间隔",

‎plugins/modbus/modbus-tcp.json‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818
},
1919
{
2020
"type": 7,
21-
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|)$"
21+
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
2222
},
2323
{
2424
"type": 8,
25-
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|)$"
25+
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
2626
},
2727
{
2828
"type": 9,
2929
"regex": "^[0-9]+![3-4][0-9]+(#BB|#BL|#LL|#LB|)$"
3030
},
3131
{
3232
"type": 10,
33-
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|)$"
33+
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
3434
},
3535
{
3636
"type": 11,
@@ -112,6 +112,35 @@
112112
"max": 10000
113113
}
114114
},
115+
"endianess_64": {
116+
"name": "Endianess of 8-Byte Data",
117+
"name_zh": "8 字节数据字节序",
118+
"description": "Tag byte order, applicable to int64, uint64 and double types",
119+
"description_zh": "点位字节序,适用于 int64,uint64 和 double 类型",
120+
"attribute": "required",
121+
"type": "map",
122+
"default": 1,
123+
"valid": {
124+
"map": [
125+
{
126+
"key": "12 34 56 78",
127+
"value": 1
128+
},
129+
{
130+
"key": "21 43 65 87",
131+
"value": 2
132+
},
133+
{
134+
"key": "87 65 43 21",
135+
"value": 3
136+
},
137+
{
138+
"key": "78 56 34 12",
139+
"value": 4
140+
}
141+
]
142+
}
143+
},
115144
"interval": {
116145
"name": "Send Interval",
117146
"name_zh": "指令发送间隔",

‎plugins/modbus/modbus.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ typedef enum modbus_area {
6666
MODBUS_AREA_HOLD_REGISTER = 4,
6767
} modbus_area_e;
6868

69+
typedef enum modbus_endianess_64 {
70+
MODBUS_LL = 1, // 12 34 56 78
71+
MODBUS_LB = 2, // 21 43 65 87
72+
MODBUS_BB = 3, // 87 65 43 21
73+
MODBUS_BL = 4, // 78 56 34 12
74+
} modbus_endianess_64;
75+
6976
struct modbus_header {
7077
uint16_t seq;
7178
uint16_t protocol;

‎plugins/modbus/modbus_point.c‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ modbus_read_cmd_sort_t *modbus_tag_sort(UT_array *tags, uint16_t max_byte)
230230
return sort_result;
231231
}
232232

233-
int cal_n_byte(int type, neu_value_u *value, neu_datatag_addr_option_u option)
233+
int cal_n_byte(int type, neu_value_u *value, neu_datatag_addr_option_u option,
234+
modbus_endianess_64 endianess_64, bool default_tag_endian_64)
234235
{
235236
int n = 0;
236237
switch (type) {
@@ -249,6 +250,9 @@ int cal_n_byte(int type, neu_value_u *value, neu_datatag_addr_option_u option)
249250
case NEU_TYPE_DOUBLE:
250251
case NEU_TYPE_INT64:
251252
case NEU_TYPE_UINT64:
253+
if (default_tag_endian_64) {
254+
modbus_convert_endianess_64(value, endianess_64);
255+
}
252256
n = sizeof(uint64_t);
253257
value->u64 = neu_htonll(value->u64);
254258
break;
@@ -282,7 +286,8 @@ int cal_n_byte(int type, neu_value_u *value, neu_datatag_addr_option_u option)
282286
return n;
283287
}
284288

285-
modbus_write_cmd_sort_t *modbus_write_tags_sort(UT_array *tags)
289+
modbus_write_cmd_sort_t *
290+
modbus_write_tags_sort(UT_array *tags, modbus_endianess_64 endianess_64)
286291
{
287292
neu_tag_sort_result_t *result =
288293
neu_tag_sort(tags, tag_sort_write, tag_cmp_write);
@@ -304,14 +309,17 @@ modbus_write_cmd_sort_t *modbus_write_tags_sort(UT_array *tags)
304309
utarray_foreach(result->sorts[i].tags, modbus_point_write_t **, tag_s)
305310
{
306311
if ((*tag_s)->point.area == MODBUS_AREA_COIL) {
307-
n_byte_tag = cal_n_byte((*tag_s)->point.type, &(*tag_s)->value,
308-
(*tag_s)->point.option);
312+
n_byte_tag =
313+
cal_n_byte((*tag_s)->point.type, &(*tag_s)->value,
314+
(*tag_s)->point.option, endianess_64, true);
309315
data_bit[k / 8] += ((*tag_s)->value.i8) << k % 8;
310316
n_byte += n_byte_tag;
311317
k++;
312318
} else {
313-
n_byte_tag = cal_n_byte((*tag_s)->point.type, &(*tag_s)->value,
314-
(*tag_s)->point.option);
319+
n_byte_tag =
320+
cal_n_byte((*tag_s)->point.type, &(*tag_s)->value,
321+
(*tag_s)->point.option, endianess_64,
322+
(*tag_s)->point.option.value64.is_default);
315323
memcpy(sort_result->cmd[i].bytes +
316324
2 *
317325
((*tag_s)->point.start_address -
@@ -521,4 +529,24 @@ static bool tag_sort_write(neu_tag_sort_t *sort, void *tag,
521529
}
522530

523531
return true;
532+
}
533+
534+
void modbus_convert_endianess_64(neu_value_u * value,
535+
modbus_endianess_64 endianess_64)
536+
{
537+
switch (endianess_64) {
538+
case MODBUS_LL:
539+
break;
540+
case MODBUS_BB:
541+
value->u64 = neu_htonll(value->u64);
542+
break;
543+
case MODBUS_LB:
544+
value->u64 = neu_htonlb(value->u64);
545+
break;
546+
case MODBUS_BL:
547+
value->u64 = neu_htonbl(value->u64);
548+
break;
549+
default:
550+
break;
551+
}
524552
}

‎plugins/modbus/modbus_point.h‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ typedef struct modbus_write_cmd_sort {
7979
modbus_write_cmd_t *cmd;
8080
} modbus_write_cmd_sort_t;
8181

82-
modbus_read_cmd_sort_t * modbus_tag_sort(UT_array *tags, uint16_t max_byte);
83-
modbus_write_cmd_sort_t *modbus_write_tags_sort(UT_array *tags);
84-
void modbus_tag_sort_free(modbus_read_cmd_sort_t *cs);
82+
modbus_read_cmd_sort_t *modbus_tag_sort(UT_array *tags, uint16_t max_byte);
83+
modbus_write_cmd_sort_t *
84+
modbus_write_tags_sort(UT_array *tags, modbus_endianess_64 endianess_64);
85+
void modbus_tag_sort_free(modbus_read_cmd_sort_t *cs);
86+
87+
void modbus_convert_endianess_64(neu_value_u * value,
88+
modbus_endianess_64 endianess_64);
8589

8690
#ifdef __cplusplus
8791
}

‎plugins/modbus/modbus_req.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ int modbus_value_handle(void *ctx, uint8_t slave_id, uint16_t n_byte,
414414
case NEU_TYPE_DOUBLE:
415415
case NEU_TYPE_INT64:
416416
case NEU_TYPE_UINT64:
417+
if ((*p_tag)->option.value64.is_default) {
418+
modbus_convert_endianess_64(&dvalue.value,
419+
plugin->endianess_64);
420+
}
417421
dvalue.value.u64 = neu_ntohll(dvalue.value.u64);
418422
break;
419423
case NEU_TYPE_BIT: {
@@ -677,6 +681,9 @@ int modbus_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
677681
case NEU_TYPE_DOUBLE:
678682
case NEU_TYPE_INT64:
679683
case NEU_TYPE_UINT64:
684+
if (tag->option.value64.is_default) {
685+
modbus_convert_endianess_64(&value, plugin->endianess_64);
686+
}
680687
value.u64 = neu_htonll(value.u64);
681688
n_byte = sizeof(uint64_t);
682689
break;
@@ -744,7 +751,7 @@ int modbus_write_tags(neu_plugin_t *plugin, void *req, UT_array *tags)
744751

745752
utarray_push_back(gtags->tags, &p);
746753
}
747-
gtags->cmd_sort = modbus_write_tags_sort(gtags->tags);
754+
gtags->cmd_sort = modbus_write_tags_sort(gtags->tags, plugin->endianess_64);
748755
for (uint16_t i = 0; i < gtags->cmd_sort->n_cmd; i++) {
749756
uint16_t response_size = 0;
750757

0 commit comments

Comments
 (0)