Skip to content

Instantly share code, notes, and snippets.

@Jacajack
Last active March 24, 2017 19:48
Show Gist options
  • Save Jacajack/a4239280b684d250d420b85d58f17aca to your computer and use it in GitHub Desktop.
Save Jacajack/a4239280b684d250d420b85d58f17aca to your computer and use it in GitHub Desktop.
To make manual building of Modbus frames easier...
#include <stdio.h>
#include <signal.h>
#include <inttypes.h>
#include <string.h>
#include <lightmodbus/master.h>
#include <pthread.h>
ModbusMaster master;
pthread_t pt;
void sigexit( int signum )
{
printf( "\nBye!\n" );
pthread_cancel( pt );
exit( 0 );
}
int promptval( const char *name )
{
int val = 0;
char buf[1024];
printf( "%s>", name );
scanf( "%1023s", buf );
sscanf( buf, "%d", &val );
return val;
}
void *prompt( void *dummy )
{
unsigned int i;
uint16_t func;
uint16_t addr;
uint16_t index;
uint16_t count;
uint16_t andmask;
uint16_t ormask;
uint16_t val;
uint8_t ec = 0;
uint8_t *data = NULL;
char strbuf[256];
addr = promptval( "addr" );
func = promptval( "func" );
switch ( func )
{
case 1:
case 2:
index = promptval( "index" );
count = promptval( "count" );
ec = modbusBuildRequest0102( &master, func, addr, index, count );
break;
case 3:
case 4:
index = promptval( "index" );
count = promptval( "count" );
ec = modbusBuildRequest0304( &master, func, addr, index, count );
break;
case 5:
index = promptval( "index" );
val = promptval( "val" );
ec = modbusBuildRequest05( &master, addr, index, !!val );
break;
case 6:
index = promptval( "index" );
val = promptval( "val" );
ec = modbusBuildRequest06( &master, addr, index, val );
break;
case 15:
index = promptval( "index" );
count = promptval( "count" );
data = calloc( BITSTOBYTES( count ), sizeof( uint8_t ) );
if ( data == NULL ) raise( SIGINT );
for ( i = 0; i < count; i++ )
{
memset( strbuf, 0, 256 );
snprintf( strbuf, 256, "%.2d:%.2d:%.2d", index, index + i, index + count );
val = promptval( strbuf );
modbusMaskWrite( data, BITSTOBYTES( count ), i, val );
}
ec = modbusBuildRequest15( &master, addr, index, count, data );
free( data );
break;
case 16:
index = promptval( "index" );
count = promptval( "count" );
data = calloc( count, sizeof( uint16_t ) );
if ( data == NULL ) raise( SIGINT );
for ( i = 0; i < count; i++ )
{
memset( strbuf, 0, 256 );
snprintf( strbuf, 256, "%.2d:%.2d:%.2d", index, index + i, index + count );
val = promptval( strbuf );
data[i] = val;
}
ec = modbusBuildRequest16( &master, addr, index, count, (uint16_t*) data );
free( data );
break;
case 22:
index = promptval( "index" );
andmask = promptval( "and" );
ormask = promptval( "or" );
ec = modbusBuildRequest22( &master, addr, index, andmask, ormask );
break;
default:
printf( ":unsupported function code!\n" );
break;
}
if ( ec != MODBUS_OK ) printf( ":error %d\n", ec );
else
{
for ( i = 0; i < master.request.length; i++ )
printf( "%.2X ", master.request.frame[i] );
printf( "\n" );
}
return NULL;
}
void sigreprompt( int signum )
{
pthread_cancel( pt );
printf( "\n" );
signal( SIGQUIT, sigreprompt );
}
int main( int argc, char **argv )
{
signal( SIGINT, sigexit );
signal( SIGQUIT, sigreprompt );
modbusMasterInit( &master );
printf( "SIGINT to exit\n" );
while ( 1 )
{
pthread_create( &pt, NULL, prompt, NULL );
pthread_join( pt, NULL );
}
modbusMasterEnd( &master );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment