int main(int argc, char *argv[]) { modbus_t *mb; uint16_t tab_reg[256]; uint8_t bit_reg[256]; int rc; int i; int actual; mb = modbus_new_tcp("192.168.1.120", 502); if (modbus_connect(mb) == -1) { fprintf(stderr, "modbus connect: %s\n", modbus_strerror(errno)); modbus_free(mb); return -1; } sscanf(getenv("QUERY_STRING"),"%d",&actual); printf("Content-type: text/plain\n\n"); /* Read 5 registers from the address 10 */ //rc = modbus_read_registers(mb, 100, 10, tab_reg); rc = modbus_write_bit(mb,100,actual); if (rc == -1) { fprintf(stderr, "write bit: %s\n", modbus_strerror(errno)); return -1; } modbus_close(mb); modbus_free(mb); }
int main(int argc, char *argv[]) { modbus_t *mb; uint16_t tab_reg[256]; uint8_t bit_reg[256]; int rc; int i; if (argc != 4) { printf("name ip adress register and value\n"); exit(1); } int setvalue; sscanf(argv[3],"%d",&setvalue); int setregister; sscanf(argv[2],"%d",&setregister); mb = modbus_new_tcp(argv[1], 502); if (modbus_connect(mb) == -1) { fprintf(stderr, "modbus connect: %s\n", modbus_strerror(errno)); modbus_free(mb); return -1; } printf("%d=%d\n",setregister, setvalue); rc = modbus_write_bit(mb,setregister,setvalue); if (rc == -1) { fprintf(stderr, "write registers: %s\n", modbus_strerror(errno)); return -1; } modbus_close(mb); modbus_free(mb); }
int main(void) { int s = -1; modbus_t *ctx; modbus_mapping_t *mb_mapping; int rc = -1; pthread_t tId; pthread_attr_t attr; /* Initialize and set thread detached attribute */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); ctx = modbus_new_tcp("127.0.0.1", 1502); modbus_set_debug(ctx, TRUE); mb_mapping = modbus_mapping_new(500, 500, 500, 500); if (mb_mapping == NULL) { fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno)); modbus_free(ctx); return -1; } s = modbus_tcp_listen(ctx, 1); int id = 0; for (;;) { id++; printf("Waiting for another connection...\n\n"); modbus_tcp_accept(ctx, &s); modbus_t *new_ctx = modbus_clone_tcp(ctx); SA *sa = (SA*)malloc(sizeof(SA)); sa->ctxt = new_ctx; sa->mm = mb_mapping; sa->id = id; rc = pthread_create(&tId, &attr, serveClient, (void *)sa); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); break; } } printf("Quit the loop: %s\n", modbus_strerror(errno)); if (s != -1) { close(s); } modbus_mapping_free(mb_mapping); modbus_close(ctx); modbus_free(ctx); return 0; }
modbus_t* modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit) { modbus_t *ctx; modbus_rtu_t *ctx_rtu; size_t dest_size; size_t ret_size; ctx = (modbus_t *) malloc(sizeof(modbus_t)); _modbus_init_common(ctx); ctx->backend = &_modbus_rtu_backend; ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t)); ctx_rtu = (modbus_rtu_t *)ctx->backend_data; dest_size = sizeof(ctx_rtu->device); ret_size = strlcpy(ctx_rtu->device, device, dest_size); if (ret_size == 0) { fprintf(stderr, "The device string is empty\n"); modbus_free(ctx); errno = EINVAL; return NULL; } if (ret_size >= dest_size) { fprintf(stderr, "The device string has been truncated\n"); modbus_free(ctx); errno = EINVAL; return NULL; } ctx_rtu->baud = baud; if (parity == 'N' || parity == 'E' || parity == 'O') { ctx_rtu->parity = parity; } else { modbus_free(ctx); errno = EINVAL; return NULL; } ctx_rtu->data_bit = data_bit; ctx_rtu->stop_bit = stop_bit; #if HAVE_DECL_TIOCSRS485 /* The RS232 mode has been set by default */ ctx_rtu->serial_mode = MODBUS_RTU_RS232; #endif #if HAVE_DECL_TIOCM_RTS /* The RTS use has been set by default */ ctx_rtu->rts = MODBUS_RTU_RTS_NONE; #endif ctx_rtu->confirmation_to_ignore = FALSE; return ctx; }
int main() { modbus_t *ctx; uint16_t tab_reg[numero]; int rc; int i; ctx = modbus_new_tcp("192.168.2.18", 502); if (modbus_connect(ctx) == -1) { fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); modbus_free(ctx); return -1; } struct timeval old_response_timeout; struct timeval response_timeout; /* Save original timeout */ //modbus_get_response_timeout(ctx, &old_response_timeout); /* Define a new and too short timeout! */ //response_timeout.tv_sec = 2; //response_timeout.tv_usec = 500000; //modbus_set_response_timeout(ctx, &response_timeout); //rc = modbus_set_slave(ctx, 49); if (rc == -1) { fprintf(stderr, "SET: %s\n", modbus_strerror(errno)); if (ctx!=NULL) { fprintf(stderr,"CLOSE\n"); modbus_close(ctx); modbus_free(ctx); } return -1; } rc = modbus_read_input_registers(ctx, direccion, numero, tab_reg); if (rc == -1) { fprintf(stderr, "HOLA: %s\n", modbus_strerror(errno)); if (ctx!=NULL) { fprintf(stderr,"CLOSE\n"); modbus_close(ctx); modbus_free(ctx); } return -1; } for (i=0; i < rc; i++) { printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]); } modbus_close(ctx); modbus_free(ctx); }
int main() { modbus_t *ctx; uint16_t tab_reg[64]; int rc; int i; ctx = modbus_new_rtu("/dev/ttyUSB0", 9600, 'N', 8,1); if (modbus_connect(ctx) == -1) { fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); modbus_free(ctx); return -1; } rc = modbus_set_slave(ctx, 1); if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); return -1; } struct timeval response_timeout; response_timeout.tv_sec = 5; response_timeout.tv_usec = 0; modbus_set_response_timeout(ctx, &response_timeout); while(1){ modbus_connect(ctx); rc = modbus_read_registers(ctx, 13952, 1, &tab_reg[0]); if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); return -1; } usleep(17000); //max transaction timing. rc = modbus_read_registers(ctx, 13954, 1, &tab_reg[1]); if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); return -1; } for (i=0; i < 2; i++) { printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]); } //modbus_flush(ctx); //modbus_close(ctx); usleep(10000); modbus_close(ctx); } modbus_free(ctx); return 0; }
modbus_t* modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit) { modbus_t *ctx; modbus_rtu_t *ctx_rtu; size_t device_size; ctx = (modbus_t *) malloc(sizeof(modbus_t)); _modbus_init_common(ctx); ctx->backend = &_modbus_rtu_backend; ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t)); ctx_rtu = (modbus_rtu_t *)ctx->backend_data; /* Device name and \0 */ device_size = (strlen(device) + 1) * sizeof(char); if (device_size == 0) { fprintf(stderr, "The device string is empty\n"); modbus_free(ctx); errno = EINVAL; return NULL; } ctx_rtu->device = (char *) malloc(device_size); strcpy(ctx_rtu->device, device); ctx_rtu->baud = baud; if (parity == 'N' || parity == 'E' || parity == 'O') { ctx_rtu->parity = parity; } else { modbus_free(ctx); errno = EINVAL; return NULL; } ctx_rtu->data_bit = data_bit; ctx_rtu->stop_bit = stop_bit; #if HAVE_DECL_TIOCSRS485 /* The RS232 mode has been set by default */ ctx_rtu->serial_mode = MODBUS_RTU_RS232; #endif #if HAVE_DECL_TIOCM_RTS /* The RTS use has been set by default */ ctx_rtu->rts = MODBUS_RTU_RTS_NONE; /* Calculate estimated time in micro second to send one byte */ ctx_rtu->onebyte_time = (1000 * 1000) * (1 + data_bit + (parity == 'N' ? 0 : 1) + stop_bit) / baud; #endif ctx_rtu->confirmation_to_ignore = FALSE; return ctx; }
static void *modbus_side (void *arg) { modbus_t *ctx; uint16_t tab_reg[64]; int rc; int i; modbus_side_restart: //ctx = modbus_new_tcp("127.0.0.1", 502); ctx = modbus_new_tcp("140.159.153.159", 502); // Use 140.159.153.159 for uni, 127.0.0.1 for home. Server port remains as 502 for both cases // Initialize connection to modbus server if(modbus_connect(ctx) == -1) // Connection to server failed { fprintf(stderr, "Connection to server failed: %s\n", modbus_strerror(errno)); modbus_free(ctx); modbus_close(ctx); usleep(100000); // Sleep for suggested delay time of 100ms // Closing connection to retry again goto modbus_side_restart; } else // Connection to server successful { fprintf(stderr, "Connection to server successful\n"); } // Read registers while(1) { rc = modbus_read_registers(ctx, 48, 2, tab_reg); // Device 48 allocated for Nick Hodson if(rc == -1) // Read of registers failed { fprintf(stderr, "Reading of registers has failed: %s\n", modbus_strerror(errno)); // CLose modbus connection and start again (retry) modbus_free(ctx); modbus_close(ctx); goto modbus_side_restart; } // not putting an else statement for succcessful read register as it will clog up the terminal window for(i = 0; i < rc; i++) // Register display { add_to_list(&list_heads[i], tab_reg[i]); // replacement for printf statement printf("register[%d] = %d (0x%X)\n", i, tab_reg[i], tab_reg[i]); } usleep(100000); // Sleep for suggested delay time of 100ms } }
//========================================================================================== int MainWindow::ReadTimeFromICPDAS(QString IPaddr, QDateTime *icpDT) { int res; modbus_t *mb; uint16_t tab_reg[200]; const uint number_readed_registers=6; uint year=0; uint month=0; uint day=0; uint hour=0; uint minute=0; uint second=0; //inialized context mb = modbus_new_tcp(IPaddr.toStdString().c_str(), 502); if (mb==NULL) {return -1;} res=modbus_connect(mb); modbus_set_slave(mb, 1); if (res==-1) {modbus_free(mb); return -2;} res=modbus_read_registers(mb, 1, number_readed_registers, tab_reg);// 6 reg from 40002 if (res!=number_readed_registers){modbus_close(mb);modbus_free(mb); return -3;} year=tab_reg[0]; month=tab_reg[1]; day=tab_reg[2]; hour=tab_reg[3]; minute=tab_reg[4]; second=tab_reg[5]; QDate dt; dt.setDate(year,month,day); QTime tm; tm.setHMS(hour,minute,second); icpDT->setDate(dt); icpDT->setTime(tm); modbus_close(mb); modbus_free(mb); return 1; }
modbus_t* modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit) { modbus_t *ctx; modbus_rtu_t *ctx_rtu; size_t dest_size; size_t ret_size; ctx = (modbus_t *) malloc(sizeof(modbus_t)); _modbus_init_common(ctx); ctx->backend = &_modbus_rtu_backend; ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t)); ctx_rtu = (modbus_rtu_t *)ctx->backend_data; dest_size = sizeof(ctx_rtu->device); ret_size = strlcpy(ctx_rtu->device, device, dest_size); printf("dest_size = %d\n", dest_size); printf("device_size = %d\n", strlen(device)); if (ret_size == 0) { fprintf(stderr, "The device string is empty\n"); modbus_free(ctx); errno = EINVAL; return NULL; } if (ret_size >= dest_size) { fprintf(stderr, "The device string has been truncated\n"); modbus_free(ctx); errno = EINVAL; return NULL; } ctx_rtu->baud = baud; if (parity == 'N' || parity == 'E' || parity == 'O') { ctx_rtu->parity = parity; } else { modbus_free(ctx); errno = EINVAL; return NULL; } ctx_rtu->data_bit = data_bit; ctx_rtu->stop_bit = stop_bit; return ctx; }
void quit_cleanup(void) { char *fnct_name = "quit_cleanup"; int counter, ret; DBG(gbl.init_dbg, "started"); for (counter = 0; counter < gbl.tot_mb_links; counter++) { if (gbl.mb_links[counter].modbus != NULL) { modbus_close(gbl.mb_links[counter].modbus); modbus_free(gbl.mb_links[counter].modbus); gbl.mb_links[counter].modbus = NULL; } } gbl.tot_mb_links = 0; if (gbl.mb_tx != NULL) { free(gbl.mb_tx); } gbl.mb_tx = NULL; gbl.tot_mb_tx = 0; if (gbl.hal_mod_id >= 0) { ret = hal_exit(gbl.hal_mod_id); DBG(gbl.init_dbg, "unloading HAL module [%d] ret[%d]", gbl.hal_mod_id, ret); } DBG(gbl.init_dbg, "done OK"); }
//open serial port bool EnergyCamOpen(unsigned int dwPort ){ m_dwPort=dwPort; char comDeviceName[100]; sprintf(comDeviceName, "/dev/ttyUSB%d", dwPort); m_ctx = (modbus_t* )modbus_new_rtu(comDeviceName, m_dwBaud, 'E', 8, 1); // parity 'E'ven used by EnergyCam's freemodbus implementation if(NULL == m_ctx) return false; modbus_set_debug( m_ctx, false); modbus_set_slave( m_ctx, 1); if (modbus_connect(m_ctx) == -1) { fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); modbus_free(m_ctx); m_ctx = NULL; return false; } else{ printf,( "Connection OK: \n"); } return true; }
void ConnectionModbusPoint::closeConnection() { if (ctx != NULL) { modbus_close(ctx); modbus_free(ctx); } }
void close_modebus() { modbus_close(ctx); modbus_free(ctx); printf("Modbus closed\n"); }
int MODBUSPuerto::abrir(){ log.info("%s: %s %s",__FILE__, "Comienza abrir puerto..",this->getName().data()); int res = 1 ; //struct timeval response_timeout; //response_timeout.tv_sec = 1; //response_timeout.tv_usec = 0; errno= 0; if ((ctx = modbus_new_rtu(this->getName().data(), baudrate, 'N', 8, 1)) != NULL){ log.error("%s: %s %s",__FILE__, "Set slave error: ",modbus_strerror(errno)); modbus_set_slave(ctx, 0x01); //log.error("%s: %s %s",__FILE__, "Set slave error: ",modbus_strerror(errno)); //modbus_set_response_timeout(ctx, &response_timeout); if (errno != 0) log.error("%s: %s %s",__FILE__, "Set timeout error",modbus_strerror(errno)); if (modbus_connect(ctx) == -1) { log.error("%s: %s %s",__FILE__, "Error al abrir puerto!",modbus_strerror(errno)); modbus_free(ctx); res = 1; this->isOpen = false ; } else { this->isOpen = true ; res = 0 ; log.info("%s: %s",__FILE__, "Puerto abierto!!"); } } return res ; }
ModbusClientV1::ModbusClientV1(char* device) { ctx = modbus_new_rtu(device, 115200, 'E', 8, 1); // Port_name, Speed, Parity, Data_bits, Stop_bits if (ctx == NULL) { fprintf(stderr, "Unable to create the libmodbus context\n"); return; } if (verbose > 4) { modbus_set_debug(ctx, 1); } int rc = modbus_set_slave(ctx, 1); // Connecting to existing modbus socket rc = modbus_connect(ctx); if (rc == -1) { fprintf(stderr, "Connection failed [%s]\n", modbus_strerror(errno)); modbus_free(ctx); return; } // Change bytes and response timeouts for connections change_byte_timeout(0, 10000); /* 10 ms */ change_response_timeout(0, 100000); /* 100 ms */ isInitialized = true; }
bool MainForm::checkModbus(){ if(m_modbus!=NULL){ modbus_close( m_modbus); modbus_free( m_modbus ); m_modbus = NULL; } char parity; if(Config::serialParity=="even"){ parity = 'E'; }else if(Config::serialParity=="odd"){ parity = 'O'; }else{ parity = 'N'; } m_modbus = modbus_new_rtu(Config::serialinterface.toAscii().constData(), Config::serialBaudRate, parity,Config::serialDatabits,Config::serialStopbits ); int re= modbus_connect( m_modbus ); // qDebug()<<"re:"<<re<<endl; return re != -1; }
/* Quit and destroy modbus-connection */ int modbus_quit(modbus_t *ctx) { modbus_close(ctx); modbus_free(ctx); return SUCCESS; }
modbus_t * ModbusMaster::openPort(QString ip, quint16 port) { modbus_t * conn; conn = modbus_new_tcp(ip.toStdString().c_str(), port); if (conn) { if (modbus_connect(conn) == -1) { #ifdef QT_DEBUG_OUTPUT qDebug() << "Connection failed: " << modbus_strerror(errno) << endl; #endif modbus_free(conn); conn = 0; } } else { #ifdef QT_DEBUG_OUTPUT qDebug() << "New TCP failed: " << modbus_strerror(errno) << endl; #endif conn = 0; } return conn; }
//free resources bool EnergyCamClose(void){ if(NULL != m_ctx){ modbus_close(m_ctx); modbus_free(m_ctx); m_ctx = NULL; } return true; }
static void close_sigint(int dummy) { close(server_socket); modbus_free(ctx); modbus_mapping_free(mb_mapping); exit(dummy); }
static void closeSerial() { if (mb) { modbus_close(mb); modbus_free(mb); mb = NULL; } }
int main(void) { int s = -1; modbus_t *ctx; modbus_mapping_t *mb_mapping; ctx = modbus_new_tcp("127.0.0.1", 1502); /* modbus_set_debug(ctx, TRUE); */ mb_mapping = modbus_mapping_new(500, 500, 500, 500); if (mb_mapping == NULL) { fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno)); modbus_free(ctx); return -1; } s = modbus_tcp_listen(ctx, 1); modbus_tcp_accept(ctx, &s); for (;;) { uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; int rc; rc = modbus_receive(ctx, query); if (rc > 0) { /* rc is the query size */ modbus_reply(ctx, query, rc, mb_mapping); } else if (rc == -1) { /* Connection closed by the client or error */ break; } } printf("Quit the loop: %s\n", modbus_strerror(errno)); if (s != -1) { close(s); } modbus_mapping_free(mb_mapping); modbus_close(ctx); modbus_free(ctx); return 0; }
void TcpIpSettingsWidget::releaseTcpModbus() { if( m_tcpModbus ) { modbus_close( m_tcpModbus ); modbus_free( m_tcpModbus ); m_tcpModbus = NULL; } }
/* Version 2.9.2 */ static int mb_init_connection (mb_host_t *host) /* {{{ */ { int status; if (host == NULL) return (EINVAL); if (host->connection != NULL) return (0); if (host->conntype == MBCONN_TCP) { if ((host->port < 1) || (host->port > 65535)) host->port = MODBUS_TCP_DEFAULT_PORT; DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.", host->node, host->port); host->connection = modbus_new_tcp (host->node, host->port); if (host->connection == NULL) { ERROR ("Modbus plugin: Creating new Modbus/TCP object failed."); return (-1); } } else { DEBUG ("Modbus plugin: Trying to connect to \"%s\", baudrate %i.", host->node, host->baudrate); host->connection = modbus_new_rtu (host->node, host->baudrate, 'N', 8, 1); if (host->connection == NULL) { ERROR ("Modbus plugin: Creating new Modbus/RTU object failed."); return (-1); } } #if COLLECT_DEBUG modbus_set_debug (host->connection, 1); #endif /* We'll do the error handling ourselves. */ modbus_set_error_recovery (host->connection, 0); status = modbus_connect (host->connection); if (status != 0) { ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.", host->node, host->port ? host->port : host->baudrate, status); modbus_free (host->connection); host->connection = NULL; return (status); } return (0); } /* }}} int mb_init_connection */
int main(int argc, char *argv[]) { uint16_t regs[1]; float heaterTemp; float heaterSetPoint; char * heaterEnableState; char * heaterElementState; char * modbusClientIP = DEFAULT_CLIENT_IP; int modbusPort = DEFAULT_PORT; int packet_counter = 0; uint8_t *tab_bits; uint16_t *tab_input_value_registers; tab_bits = (uint8_t *)malloc(NO_BITS * sizeof(uint8_t)); memset(tab_bits, 0, NO_BITS * sizeof(uint8_t)); tab_input_value_registers = (uint16_t *)malloc(NO_REGS* sizeof(uint16_t)); memset(tab_input_value_registers, 0, NO_REGS * sizeof(uint16_t)); bool sts; int rc; uint8_t coils[2]; ctx = modbus_new_tcp(modbusClientIP, modbusPort); if (ctx == NULL) { fprintf(stderr, "Unable to allocate libmodbus context\n"); return -1; } if (modbus_connect(ctx) == -1) { sts = false; } rc = modbus_read_input_registers(ctx,0,1, regs); packet_counter++; printf("Packet No: %d, Value of current:%d\n",packet_counter, regs[0]); modbus_close(ctx); ctx = modbus_new_tcp(modbusClientIP, modbusPort); if (ctx == NULL) { fprintf(stderr, "Unable to allocate libmodbus context\n"); return -1; } if (modbus_connect(ctx) == -1) { sts = false; } modbus_close(ctx); modbus_free(ctx); return 0; }
int main(int argc, char *arg[]) { modbus_t *ctx, *dup_ctx; int server_fd; pthread_t tcp_thread; file_read_random_data(RANDOM_DATA_POOL); if ((ctx = modbus_new_tcp("SERVER", MODBUS_TCP_DEFAULT_PORT)) == NULL) { printf("Failied to initialise modbus\n"); return -1; } mb_mapping = modbus_mapping_new(0, 0, file_get_highest_channel(), 0); listen: if ((server_fd = modbus_tcp_listen(ctx, LISTEN_BACKLOG)) < 0) { printf("Failed to initiate modbus_tcp server, %i\n", server_fd); printf("Root permissions are needed to open ports below 1024\n"); printf("Currently configured to open port %i\n", MODBUS_TCP_DEFAULT_PORT); modbus_free(ctx); return -1; } while (1) { modbus_set_debug(ctx, TRUE); if (modbus_tcp_accept(ctx, &server_fd) < 0) { /* libmodbus closes the supplied socket if accept fails - this may * be making new connections impossible */ printf("Accept failed \n"); goto listen; } modbus_set_debug(ctx, FALSE); dup_ctx = malloc(sizeof(struct ctx_data)); memcpy(dup_ctx, ctx, sizeof(struct ctx_data)); pthread_create(&tcp_thread, 0, connection, dup_ctx); } modbus_mapping_free(mb_mapping); close(server_fd); modbus_free(ctx); return 0; }
int main(int argc, char *argv[]) { uint16_t *tab_rp_registers = NULL; modbus_t *ctx = NULL; uint32_t sec_to = 1; uint32_t usec_to = 0; int i; int rc; int nb_points = 1; ctx = modbus_new_rtu(SERIAL_PORT, BAUD_RATE, PARITY, BYTE_SIZE, STOP_BITS); if (ctx == NULL) { fprintf(stderr, "Unable to allocate libmodbus context\n"); return -1; } 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_pin(ctx,RPI_PIN); modbus_rpi_pin_export_direction(ctx); //modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec); if (modbus_connect(ctx) == -1) { fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); modbus_free(ctx); return -1; } // modbus_get_response_timeout(ctx, &new_response_to_sec, &new_response_to_usec); /* Allocate and initialize the memory to store the registers */ tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t)); memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t)); rc = modbus_read_registers(ctx, 97,1, 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); }
RbCtrlIface::~RbCtrlIface() { if( mModbus ) { modbus_close( mModbus ); modbus_free( mModbus ); } if(mReplyBuffer) delete [] mReplyBuffer; }
int MeterModbus::close() { /*struct addressparam *addressptr = _addressparams; while(addressptr->function_code != 0xFF){ print(log_debug, "Freeing Address %p", "", addressptr->recalc_str); delete[] addressptr->recalc_str; addressptr++; } free((void *)_addressparams);*/ modbus_close(_mb); modbus_free(_mb); return 0; }