/* Version 2.0.3 */ static int mb_init_connection (mb_host_t *host) /* {{{ */ { int status; if (host == NULL) return (EINVAL); #if COLLECT_DEBUG modbus_set_debug (&host->connection, 1); #endif /* We'll do the error handling ourselves. */ modbus_set_error_handling (&host->connection, NOP_ON_ERROR); 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); modbus_init_tcp (&host->connection, /* host = */ host->node, /* port = */ host->port); } else /* MBCONN_RTU */ { DEBUG ("Modbus plugin: Trying to connect to \"%s\".", host->node); modbus_init_rtu (&host->connection, /* device = */ host->node, /* baudrate = */ host->baudrate, 'N', 8, 1, 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); return (status); } host->is_connected = 1; return (0); } /* }}} int mb_init_connection */
int main(int argc, char** argv) { modbus_param_t mb_param; int half_duplex = 0; int c; char ** svp; int i; int set_args_index = -1; char set_args[MAX_SETS][MAX_STR]; char *device = NULL; memset(set_args, 0, sizeof(set_args)); while( (c = getopt(argc, argv, "hnvd:s:")) != -1) { switch(c){ case 's': /* NOTE IMPORTANT! This block MUST COME FIRST in the switch, before any opts, otherwise GCC or getopts or something gets the indexes all wrong */ // Thanks to http://stackoverflow.com/questions/3939157/c-getopt-multiple-value // Special case the first arg set_args_index = 0; strcpy(set_args[0], optarg); // Loop the rest i=1; while(optind < argc && *argv[optind] != '-'){ strcpy(set_args[i], argv[optind]); if(debug > 0){ printf("getopts multiopt: %s %s\n", argv[optind], set_args[i] ); } // Ugly but explicit set_args_index++; i++; optind++; } break; case 'h': half_duplex = 1; break; case 'v': debug = 1; break; case 'n': dry = 1; break; case 'd': device = optarg; default: break; } } if(!device){ printf("need to give serial port on command line\n"); usage(); return(1); } /* Setup the serial port parameters */ modbus_init_rtu(&mb_param, device, 9600, "none", 8, 2, half_duplex); if(debug > 0){ modbus_set_debug(&mb_param, TRUE); } /* Open the MODBUS connection */ if (modbus_connect(&mb_param) == -1) { printf("ERROR Connection failed\n"); return(1); } if(debug > 0){ printf("We have %d set args to process...\n", set_args_index + 1); } i = set_args_index; while(i >= 0){ printf("\nSetting values... %s\n", set_args[i]); set_options(&mb_param, set_args[i]); i--; } if(dry < 1 && set_args_index >= 0){ commit_options(&mb_param); } if(dry < 1){ read_values(&mb_param); } /* Close the MODBUS connection */ modbus_close(&mb_param); return(0); }
int main(int argc, char **argv) { int retval; modbus_param_t mb_param; haldata_t *haldata; slavedata_t slavedata; int hal_comp_id; struct timespec loop_timespec, remaining; int baud, bits, stopbits, debug, verbose; char *device, *parity, *endarg; int opt; int argindex, argvalue; done = 0; // assume that nothing is specified on the command line baud = 38400; bits = 8; stopbits = 1; debug = 0; verbose = 0; device = "/dev/ttyS0"; parity = "odd"; /* slave / register info */ slavedata.slave = 1; slavedata.read_reg_start = START_REGISTER_R; slavedata.read_reg_count = NUM_REGISTERS_R; slavedata.write_reg_start = START_REGISTER_W; slavedata.write_reg_count = NUM_REGISTERS_R; // process command line options while ((opt=getopt_long(argc, argv, option_string, long_options, NULL)) != -1) { switch(opt) { case 'b': // serial data bits, probably should be 8 (and defaults to 8) argindex=match_string(optarg, bitstrings); if (argindex<0) { printf("gs2_vfd: ERROR: invalid number of bits: %s\n", optarg); retval = -1; goto out_noclose; } bits = atoi(bitstrings[argindex]); break; case 'd': // device name, default /dev/ttyS0 // could check the device name here, but we'll leave it to the library open if (strlen(optarg) > FILENAME_MAX) { printf("gs2_vfd: ERROR: device node name is too long: %s\n", optarg); retval = -1; goto out_noclose; } device = strdup(optarg); break; case 'g': debug = 1; verbose = 1; break; case 'n': // module base name if (strlen(optarg) > HAL_NAME_LEN-20) { printf("gs2_vfd: ERROR: HAL module name too long: %s\n", optarg); retval = -1; goto out_noclose; } modname = strdup(optarg); break; case 'p': // parity, should be a string like "even", "odd", or "none" argindex=match_string(optarg, paritystrings); if (argindex<0) { printf("gs2_vfd: ERROR: invalid parity: %s\n", optarg); retval = -1; goto out_noclose; } parity = paritystrings[argindex]; break; case 'r': // Baud rate, 38400 default argindex=match_string(optarg, ratestrings); if (argindex<0) { printf("gs2_vfd: ERROR: invalid baud rate: %s\n", optarg); retval = -1; goto out_noclose; } baud = atoi(ratestrings[argindex]); break; case 's': // stop bits, defaults to 1 argindex=match_string(optarg, stopstrings); if (argindex<0) { printf("gs2_vfd: ERROR: invalid number of stop bits: %s\n", optarg); retval = -1; goto out_noclose; } stopbits = atoi(stopstrings[argindex]); break; case 't': // target number (MODBUS ID), default 1 argvalue = strtol(optarg, &endarg, 10); if ((*endarg != '\0') || (argvalue < 1) || (argvalue > 254)) { printf("gs2_vfd: ERROR: invalid slave number: %s\n", optarg); retval = -1; goto out_noclose; } slavedata.slave = argvalue; break; case 'v': // verbose mode (print modbus errors and other information), default 0 verbose = 1; break; case 'h': default: usage(argc, argv); exit(0); break; } } printf("%s: device='%s', baud=%d, bits=%d, parity='%s', stopbits=%d, address=%d, verbose=%d\n", modname, device, baud, bits, parity, stopbits, slavedata.slave, debug); /* point TERM and INT signals at our quit function */ /* if a signal is received between here and the main loop, it should prevent some initialization from happening */ signal(SIGINT, quit); signal(SIGTERM, quit); /* Assume 38.4k O-8-1 serial settings, device 1 */ modbus_init_rtu(&mb_param, device, baud, parity, bits, stopbits, verbose); mb_param.debug = debug; /* the open has got to work, or we're out of business */ if (((retval = modbus_connect(&mb_param))!=0) || done) { printf("%s: ERROR: couldn't open serial device\n", modname); goto out_noclose; } /* create HAL component */ hal_comp_id = hal_init(modname); if ((hal_comp_id < 0) || done) { printf("%s: ERROR: hal_init failed\n", modname); retval = hal_comp_id; goto out_close; } /* grab some shmem to store the HAL data in */ haldata = (haldata_t *)hal_malloc(sizeof(haldata_t)); if ((haldata == 0) || done) { printf("%s: ERROR: unable to allocate shared memory\n", modname); retval = -1; goto out_close; } retval = hal_pin_s32_newf(HAL_OUT, &(haldata->stat1), hal_comp_id, "%s.status-1", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_s32_newf(HAL_OUT, &(haldata->stat2), hal_comp_id, "%s.status-2", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_float_newf(HAL_OUT, &(haldata->freq_cmd), hal_comp_id, "%s.frequency-command", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_float_newf(HAL_OUT, &(haldata->freq_out), hal_comp_id, "%s.frequency-out", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_float_newf(HAL_OUT, &(haldata->curr_out), hal_comp_id, "%s.output-current", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_float_newf(HAL_OUT, &(haldata->DCBusV), hal_comp_id, "%s.DC-bus-volts", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_float_newf(HAL_OUT, &(haldata->outV), hal_comp_id, "%s.output-voltage", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_float_newf(HAL_OUT, &(haldata->RPM), hal_comp_id, "%s.motor-RPM", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_float_newf(HAL_OUT, &(haldata->scale_freq), hal_comp_id, "%s.scale-frequency", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_float_newf(HAL_OUT, &(haldata->power_factor), hal_comp_id, "%s.power-factor", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_float_newf(HAL_OUT, &(haldata->load_pct), hal_comp_id, "%s.load-percentage", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_s32_newf(HAL_OUT, &(haldata->FW_Rev), hal_comp_id, "%s.firmware-revision", modname); if (retval!=0) goto out_closeHAL; retval = hal_param_s32_newf(HAL_RW, &(haldata->errorcount), hal_comp_id, "%s.error-count", modname); if (retval!=0) goto out_closeHAL; retval = hal_param_float_newf(HAL_RW, &(haldata->looptime), hal_comp_id, "%s.loop-time", modname); if (retval!=0) goto out_closeHAL; retval = hal_param_s32_newf(HAL_RW, &(haldata->retval), hal_comp_id, "%s.retval", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_bit_newf(HAL_OUT, &(haldata->at_speed), hal_comp_id, "%s.at-speed", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_bit_newf(HAL_OUT, &(haldata->is_stopped), hal_comp_id, "%s.is-stopped", modname); // JET if (retval!=0) goto out_closeHAL; retval = hal_pin_float_newf(HAL_IN, &(haldata->speed_command), hal_comp_id, "%s.speed-command", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_bit_newf(HAL_IN, &(haldata->spindle_on), hal_comp_id, "%s.spindle-on", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_bit_newf(HAL_IN, &(haldata->spindle_fwd), hal_comp_id, "%s.spindle-fwd", modname); if (retval!=0) goto out_closeHAL; retval = hal_pin_bit_newf(HAL_IN, &(haldata->spindle_rev), hal_comp_id, "%s.spindle-rev", modname); //JET if (retval!=0) goto out_closeHAL; retval = hal_pin_bit_newf(HAL_IN, &(haldata->err_reset), hal_comp_id, "%s.err-reset", modname); if (retval!=0) goto out_closeHAL; retval = hal_param_float_newf(HAL_RW, &(haldata->speed_tolerance), hal_comp_id, "%s.tolerance", modname); if (retval!=0) goto out_closeHAL; retval = hal_param_float_newf(HAL_RW, &(haldata->motor_hz), hal_comp_id, "%s.nameplate-HZ", modname); if (retval!=0) goto out_closeHAL; retval = hal_param_float_newf(HAL_RW, &(haldata->motor_RPM), hal_comp_id, "%s.nameplate-RPM", modname); if (retval!=0) goto out_closeHAL; retval = hal_param_s32_newf(HAL_RW, &(haldata->ack_delay), hal_comp_id, "%s.ack-delay", modname); if (retval!=0) goto out_closeHAL; /* make default data match what we expect to use */ *(haldata->stat1) = 0; *(haldata->stat2) = 0; *(haldata->freq_cmd) = 0; *(haldata->freq_out) = 0; *(haldata->curr_out) = 0; *(haldata->DCBusV) = 0; *(haldata->outV) = 0; *(haldata->RPM) = 0; *(haldata->scale_freq) = 0; *(haldata->power_factor) = 0; *(haldata->load_pct) = 0; *(haldata->FW_Rev) = 0; haldata->errorcount = 0; haldata->looptime = 0.1; haldata->motor_RPM = 1730; haldata->motor_hz = 60; haldata->speed_tolerance = 0.01; haldata->ack_delay = 2; *(haldata->err_reset) = 0; *(haldata->spindle_on) = 0; *(haldata->spindle_fwd) = 1; *(haldata->spindle_rev) = 0; haldata->old_run = -1; // make sure the initial value gets output haldata->old_dir = -1; haldata->old_err_reset = -1; hal_ready(hal_comp_id); /* here's the meat of the program. loop until done (which may be never) */ while (done==0) { read_data(&mb_param, &slavedata, haldata); write_data(&mb_param, &slavedata, haldata); /* don't want to scan too fast, and shouldn't delay more than a few seconds */ if (haldata->looptime < 0.001) haldata->looptime = 0.001; if (haldata->looptime > 2.0) haldata->looptime = 2.0; loop_timespec.tv_sec = (time_t)(haldata->looptime); loop_timespec.tv_nsec = (long)((haldata->looptime - loop_timespec.tv_sec) * 1000000000l); nanosleep(&loop_timespec, &remaining); } retval = 0; /* if we get here, then everything is fine, so just clean up and exit */ out_closeHAL: hal_exit(hal_comp_id); out_close: modbus_close(&mb_param); out_noclose: return retval; }