Raw Data in libmodbus

237 views
Skip to first unread message

Matthew Butch

unread,
Apr 19, 2012, 12:01:37 PM4/19/12
to libm...@googlegroups.com
I'm the developer of ModBusKit (a Cocoa framework for use on OS X) and ModBus Probe (which uses MBK). Download them here: http://modbusapps.com/

MBK was originally developed with libmodbus 1.2.3, and I'm updating it with 3.0.1.

For MBP, there is the ability to see the raw data of the ModBus communications (query and response). I had to add four functions and 4 variables to the libmodbus code to make that work. And it was fine. But obviously a lot has changed between the two libmodbus versions, so when I tried to do the same thing to the new one, I keep getting crashes from what looks like memory corruption.

I'd like this to be part of the official libmodbus release, if that's possible. I'd be happy to share the complete source code.

Here's the relevant code:

In modbus.c:
--
// Activates saving raw query and responses
void modbus_set_raw_data_save(modbus_t *mb_param, int boolean)
{
mb_param->saveRawData=boolean;
}

// Returns a raw query
int modbus_last_raw_query(modbus_t *mb_param, uint8_t *rawQuery)
{
int i;
if(mb_param->rawQuery!=NULL)
{
for (i = 0; i < MIN_QUERY_LENGTH; i++)
rawQuery[i]=mb_param->rawQuery[i];
return TRUE;
}
return FALSE;
}

// Returns a raw response
int modbus_last_raw_response(modbus_t *mb_param, uint8_t *rawResponse)
{
int i;
if(mb_param->rawResponse!=NULL)
{
for (i = 0; i < mb_param->rawResponseLength; i++)
rawResponse[i]=mb_param->rawResponse[i];
return TRUE;
}
return FALSE;
}

int modbus_last_raw_query_length(modbus_t *mb_param)
{
return(mb_param->rawQueryLength);
}

int modbus_last_raw_response_length(modbus_t *mb_param)
{
return(mb_param->rawResponseLength);
}

static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
{
// snip other code

if (ctx->saveRawData && ctx->rawResponse!=NULL)
{
for (i = 0; i < msg_length; i++)
ctx->rawResponse[i]=msg[i];
ctx->rawResponseLength=msg_length;
}
}

static int send_msg(modbus_t *ctx, uint8_t *msg, int msg_length)
{
// snip other code
if (ctx->saveRawData && ctx->rawQuery!=NULL)
{
for (i = 0; i < msg_length; i++)
ctx->rawQuery[i]=msg[i];
ctx->rawQueryLength=msg_length;
}

// snip other code
}

--

-Matt

--

"Innovation distinguishes between a leader and a follower."
- Steve Jobs, 1955-2011

Stephane Raimbault

unread,
Jun 11, 2012, 6:44:00 PM6/11/12
to libm...@googlegroups.com
Le jeudi 19 avril 2012 18:01:37 UTC+2, Matthew Butch a écrit :
I'm the developer of ModBusKit (a Cocoa framework for use on OS X) and ModBus Probe (which uses MBK). Download them here: http://modbusapps.com/

MBK was originally developed with libmodbus 1.2.3, and I'm updating it with 3.0.1.

For MBP, there is the ability to see the raw data of the ModBus communications (query and response). I had to add four functions and 4 variables to the libmodbus code to make that work. And it was fine. But obviously a lot has changed between the two libmodbus versions, so when I tried to do the same thing to the new one, I keep getting crashes from what looks like memory corruption.

I'd like this to be part of the official libmodbus release, if that's possible. I'd be happy to share the complete source code.


You want to save the last query/response, in this goal, I prefer to define a generic callback system so a function given in argument will be called by libmodbus on send and another on receive (if defined).

The idea could be to trigger the callback on specific queries/responses but I think it will be easier to just call the callback and run the filering in the user callback.

So you could store your data outside of the libmodbus context (in database for example).

Stéphane
 
Reply all
Reply to author
Forward
0 new messages