Help to use libmodbus to create a slave

7,757 views
Skip to first unread message

José Eduardo S. C. Xavier

unread,
May 10, 2012, 9:59:08 AM5/10/12
to libm...@googlegroups.com
Hi,
Because it is my first mail I want to thanks you for the library :)

Right now I'm using the libmodbus in an application that act as modbus master and it is working good but now I need to create an app to be a slave.
What should I do in this case? Just initialize the tcp with modbus_init_listen_tcp() and wait for something with modbus_listen()?

thanks



metRo_

unread,
May 10, 2012, 10:36:09 AM5/10/12
to libmodbus
This is the code I have in the aplication that works as server:

modbusConnection::modbusConnection()
{
mutex.lock();

modbus_connection=new modbus_param_t();
modbus_init_tcp(modbus_connection, "127.0.0.2", 502);
//modbus_init_tcp(modbus_connection,"192.168.105.183", 502);

while (modbus_connect(modbus_connection)==-1)
{
QMessageBox::information(NULL,"Error","Nao consegui ligar aos
tapetes!");

connected=false;

qDebug() << "Couldn't connect to machine";
}

mutex.unlock();
}

It was a friend that help me with that.

To try implement the the slave I'm doing the following:

modbus_slave_data=new modbus_mapping_t();
int a;
a = modbus_mapping_new(modbus_slave_data,10,10,10,10);
qDebug()<<a; //return 1 so I supose it is ok

modbus_connection=new modbus_param_t();
int b;
b = modbus_init_listen_tcp(modbus_connection);
qDebug()<<b; //the application blocks here :s


//uint8_t *q;
//int *ql;
//int i=0;
//qDebug()<<modbus_listen(modbus_connection, q, ql);



On 10 Maio, 14:59, José Eduardo S. C. Xavier <josescxav...@gmail.com>
wrote:

Stephane Raimbault

unread,
Jun 11, 2012, 6:23:58 PM6/11/12
to libm...@googlegroups.com
You'll find many slave/server examples in https://github.com/stephane/libmodbus/tree/master/tests

Stéphane

Lakshmana Vuppu

unread,
Feb 3, 2013, 9:47:34 AM2/3/13
to libm...@googlegroups.com
Hi stephane

I wish you are the person, who made all those example programs under "tests"
I have found the examples very helpful and ready to use.

But, I have problem using the "modbus_set_float" and "modbus_get_float" functions.

I would like to read and and change the values of the registers on the slave program (bandwidth-server-many-up.c).
The values should be continuously read using the client and modified if necessary.

At the moment, I can connect to slave using the master (TCP client) and write to the registers, but I have no idea, where the values are stored on the slave and how they can be accessed on the server program.

From, what I have understood, the modbus-get-* functions are used for this purpose.
Could you please help me, how to create my own table and update it on the slave program, and access is using the master.

Thank you
Lakshmana Vuppu

Stéphane Raimbault

unread,
May 23, 2013, 3:42:47 AM5/23/13
to libm...@googlegroups.com
Hi Lakshmana,

I missed your mail so I hope your question still relevant after a so long time.

Yes, I wrote all example programs and I'm the maintainer of libmodbus.
Your client can read or write float values by using usual modbus_read_registers and modbus_write_registers.
The only difference is that float a stored in 32 bits (2 registers of u_int16) so you need to write/read two registers at once for one float.

Once you have read your two registers which contain the float value, you can extract it from the array with modbus_get_float (or the new modbus_get_float_dcba).
You'll find example in unit-test-client.c (I've updated documentation in master and I'm working on another patch to add an example in doc).

Stéphane



2013/2/3 Lakshmana Vuppu <laks...@gmail.com>

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes libmodbus.
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.
Pour plus d'options, visitez le site https://groups.google.com/groups/opt_out .
 
 

Reply all
Reply to author
Forward
0 new messages