开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Re: ModIO with libmodbus and Reset


 

Hi Kirk,

Looking at the program, I can't see where you specify, the timeout period for when the master decides that the slave hasn't responded. I haven't used libmodbus, but there should be a way when setting to the master, to specify the timeout.

This may be why the master keeps reporting errors.

Cheers,


Peter.

On 11/02/2012 12:27 PM, Kirk Wallace wrote:
On Sat, 2012-02-11 at 11:06 +1100, Peter Homann wrote:

Hi Kirk,

I'm struggling to understand what you are saying. :)
Just in case, here is the program below. I'm not that good with C so
don't be surprised if it is poorly written.
~~~~~~~~~~~~~~

/*
* Copyright ? 2008-2010 Stéphane Raimbault
<stephane.raimbault@...>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see<>.
*/

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<modbus.h>

// #include "unit-test.h"

int main(int argc, char *argv[])
{
modbus_t *ctx;
int rc;
char lcdchar[80];
int lcdgrp[40];
uint16_t regdata[24];
uint16_t *lcddata;
ctx = modbus_new_rtu("/dev/ttyS0", 57600, 'N', 8, 1);
if (ctx == NULL) {
fprintf(stderr, "Unable to create the libmodbus context&#92;n");
return -1;
}
modbus_set_slave(ctx, 6);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s&#92;n",
modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
printf("** TESTING **&#92;n");
rc = modbus_read_registers(ctx, 100, 7, regdata);
printf("&#92;nmodbus_read_registers: ");
printf("rc = %d&#92;n", rc);
if (rc == 7) {
printf("OK&#92;n");
} else {
printf("FAILED to read registers&#92;n");
goto close;
}
printf("100 = %0X&#92;n", regdata[0]);
printf("101 = %0X&#92;n", regdata[1]);
printf("102 = %0X&#92;n", regdata[2]);
printf("103 = %0X&#92;n", regdata[3]);
printf("104 = %0X&#92;n", regdata[4]);
printf("105 = %0X&#92;n", regdata[5]);
printf("106 = %0X&#92;n", regdata[6]);
printf("OK&#92;n");

printf("&#92;n** Setting up LCD **&#92;n");
regdata[1] |= (1<< 0); /* enable LCD */
// regdata[1]&= ~(1<< 0); /* disable LCD */
// regdata[1] |= (1<< 7); /* disable debug display */
// regdata[1] = 0x84; /* Default */
rc = modbus_write_register(ctx, 101, regdata[1]);
printf("write rc = %d&#92;n", rc);
rc = modbus_read_registers(ctx, 101, 1,&regdata[1]);
printf("read rc = %d&#92;n", rc);
printf("101 = %0X&#92;n", regdata[1]);
/*
rc = modbus_write_register(ctx, 1140, 1);
printf("reset = %d&#92;n", rc);
*/
printf("&#92;n** Write to LCD **&#92;n");
rc = modbus_write_register(ctx, 1000, 0x3130);
printf("write rc = %d&#92;n", rc);
rc = modbus_write_register(ctx, 1001, 0x3332);
printf("write rc = %d&#92;n", rc);
rc = modbus_write_register(ctx, 1002, 0x3534);
printf("write rc = %d&#92;n", rc);
rc = modbus_write_register(ctx, 1003, 0x3736);
printf("write rc = %d&#92;n", rc);

printf("&#92;n** Done **&#92;n");

close:
modbus_close(ctx);
modbus_free(ctx);

return 0;
}
~~~~~~~~~~~~~~~~~~~
--
-----------------------------------------------------------------------------
eStore:
Web : ModIO - Modbus Interface Unit
email : peter@... DigiSpeed - Isolated 10Vdc I/F
Phone : +61 421 601 665 TurboTaig - Taig Mill Upgrade board

Join [email protected] to automatically receive all group messages.