0

I'm writing application using libmodbus 3.1.2. I'm unable to print complete output of modbus functions. Also I'm getting some garbage values in output. Please suggest.

 printf("================Sending  Sample delay command==============\n");
  i32_read_ret = modbus_read_registers(ctx, 164, 1, sample_delay);
  printf("\nSample delay result:%d\n", i32_read_ret);
  printf("\n Sample delay : \n");
  for (i = 0; i < 40; i++) //printing only the received characters
  {
    printf(" : %.2x \t", (int)( * (unsigned char * )( & sample_delay[i])));

  }
  printf("\n\n\n");

  tcflush(fd, TCIOFLUSH);
  printf("================Sending  measurement command==============\n");

  i32_read_ret = modbus_write_registers(ctx, 1, 5, measurement);
  printf("\nMeasurement result : %d \n", i32_read_ret);
  for (i = 0; i < 40; i++) {
    printf(": %.2x \t", (int)( * (unsigned char * )( & measurement[i])));

  }
  printf("\n");
  tcflush(fd, TCIOFLUSH);
  printf("================Sending  actual measurement command==============\n");
  i32_read_ret = modbus_read_registers(ctx, 83, 10, actual_measurement);
  printf("\nRead Return actual_measurement 3:%d\n\n\n", i32_read_ret);

  for (i = 0; i < 40; i++) //printing only the received characters
  {
    printf(" : %.2x \t", (int)( * (unsigned char * )( & actual_measurement[i])));

  }
  printf("\n");
  printf("%s\n", actual_measurement);
  tcflush(fd, TCIOFLUSH);

o/p

================Sending  Sample delay command==============
[28][03][00][A4][00][01][C2][10]
Waiting for a confirmation...
<28><03><02><01><2C><E5><CF>

Sample delay result:1

 Sample delay : 
 : 2c    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 08    : 08    : 80    : 00    : 38    : 00    : 29    : 00    : 00    : 00    : c8    : 7d    : 01    : 00    : 00    : 00    : 30    : 00    : 00    : 00   


================Sending  measurement command==============
[28][10][00][01][00][05][0A][B1][D8][77][1E][B1][D8][77][1E][0D][F8][9F][E7]
Waiting for a confirmation...
<28><10><00><01><00><05><56><33>

Measurement result : 5 
: d8    : 1e    : d8    : 1e    : f8    : 00    : 65    : 57    : 70    : 00    : 10    : 02    : 65    : 57    : 7c    : 00    : 00 : 04   : 01    : 00    : 49    : 01    : 31    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00 : 00   : 00    : 00    : 00    : 00    : 00    
================Sending  actual measurement command==============
[28][03][00][53][00][0A][32][25]
Waiting for a confirmation...
<28><03><14><41><DD><8E><59><00><00><00><00><00><00><00><00><46><1C><34><00><46><1C><34><00><48><84>

Read Return actual_measurement 3:10


 : dd    : 59    : 00    : 00    : 00    : 00    : 1c    : 00    : 1c    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 08    : 1b    : 31    : 00    : 28    : 00    : 08    : 00    : 01    : 00    : 00    : 00    : 00    : 00    : 20    : 07    : 00    : 00    : 20    : 07   
�AY�
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
APS
  • 59
  • 1
  • 7
  • getting partial ouput for printf. Pls ignore typo mistake in title. – APS Feb 04 '19 at 07:41
  • You should use return values of modbus functions to print the result. `for(i=0;i<40;i++)` --> `for(i=0;i – kiran Biradar Feb 04 '19 at 07:45
  • tried with same, but still getting same result. for(i=0;i – APS Feb 04 '19 at 07:52
  • What result? Also how are you defining `actual_measurement`? if it is of type `uint16_t` you cannot use `printf("%s\n", actual_measurement);` to print as `string`. See [how to print uint16_t](https://stackoverflow.com/questions/29112878/how-do-i-printf-a-uint16-t). – kiran Biradar Feb 04 '19 at 07:53
  • removed line printf("%s\n", actual_measurement); printing with following lines. for(i=0;i – APS Feb 04 '19 at 08:01
  • Actual output : ================Sending actual measurement command============== [28][03][00][53][00][0A][32][25] ---- > command Waiting for a confirmation... <28><03><14><41><01><49><00><00><00><00><00><00><00><00><46><1C><34><00><46><1C><34><00><1B> -----> expected output by printf actual output getting : Read Return actual_measurement 3:10 : de : 49 : 00 : 00 : 00 : 00 : 1c : 00 : 1c : 00 – APS Feb 04 '19 at 08:02
  • I'm not understanding the problem you are facing as `modbus_read_registers` is reading 10 registers contents as you asked. How is it partial output? – kiran Biradar Feb 04 '19 at 08:08
  • Thanks for your suggestions. partial output PRINTING problem resolved after changing printf lines but garbage values problem still persists. : for(i=0;i – APS Feb 04 '19 at 08:35

1 Answers1

0

After correcting line of printf issue got resolved.

for(i=0;i<i32_read_ret;i++)
{
   printf("%X",sample_delay[i]);
}
APS
  • 59
  • 1
  • 7
  • .... so your code is working as expected now? If so, congratulations :) Feel free to accept your own answer as the solution. Otherwise, please edit your original question to reflect the new problems you have encountered (if you have reputation enough to make an edit?) – Morten Jensen Feb 04 '19 at 11:19