stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.44k stars 1.75k forks source link

How to Build as independent executable? #685

Open georgethomas08 opened 1 year ago

georgethomas08 commented 1 year ago

Hi Team,

I'm new to C and C compilers, I need to poll a MODBUS RTU and I tired my best built a program. please find the program below


include

include

include

include

include

include "modbus.h"

include "modbus-version.h"

define SERVER_ID 10

define UART_PORT "/dev/ttyHS1"

define BAUD_RATE 9600

define PARITY 'E'

define BYTESIZE 8

define STOPBITS 1

//#define BCM_PIN_DE 17 //#define BCM_PIN_RE 18

define REGISTER_ADDRESS 0

define NO_OF_REGISTERS 114

int main(int argc, char argv[]) { uint16_t tab_rp_registers = NULL; modbus_t ctx = NULL; uint32_t sec_to = 10; uint32_t usec_to = 0; int i; int rc; int nb_points = 1; ctx = modbus_new_rtu(UART_PORT, BAUD_RATE, PARITY, BYTESIZE, STOPBITS); if (ctx == NULL) { //fprintf(stderr, "Unable to allocate libmodbus context\n"); printf("Unable to allocate libmodbus context\n"); return -1; } printf("RTU allocation done....\n"); modbus_set_debug(ctx, TRUE); modbus_set_error_recovery(ctx, MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL); modbus_set_slave(ctx, SERVER_ID); modbus_get_response_timeout(ctx, &sec_to, &usec_to); // modbus_enable_rpi(ctx,TRUE); // modbus_configure_rpi_bcm_pins(ctx,BCM_PIN_DE,BCM_PIN_RE); // modbus_rpi_pin_export_direction(ctx); printf("Connection Initiated....\n"); if (modbus_connect(ctx) == -1) { fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); modbus_free(ctx); return -1; } printf("Connection Successs....\n"); printf("Reading registers Start....\n"); // modbus sample query - modify it for your own purpose tab_rp_registers = (uint16_t ) malloc(nb_points sizeof(uint16_t)); memset(tab_rp_registers, 0, nb_points sizeof(uint16_t)); rc = modbus_read_input_registers(ctx, REGISTER_ADDRESS,NO_OF_REGISTERS, tab_rp_registers); //rc = modbus_read_registers(ctx, REGISTER_ADDRESS,NO_OF_REGISTERS, tab_rp_registers); printf("Date received is : %d\n",tab_rp_registers[0]); free(tab_rp_registers);

/* Close the connection */
//modbus_rpi_pin_unexport_direction(ctx);
modbus_close(ctx);
modbus_free(ctx);

}


And the out I'm getting the output but I don't how to built or how to package the executable which can work independently without shared libraries.

I would like to know it possible to an executable which can be created without shared libraries

In addition how to add the custom libraries to Visual studio code compiler library list. ?

It would be grateful if you could help

Regards George Thomas