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
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.