Skip to content

Commit 5cf917d

Browse files
committed
Various fixes from #275
Thanks to AlexLonardi.
1 parent bb12c81 commit 5cf917d

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/modbus-ascii.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ static uint8_t hex_ascii_to_nibble(char digit) {
6161

6262
/* Build an ASCII request header */
6363
static int _modbus_ascii_build_request_basis(modbus_t *ctx, int function,
64-
int addr, int nb,
65-
uint8_t *req)
64+
int addr, int nb, uint8_t *req)
6665
{
6766
assert(ctx->slave != -1);
6867

@@ -74,16 +73,17 @@ static int _modbus_ascii_build_request_basis(modbus_t *ctx, int function,
7473
req[5] = nb >> 8;
7574
req[6] = nb & 0x00ff;
7675

77-
return _MODBUS_ASCII_PRESET_REQ_LENGTH;
76+
return 7;
7877
}
7978

8079
/* Build an ASCII response header */
8180
static int _modbus_ascii_build_response_basis(sft_t *sft, uint8_t *rsp)
8281
{
83-
rsp[0] = sft->slave;
84-
rsp[1] = sft->function;
82+
rsp[0] = ':';
83+
rsp[1] = sft->slave;
84+
rsp[2] = sft->function;
8585

86-
return _MODBUS_ASCII_PRESET_RSP_LENGTH;
86+
return 3;
8787
}
8888

8989
/* calculate the Longitudinal Redundancy Checking (LRC)
@@ -223,11 +223,14 @@ static ssize_t _modbus_ascii_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
223223

224224
static ssize_t _modbus_ascii_send(modbus_t *ctx, const uint8_t *req, int req_length)
225225
{
226-
uint8_t ascii_req[3 + (MODBUS_ASCII_MAX_ADU_LENGTH * 2)];
227-
226+
/* FIXME Ugly copy! */
227+
uint8_t ascii_req[MODBUS_ASCII_MAX_ADU_LENGTH];
228228
ssize_t i, j = 0;
229+
229230
for (i = 0; i < req_length; i++) {
230-
if (req[i] == ':' || req[i] == '\r' || req[i] == '\n') {
231+
if ((i == 0 && req[i] == ':') ||
232+
(i == req_length - 2 && req[i] == '\r') ||
233+
(i == req_length - 1 && req[i] == '\n')) {
231234
ascii_req[j++] = req[i];
232235
} else {
233236
ascii_req[j++] = nibble_to_hex_ascii(req[i] >> 4);
@@ -237,7 +240,8 @@ static ssize_t _modbus_ascii_send(modbus_t *ctx, const uint8_t *req, int req_len
237240
ascii_req[j] = '\0';
238241

239242
ssize_t size = _modbus_serial_send(ctx, ascii_req, j);
240-
return ((size - 3) / 2) +3;
243+
/* FIXME */
244+
return ((size - 3) / 2) + 3;
241245
}
242246

243247
static void _modbus_ascii_free(modbus_t *ctx) {

0 commit comments

Comments
 (0)