Help modbus_write_register - Not working - Raspberry pi

閲覧: 111 回
最初の未読メッセージにスキップ

Sathya Narayanan

未読、
2016/06/19 3:00:432016/06/19
To: libmodbus
HI All,

Can someone please help me using the modbus_write_register function. I am writing a c program that would be executed in my Raspberry pi  running Raspbian Jessie.

I am trying to update the value of the register of a pic microcontroller. I am able to do this with Medford.exe. So I suppose the pic microcontroller is in working order.

Note: I only have 1 input register and 1 holding register in PIC microcontroller the slave ID of my PIC Microcontroller is 0x02

 int16 hold_regs[] = {0x1100};  
 int16 input_regs[] = {0x1100};

Please help me with the code below. where I am trying to update the input register of the pic microcontroller using Raspberry pi. I am using a USB to RS485 converter and I have verified that the device is in /dev/ttyUSB0.  


#include <stdio.h>
#include <modbus.h>

#define MB_BITRATE 9600
#define MB_DATABITS 8
#define MB_STOPBITS 1
#define MB_PARITY 'N'
#define MB_SLAVE_ADDRESS 2

#define MB_REG_BASE 0
#define MB_REG_COUNT 1

int main ()
{
 modbus_t *ctx;
 uint16_t tab_reg[MB_REG_BASE+MB_REG_COUNT];
 int n, i;
 int errno = 0;

  ctx = modbus_new_rtu("/dev/ttyUSB0",
                MB_BITRATE,
                    MB_PARITY,
                     MB_DATABITS,
                   MB_STOPBITS);
 if (ctx == NULL) {
   fprintf(stderr,
        "Unable to create the libmodbus context on serial port %s\n",
          "/dev/ttyUSB0");
   return -1;
 }
 printf("connection created \n");

  if ( modbus_set_slave(ctx, MB_SLAVE_ADDRESS) )
               {
                fprintf(stderr, "Failed to set modbus slave address\n");
               errno = -1;
            goto fail;
           }  
    printf("slave set \n");
 
 if ( modbus_connect(ctx))
   {
     fprintf(stderr,
       "Unable to connect to modbus server");
     errno = -1;
     goto fail;
   }
     printf("connted to slave \n");
 printf("Starting write \n");
 n = modbus_write_register(ctx, 0, 255); //I suppose 2nd parameter is the memory address.
 printf("Value of N is %d \n", n);
 if ( n <= 0 )
   {
     fprintf(stderr, "Unable to read modbus registers\n");
     errno = -1;
     goto fail;
   }
 
 for ( i=MB_REG_BASE; i<MB_REG_BASE+n; i++ )
   {
     printf ( "[%d]: %d\n", i, tab_reg[i]);
   }

 fail:
 modbus_close(ctx);
 modbus_free(ctx);

  return errno;
}





Sathya Narayanan

未読、
2016/06/19 14:32:092016/06/19
To: libmodbus
Nobody? wow...

Stéphane Raimbault

未読、
2016/06/19 14:45:492016/06/19
To: libm...@googlegroups.com
 //I suppose 2nd parameter is the memory address.

Don't suppose anything, just read the documentation:

so it's the Modbus address of the remote host.

Your comment says: Unable to read whereas you try to write...

BTW it's useless to loop over tab_reg after a write
 

--
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 obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

全員に返信
投稿者に返信
転送
新着メール 0 件