int connect_pvs (pv* pvs, int nPvs) { int returncode = create_pvs ( pvs, nPvs, 0); if ( returncode == 0 ) { /* Wait for channels to connect */ int result = ca_pend_io (caTimeout); if (result == ECA_TIMEOUT) { if (nPvs > 1) { fprintf(stderr, "Channel connect timed out: some PV(s) not found.\n"); } else { fprintf(stderr, "Channel connect timed out: '%s' not found.\n", pvs[0].name); } returncode = 1; } } return returncode; }
int main (int argc, char *argv[]) { int returncode = 0; int n = 0; int result; /* CA result */ int opt; /* getopt() current option */ int digits = 0; /* getopt() no. of float digits */ int nPvs; /* Number of PVs */ pv* pvs = 0; /* Array of PV structures */ setvbuf(stdout,NULL,_IOLBF,BUFSIZ); /* Set stdout to line buffering */ while ((opt = getopt(argc, argv, ":nhm:se:f:g:#:d:0:w:t:")) != -1) { switch (opt) { case 'h': /* Print usage */ usage(); return 0; case 'n': /* Print ENUM as index numbers */ enumAsNr=1; break; case 't': /* Select timestamp source(s) and type */ tsSrcServer = 0; tsSrcClient = 0; { int i = 0; char c; while ((c = optarg[i++])) switch (c) { case 's': tsSrcServer = 1; break; case 'c': tsSrcClient = 1; break; case 'n': break; case 'r': tsType = relative; break; case 'i': tsType = incremental; break; case 'I': tsType = incrementalByChan; break; default : fprintf(stderr, "Invalid argument '%c' " "for option '-t' - ignored.\n", c); } } break; case 'w': /* Set CA timeout value */ if(epicsScanDouble(optarg, &caTimeout) != 1) { fprintf(stderr, "'%s' is not a valid timeout value " "- ignored. ('caget -h' for help.)\n", optarg); caTimeout = DEFAULT_TIMEOUT; } break; case '#': /* Array count */ if (sscanf(optarg,"%ld", &reqElems) != 1) { fprintf(stderr, "'%s' is not a valid array element count " "- ignored. ('caget -h' for help.)\n", optarg); reqElems = 0; } break; case 'm': /* Select CA event mask */ eventMask = 0; { int i = 0; char c, err = 0; while ((c = optarg[i++]) && !err) switch (c) { case 'v': eventMask |= DBE_VALUE; break; case 'a': eventMask |= DBE_ALARM; break; case 'l': eventMask |= DBE_LOG; break; default : fprintf(stderr, "Invalid argument '%s' " "for option '-m' - ignored.\n", optarg); eventMask = DBE_VALUE | DBE_ALARM; err = 1; } } break; case 's': /* Select string dbr for floating type data */ floatAsString = 1; break; case 'e': /* Select %e/%f/%g format, using <arg> digits */ case 'f': case 'g': if (sscanf(optarg, "%d", &digits) != 1) fprintf(stderr, "Invalid precision argument '%s' " "for option '-%c' - ignored.\n", optarg, opt); else { if (digits>=0 && digits<=VALID_DOUBLE_DIGITS) sprintf(dblFormatStr, "%%-.%d%c", digits, opt); else fprintf(stderr, "Precision %d for option '-%c' " "out of range - ignored.\n", digits, opt); } break; case '0': /* Select integer format */ switch ((char) *optarg) { case 'x': outType = hex; break; /* 0x print Hex */ case 'b': outType = bin; break; /* 0b print Binary */ case 'o': outType = oct; break; /* 0o print Octal */ default : fprintf(stderr, "Invalid argument '%s' " "for option '-0' - ignored.\n", optarg); } break; case '?': fprintf(stderr, "Unrecognized option: '-%c'. ('caget -h' for help.)\n", optopt); return 1; case ':': fprintf(stderr, "Option '-%c' requires an argument. ('caget -h' for help.)\n", optopt); return 1; default : usage(); return 1; } } nPvs = argc - optind; /* Remaining arg list are PV names */ if (nPvs < 1) { fprintf(stderr, "No pv name specified. ('camonitor -h' for help.)\n"); return 1; } /* Start up Channel Access */ result = ca_context_create(ca_disable_preemptive_callback); if (result != ECA_NORMAL) { fprintf(stderr, "CA error %s occurred while trying " "to start channel access '%s'.\n", ca_message(result), pvs[n].name); return 1; } /* Allocate PV structure array */ pvs = calloc (nPvs, sizeof(pv)); if (!pvs) { fprintf(stderr, "Memory allocation for channel structures failed.\n"); return 1; } /* Connect channels */ /* Copy PV names from command line */ for (n = 0; optind < argc; n++, optind++) { pvs[n].name = argv[optind]; } /* Create CA connections */ returncode = create_pvs(pvs, nPvs, connection_handler); if ( returncode ) { return returncode; } /* Check for channels that didn't connect */ ca_pend_event(caTimeout); for (n = 0; n < nPvs; n++) { if (!pvs[n].onceConnected) print_time_val_sts(&pvs[n], pvs[n].reqElems); } /* Read and print data forever */ ca_pend_event(0); /* Shut down Channel Access */ ca_context_destroy(); return result; }