/* Return a DTP41 error code */ static int dtp41_fcommand( dtp41 *p, char *in, /* In string */ char *out, /* Out string buffer */ int bsize, /* Out buffer size */ char *tc, /* Terminating characters */ int ntc, /* Number of terminating characters */ double to) { /* Timout in seconts */ int rv, se; if ((se = p->icom->write_read(p->icom, in, 0, out, bsize, NULL, tc, ntc, to)) != 0) { a1logd(p->log, 1, "dtp41_fcommand: serial i/o failure 0x%x on write_read '%s'\n",se,icoms_fix(in)); return icoms2dtp41_err(se); } rv = DTP41_OK; if (tc[0] == '>' && ntc == 1) { rv = extract_ec(out); if (rv > 0) { rv &= inst_imask; if (rv != DTP41_OK) { /* Clear the error */ char buf[MAX_MES_SIZE]; p->icom->write_read(p->icom, "CE\r", 0, buf, MAX_MES_SIZE, NULL, ">", 1, 0.5); } } } a1logd(p->log, 4, "dtp41_fcommand: command '%s' returned '%s', value 0x%x\n", icoms_fix(in), icoms_fix(out),rv); return rv; }
/* Return a DTP51 error code */ static int dtp51_fcommand( struct _dtp51 *p, char *in, /* In string */ char *out, /* Out string buffer */ int bsize, /* Out buffer size */ char tc, /* Terminating character */ int ntc, /* Number of terminating characters */ double to) { /* Timout in seconts */ int rv, se; if ((se = p->icom->write_read(p->icom, in, out, bsize, tc, ntc, to)) != 0) { #ifdef DEBUG printf("dtp51 fcommand: serial i/o failure on write_read '%s'\n",icoms_fix(in)); #endif return icoms2dtp51_err(se); } rv = DTP51_OK; if (tc == '>' && ntc == 1) { rv = extract_ec(out); if (rv > 0) { rv &= inst_imask; if (rv != DTP51_OK) { /* Clear the error */ char buf[MAX_MES_SIZE]; p->icom->write_read(p->icom, "CE\r", buf, MAX_MES_SIZE, '>', 1, 0.5); } } } #ifdef DEBUG printf("command '%s'",icoms_fix(in)); printf(" returned '%s', value 0x%x\n",icoms_fix(out),rv); #endif return rv; }
/* Return DTP41_COMS_FAIL on failure to establish communications */ static inst_code dtp41_init_coms(inst *pp, baud_rate br, flow_control fc, double tout) { dtp41 *p = (dtp41 *)pp; static char buf[MAX_MES_SIZE]; baud_rate brt[9] = { baud_9600, baud_19200, baud_38400, baud_57600, baud_4800, baud_2400, baud_1200, baud_600, baud_300 }; char *brc[9] = { "9600BR\r", "19200BR\r", "38400BR\r", "57600BR\r", "4800BR\r", "2400BR\r", "1200BR\r", "600BR\r", "300BR\r" }; char *fcc; unsigned int etime; int ci, bi, i, se; inst_code ev = inst_ok; a1logd(p->log, 2, "dtp41_init_coms: About to init Serial I/O\n"); /* Deal with flow control setting */ if (fc == fc_nc) fc = DEFFC; if (fc == fc_XonXOff) { fcc = "0304CF\r"; } else if (fc == fc_Hardware) { fcc = "0104CF\r"; } else { fc = fc_none; fcc = "0004CF\r"; } /* Figure DTP41 baud rate being asked for */ for (bi = 0; bi < 9; bi++) { if (brt[bi] == br) break; } if (bi >= 9) bi = 0; /* Figure current icoms baud rate */ for (ci = 0; ci < 9; ci++) { if (brt[ci] == p->icom->br) break; } if (ci >= 9) ci = bi; /* The tick to give up on */ etime = msec_time() + (long)(1000.0 * tout + 0.5); while (msec_time() < etime) { /* Until we time out, find the correct baud rate */ for (i = ci; msec_time() < etime;) { if ((se = p->icom->set_ser_port(p->icom, fc_none, brt[i], parity_none, stop_1, length_8)) != ICOM_OK) { a1logd(p->log, 1, "dtp41_init_coms: set_ser_port failed ICOM err 0x%x\n",se); return dtp41_interp_code((inst *)p, icoms2dtp41_err(se)); } if (((ev = dtp41_command(p, "\r", buf, MAX_MES_SIZE, 0.5)) & inst_mask) != inst_coms_fail) break; /* We've got coms */ /* Check for user abort */ if (p->uicallback != NULL) { inst_code ev; if ((ev = p->uicallback(p->uic_cntx, inst_negcoms)) == inst_user_abort) { a1logd(p->log, 1, "dtp41_init_coms: user aborted\n"); return inst_user_abort; } } if (++i >= 9) i = 0; } break; /* Got coms */ } if (msec_time() >= etime) { /* We haven't established comms */ return inst_coms_fail; } /* set the protocol to RCI */ if ((ev = dtp41_command(p, "0012CF\r", buf, MAX_MES_SIZE, 1.5)) != inst_ok) return ev; /* Set the handshaking (cope with coms breakdown) */ if ((se = p->icom->write_read(p->icom, fcc, 0, buf, MAX_MES_SIZE, NULL, ">", 1, 1.5)) != 0) { if (extract_ec(buf) != DTP41_OK) return inst_coms_fail; } /* Change the baud rate to the rate we've been told (cope with coms breakdown) */ if ((se = p->icom->write_read(p->icom, brc[bi], 0, buf, MAX_MES_SIZE, NULL, ">", 1, 1.5)) != 0) { if (extract_ec(buf) != DTP41_OK) return inst_coms_fail; } /* Configure our baud rate and handshaking as well */ if ((se = p->icom->set_ser_port(p->icom, fc, brt[bi], parity_none, stop_1, length_8)) != ICOM_OK) { a1logd(p->log, 1, "dtp41_init_coms: set_ser_port failed ICOM err 0x%x\n",se); return dtp41_interp_code((inst *)p, icoms2dtp41_err(se)); } /* Loose a character (not sure why) */ p->icom->write_read(p->icom, "\r", 0, buf, MAX_MES_SIZE, NULL, ">", 1, 0.5); /* Check instrument is responding */ if ((ev = dtp41_command(p, "\r", buf, MAX_MES_SIZE, 1.5)) != inst_ok) { a1logd(p->log, 1, "dtp41_init_coms: instrument failed to respond\n"); return inst_coms_fail; } a1logd(p->log, 2, "dtp41_init_coms: init coms has suceeded\n"); p->gotcoms = 1; return inst_ok; }
/* Return DTP_COMS_FAIL on failure to establish communications */ static inst_code dtp51_init_coms(inst *pp, int port, baud_rate br, flow_control fc, double tout) { dtp51 *p = (dtp51 *)pp; static char buf[MAX_MES_SIZE]; baud_rate brt[5] = { baud_9600, baud_19200, baud_4800, baud_2400, baud_1200 }; char *brc[5] = { "30BR\r", "60BR\r", "18BR\r", "0CBR\r", "06BR\r" }; char *fcc; long etime; int ci, bi, i, rv; inst_code ev = inst_ok; if (p->debug) p->icom->debug = p->debug; /* Turn on debugging */ /* Deal with flow control setting */ if (fc == fc_nc) fc = DEFFC; if (fc == fc_XonXOff) { fcc = "0304CF\r"; } else if (fc == fc_Hardware) { fcc = "0104CF\r"; } else { fc = fc_none; fcc = "0004CF\r"; } /* Figure DTP51 baud rate being asked for */ for (bi = 0; bi < 5; bi++) { if (brt[bi] == br) break; } if (bi >= 5) bi = 0; /* Figure current icoms baud rate */ for (ci = 0; ci < 5; ci++) { if (brt[ci] == p->icom->br) break; } if (ci >= 5) ci = bi; /* The tick to give up on */ etime = msec_time() + (long)(1000.0 * tout + 0.5); while (msec_time() < etime) { /* Until we time out, find the correct baud rate */ for (i = ci; msec_time() < etime;) { p->icom->set_ser_port(p->icom, port, fc_none, brt[i], parity_none, stop_1, length_8); if (((ev = dtp51_command(p, "\r", buf, MAX_MES_SIZE, 0.5)) & inst_mask) != inst_coms_fail) break; /* We've got coms or user abort */ if (++i >= 5) i = 0; } if ((ev & inst_mask) == inst_user_abort) return ev; break; /* Got coms */ } if (msec_time() >= etime) { /* We haven't established comms */ return inst_coms_fail; } /* Set the handshaking */ if ((ev = dtp51_command(p, fcc, buf, MAX_MES_SIZE, 1.5)) != inst_ok) return ev; /* Change the baud rate to the rate we've been told */ if ((rv = p->icom->write_read(p->icom, brc[bi], buf, MAX_MES_SIZE, '>', 1, 1.5)) != 0) { if (extract_ec(buf) != DTP51_OK) return inst_coms_fail; } /* Configure our baud rate and handshaking as well */ p->icom->set_ser_port(p->icom, port, fc, brt[bi], parity_none, stop_1, length_8); /* Loose a character (not sure why) */ p->icom->write_read(p->icom, "\r", buf, MAX_MES_SIZE, '>', 1, 0.5); /* Check instrument is responding */ if ((ev = dtp51_command(p, "\r", buf, MAX_MES_SIZE, 1.5)) != inst_ok) return inst_coms_fail; if (p->verb) { int i, j; if ((ev = dtp51_command(p, "GI\r", buf, MAX_MES_SIZE, 0.2)) != inst_ok && (ev & inst_imask) != DTP51_BAD_COMMAND) return ev; if ((ev & inst_imask) == DTP51_BAD_COMMAND) { /* Some fimware doesn't support GI */ printf("Firware doesn't support GI command\n"); } else { for (j = i = 0; ;i++) { if (buf[i] == '<' || buf[i] == '\000') break; if (buf[i] == '\r') { buf[i] = '\000'; printf(" %s\n",&buf[j]); if (buf[i+1] == '\n') i++; j = i+1; } } } } #ifdef DEBUG printf("Got communications\n"); #endif p->gotcoms = 1; return inst_ok; }