Exemplo n.º 1
0
        //* <f> */
        //*============================================================================
        //*
        //* function name: sr_stop()
        //*
        //* purpose: stop and close shift register.
        //*
        //* return value: None.
        //*
        //* special notes:
        //*
        //* revision history:
        //*
        //*  date	author		revision
        //*  ----	------		--------
        //*  11/30/93	Bill Harker	created
        //*
        //*============================================================================*/

        public int sr_stop()
        {
            int status;

            log.TraceEvent(LogLevels.Info, 0x4f32D, "Stopping the shift register");
            status = SRLib.Control(dsid.SerialPort, sr_h.SR_STOP, null);
            if (status != sr_h.SR_NOTOPEN)
            {
                SRLib.Close(dsid.SerialPort);
                log.TraceEvent(LogLevels.Info, 0x4f32C, "Closing the serial port");
            }
            return(status);
        }
Exemplo n.º 2
0
        //* <f> */
        //*============================================================================
        //*
        //* function name: sr_start_counting()
        //*
        //* purpose: zero out the shift register counters and start a reading.
        //*
        //* return value: SR_SUCCESS/SR_TRY_AGAIN/FAIL
        //*
        //* special notes: if there is a failure, the shift register port is closed.
        //*
        //* revision history:
        //*
        //*  date	author		revision
        //*  ----	------		--------
        //*  11/30/93	Bill Harker	created
        //*
        //*============================================================================*/

        public int sr_start_counting()
        {
            ushort i;
            int    status;
            byte   sr_status;

            log.TraceEvent(LogLevels.Info, 0x4f322, "Start counting with the shift register");

            /* first make sure the shift register is stopped. */
            i = 0;
            do
            {
                status = SRLib.Control(dsid.SerialPort, sr_h.SR_STOP, null);
                i++;
            } while ((status != sr_h.SR_SUCCESS) && (i < SR.SR_NUM_TRYS) && !lcts.IsCancellationRequested);
            if (status != sr_h.SR_SUCCESS)
            {
                status = sr_restart();
                if (status == SUCCESS)
                {
                    return(SR.SR_TRY_AGAIN);
                }
                else
                {
                    return(SR.FAIL);
                }
            }

            /* check shift register status to be sure it is stopped. */
            sr_status = 0;
            i         = 0;
            do
            {
                status = SRLib.Control(dsid.SerialPort, sr_h.SR_GET_STATUS, ref sr_status);
                i++;
            } while ((status != sr_h.SR_SUCCESS) && (i < SR.SR_NUM_TRYS) && !lcts.IsCancellationRequested);
            if (status != sr_h.SR_SUCCESS)
            {
                log.TraceEvent(LogLevels.Warning, 0x44F0A, "{0} Status on shift register", sr_h.SRFunctionReturnStatusCode(status));
                status = sr_restart();
                if (status == SR.SUCCESS)
                {
                    return(SR.SR_TRY_AGAIN);
                }
                else
                {
                    return(SR.FAIL);
                }
            }
            if ((sr_status & sr_h.SR_S_STOPPED) == 0)
            {
                log.TraceEvent(LogLevels.Warning, 0x44F0B, "{0:x8} status on shift register", sr_status);
                status = sr_restart();
                if (status == SR.SUCCESS)
                {
                    return(SR.SR_TRY_AGAIN);
                }
                else
                {
                    return(SR.FAIL);
                }
            }

            i = 0;
            do
            {
                /* zero out all shift register counters */
                SRLib.Control(dsid.SerialPort, sr_h.SR_ZERO, null);

                /* start shift register counting */
                status = SRLib.Control(dsid.SerialPort, sr_h.SR_START, null);
                i++;
            } while ((status != sr_h.SR_SUCCESS) && (i < SR.SR_NUM_TRYS) && !lcts.IsCancellationRequested);
            if (status != sr_h.SR_SUCCESS)
            {
                log.TraceEvent(LogLevels.Warning, 0x44F0D, "{0} Status on shift register", sr_h.SRFunctionReturnStatusCode(status));
                status = sr_restart();
                if (status == SUCCESS)
                {
                    return(SR.SR_TRY_AGAIN);
                }
                else
                {
                    return(SR.FAIL);
                }
            }

            /* make sure shift register actually started. */
            sr_status = 0;
            i         = 0;
            do
            {
                status = SRLib.Control(dsid.SerialPort, sr_h.SR_GET_STATUS, ref sr_status);
                i++;
            }while ((status != sr_h.SR_SUCCESS) && (i < SR.SR_NUM_TRYS) && !lcts.IsCancellationRequested);
            if ((sr_status != 0) || (status != sr_h.SR_SUCCESS))
            {
                status = sr_restart();
                if (status == SR.SUCCESS)
                {
                    return(SR.SR_TRY_AGAIN);
                }
                else
                {
                    return(SR.FAIL);
                }
            }

            return(sr_h.SR_SUCCESS);
        }
Exemplo n.º 3
0
        //* <f> */
        //*============================================================================
        //*
        //* function name: sr_get_data()
        //*
        //* purpose: check for shift register stopped, and if so read the data and
        //*	    put it in the database run record.
        //*
        //* return value: SR_SUCCESS/SR_NOT_FINISHED/ZERO_COUNT_TIME/SR_TRY_AGAIN/FAIL
        //*
        //* special notes: if there is a failure, the shift register port is closed.
        //*
        //* revision history:
        //*
        //*  date	author		revision
        //*  ----	------		--------
        //*  11/30/93	Bill Harker	created
        //*
        //*============================================================================*/

        public unsafe int sr_get_data(ref run_rec_ext run_ptr)
        {
            sr_h.sr_rdout       rdout = new sr_h.sr_rdout(); /* sr scaler data */
            sr_h.sr_mult_rdout  mult_rdout;                  /* sr multiplicity data */
            sr_h.sr_mult_rdout2 mult_rdout2;                 /* sr multiplicity data */
            int    bincount = sr_h.SR_MAX_MULT;
            byte   sr_status;
            int    status, mult_status;
            ushort num_trys;
            ushort i, j;

            /* check for shift register stopped */
            sr_status = 0;
            status    = SRLib.Control(dsid.SerialPort, sr_h.SR_GET_STATUS, ref sr_status);
            if (status != sr_h.SR_SUCCESS)
            {
                status = sr_restart();
                if (status == SUCCESS)
                {
                    return(SR_TRY_AGAIN);
                }
                else
                {
                    return(FAIL);
                }
            }
            if ((sr_status & sr_h.SR_S_FAULT) != 0)
            {
                status = sr_restart();
                if (status == SUCCESS)
                {
                    return(SR_TRY_AGAIN);
                }
                else
                {
                    return(FAIL);
                }
            }
            if (((sr_status & sr_h.SR_S_TIMEOUT) == 0) && ((sr_status & sr_h.SR_S_STOPPED) == 0))
            {
                return(SR_NOT_FINISHED);
            }
            log.TraceEvent(LogLevels.Info, 0x4f32E, "Getting data from the shift register");
            /* get a set of scaler data from the shift register */
            num_trys = 0;
            do
            {
                status = SRLib.Control(dsid.SerialPort, sr_h.SR_GET_RDOUT, ref rdout);
                num_trys++;
            } while ((status == sr_h.SR_TIMEOUT) && (num_trys <= SR_NUM_TRYS));

            /* get a set of multiplicity data from the shift register */
            if ((dsid.SRType == InstrType.MSR4A) ||
                (dsid.SRType == InstrType.PSR) || AMSRFerSher)
            {
                mult_rdout = new sr_h.sr_mult_rdout();  /* sr multiplicity data */
                num_trys   = 0;
                do
                {
                    Thread.Sleep(100);          /* allow windows system to execute */
                    mult_status = SRLib.Control(dsid.SerialPort, sr_h.SR_GET_MULT_RDOUT, ref mult_rdout);
                    num_trys++;
                } while ((mult_status == sr_h.SR_TIMEOUT) && (num_trys <= SR_NUM_TRYS));
            }
            else if (JSR15orUNAPMasqueradingAsAMSR || dsid.SRType == InstrType.JSR15 || dsid.SRType == InstrType.UNAP)
            {
                bincount    = sr_h.SR_MAX_MULT2;
                mult_rdout2 = new sr_h.sr_mult_rdout2();        /* 512 sr multiplicity data */
                num_trys    = 0;
                do
                {
                    Thread.Sleep(100);          /* allow windows system to execute */
                    mult_status = SRLib.Control(dsid.SerialPort, sr_h.SR_GET_MULT_RDOUT2, ref mult_rdout2);
                    num_trys++;
                } while ((mult_status == sr_h.SR_TIMEOUT) && (num_trys <= SR_NUM_TRYS));
            }
            else
            {
                mult_rdout   = new sr_h.sr_mult_rdout();
                mult_status  = sr_h.SR_SUCCESS;
                mult_rdout.n = bincount;
                for (j = 0; j < bincount; j++)
                {
                    mult_rdout.rpa[j] = 0;
                    mult_rdout.a[j]   = 0;
                }
            }

            if ((status != sr_h.SR_SUCCESS) || (mult_status != sr_h.SR_SUCCESS))
            {
                status = sr_restart();
                if (status == SUCCESS)
                {
                    return(SR_TRY_AGAIN);
                }
                else
                {
                    return(FAIL);
                }
            }
            else if (rdout.time <= 0)
            {
                return(ZERO_COUNT_TIME);
            }

            /* read high voltage */
            if (dsid.SRType == InstrType.JSR12)
            {
                SRLib.Ioctl(dsid.SerialPort, sr_h.SR_JSR12_GET_HV,
                            ref run_ptr.run_high_voltage);
            }
            else if ((dsid.SRType == InstrType.PSR) ||
                     (dsid.SRType == InstrType.AMSR) || (dsid.SRType == InstrType.UNAP))
            {
                SRLib.Ioctl(dsid.SerialPort, sr_h.SR_PSR_GET_HV,
                            ref run_ptr.run_high_voltage);
            }
            else if (dsid.SRType == InstrType.JSR15) // HHMR
            {
                SRLib.Ioctl(dsid.SerialPort, sr_h.SR_HHMR_GET_HV,
                            ref run_ptr.run_high_voltage);
            }
            else if (dsid.SRType == InstrType.DGSR)
            {
                SRLib.Ioctl(dsid.SerialPort, sr_h.SR_DGSR_GET_HV,
                            ref run_ptr.run_high_voltage);
            }
            else
            {
                run_ptr.run_high_voltage = 0.0;
            }

            /* put shift register data in run record */
            /* use current date and time */
            string dt = String.Empty;
            int    x  = NCCTransfer.INCC.Gen32.gen_date_time(NCCTransfer.INCC.Gen32.GEN_DTF_IAEA, ref dt);

            //gen_date_time (GEN_DTF_IAEA, &string_addr);
            //strcpy (run_date_time_string, string_addr);
            Byte[] dtba = Encoding.ASCII.GetBytes(dt);
            fixed(byte *rd = run_ptr.run_date,
                  rt       = run_ptr.run_time,
                  tst      = run_ptr.run_tests)
            {
                TransferUtils.Copy(dtba, 0, rd, 0, INCC.DATE_TIME_LENGTH);
                TransferUtils.Copy(dtba, 9, rt, 0, INCC.DATE_TIME_LENGTH); // ? index?
                TransferUtils.PassPack(tst);

                run_ptr.run_count_time     = rdout.time;
                run_ptr.run_singles        = rdout.totals;
                run_ptr.run_scaler1        = rdout.totals2;
                run_ptr.run_scaler2        = rdout.totals3;
                run_ptr.run_reals_plus_acc = rdout.rpa;
                if (dsid.SRType == InstrType.DGSR)  // todo: gate_length2, but no-one will ever need it
                {
                    run_ptr.run_acc = rdout.a * (sr_parms.gateLengthMS / sr_parms.gateLengthMS);
                    //  sr_parms.gate_length2);
                }
                else
                {
                    run_ptr.run_acc = rdout.a;
                }

                fixed(double *RA = run_ptr.run_mult_reals_plus_acc, A = run_ptr.run_mult_acc)
                {
                    for (i = 0; i < bincount; i++)
                    {
                        if (bincount == sr_h.SR_MAX_MULT)
                        {
                            RA[i] = mult_rdout.rpa[i];
                            A[i]  = mult_rdout.a[i];
                        }
                        else if (bincount == sr_h.SR_MAX_MULT2)
                        {
                            RA[i] = mult_rdout2.rpa[i];
                            A[i]  = mult_rdout2.a[i];
                        }
                    }
                }
            }

            return(sr_h.SR_SUCCESS);
        }
Exemplo n.º 4
0
        unsafe public int sr_init(double time_per_run, double sethigh_voltage)
        {
            restart_time_per_run = time_per_run; restart_sethigh_voltage = sethigh_voltage;
            const double MAX_HV_DELTA = 2.0;

            sr_h.sr_parms parms;
            double        high_voltage, previous_high_voltage;
            int           status;
            ushort        i;

            if (time_per_run <= 0.0)
            {
                return(SR.ZERO_COUNT_TIME);
            }

            /* as a diagnostic, dump all shift register data to a file */
            /* sr_set_log_fname ("sr_data.tst"); */


            // todo: add use of progress event reporting via the timer here
            //        "Please wait, opening shift register.");

            log.TraceEvent(LogLevels.Info, 0x4f32F, "Please wait, opening shift register");

            /* open shift register */
            i = 0;
            InstrType insttype = dsid.SRType;

            do
            {
                i++;
                if (dsid.SRType == InstrType.JSR11)
                {
                    dsid.BaudRate = JSR11_BAUD_RATE;
                }
                else if (dsid.SRType == InstrType.JSR12)
                {
                    dsid.BaudRate = JSR12_BAUD_RATE;
                }
                else if ((dsid.SRType == InstrType.PSR) ||
                         (dsid.SRType == InstrType.AMSR) || dsid.SRType.isVirtualAMSR())
                {
                    if (dsid.SRType == InstrType.AMSR && dsid.BaudRate != PSR_BAUD_RATE)                                      // AMSR faux override for UNAP
                    {
                        log.TraceEvent(LogLevels.Verbose, 0x4f32D, "AMSR Baud rate override at " + dsid.BaudRate.ToString()); // retain AMSR setting, probably fine
                    }
                    else if (dsid.SRType == InstrType.PSR && dsid.BaudRate != PSR_BAUD_RATE)                                  // PSR faux override for JSR15
                    {
                        log.TraceEvent(LogLevels.Verbose, 0x4f32D, "PSR Baud rate override at " + dsid.BaudRate.ToString());  //retain PSR setting, probably bad fail for JSR15
                    }
                    else if (dsid.SRType.BigLoveMultiplicity())
                    {
                        log.TraceEvent(LogLevels.Verbose, 0x4f337, dsid.SRType.ToString() + " Baud rate is " + dsid.BaudRate.ToString()); // explicitly JSR15 or UNAP
                    }
                    else
                    {
                        dsid.BaudRate = PSR_BAUD_RATE; // the original INCC5 treatment
                        insttype      = InstrType.PSR;
                    }
                }
                else if (dsid.SRType == InstrType.DGSR)
                {
                    dsid.BaudRate = DGSR_BAUD_RATE;
                }
                else if (dsid.SRType == InstrType.JSR15 || dsid.SRType == InstrType.UNAP)
                {
                    // use the user-level setting dsid.BaudRate
                    log.TraceEvent(LogLevels.Verbose, 0x4f335, "JSR15 Baud rate is " + dsid.BaudRate.ToString()); // dsid.BaudRate
                }
                else
                {
                    dsid.BaudRate = MSR4A_BAUD_RATE;
                }
                status = SRLib.Open(dsid.SerialPort, (int)insttype, dsid.BaudRate);
                if (status != sr_h.SR_SUCCESS)
                {
                    sr_stop();
                    if (i < SR.SR_NUM_TRYS)
                    {
                        log.TraceEvent(LogLevels.Warning, 0x4f335, "Retrying the shift register init process");
                    }
                }
            } while ((status != sr_h.SR_SUCCESS) && (i < SR.SR_NUM_TRYS) && !lcts.IsCancellationRequested);

            if ((status != sr_h.SR_SUCCESS))
            {
                log.TraceEvent(LogLevels.Warning, 0x4f330, "{0}; Unable to open shift register serial port {1} for the {2} at rate {3}\r\nCheck serial port # and baud rate, traditionally 9600 (300 for the JSR-11), can be higher for JSR15/HHMR and later devices",
                               sr_h.SRFunctionReturnStatusCode(status), dsid.SerialPort + 1, dsid.SRType.ToString(), dsid.BaudRate.ToString());
                //log.TraceEvent(LogLevels.Warning, 0x4f330, "{0}; Unable to open shift register serial port.\r\nMake sure that the shift register is turned on and properly connected.\r\nMake sure that the baud rate is set to 9600 (300 for the JSR-11).\r\nYou should also use the Measurement Parameters dialog box under Setup to verify that you have selected the correct shift register type and serial port", sr_h.SRFunctionReturnStatusCode(status));
                return(status);
            }
            else
            {
                log.TraceEvent(LogLevels.Verbose, 0x4f32B, "Opened shift register serial port {0} for the {1} at rate {2}", dsid.SerialPort + 1, dsid.SRType.ToString(), dsid.BaudRate);
            }

            /* set shift register parameters */
            parms.predelay = sr_parms.predelayMS;
            parms.gate     = sr_parms.gateLengthMS;
            parms.time     = time_per_run;
            parms.hv       = sethigh_voltage;
            if (dsid.SRType == InstrType.DGSR)
            {
                parms.gate2 = sr_parms.gateLengthMS; // todo: DGSR gate_length2, but no-one will ever need it
            }
            else
            {
                parms.gate2 = 0.0;
            }
            i = 0;
            do
            {
                status = SRLib.Control(dsid.SerialPort, sr_h.SR_SET_PARMS, ref parms);
                i++;
            } while ((status != sr_h.SR_SUCCESS) && (i < SR.SR_NUM_TRYS) && !lcts.IsCancellationRequested);
            if ((status != sr_h.SR_SUCCESS)) // devnote: this test can fail for JSR-15 with an open serial port, then must depend upon the timeout failure in the voltage step following
            {
                log.TraceEvent(LogLevels.Warning, 0x4f331, "{0}; Unable to set shift register parameters; HV {1}, predelay {2}, gate {3}; {4} second cycle", sr_h.SRFunctionReturnStatusCode(status), parms.hv, parms.predelay, parms.gate, parms.time);
                sr_stop();
                return(status);
            }
            else
            {
                log.TraceEvent(LogLevels.Verbose, 0x4f32A, "Set shift register parameters; HV {0}, predelay {1}, gate {2}; {3} second cycle", parms.hv, parms.predelay, parms.gate, parms.time);
            }
            bool   retry    = false;
            ushort timeouts = 0;             // allow for 100 retries with timeouts (hn), a timeout means something is wrong with communication

            /* if a PSR, JSR-12, DGSR or AMSR (or JSR15/UNAP) then wait for high voltage to stabilize. */
            if ((dsid.SRType == InstrType.PSR) ||
                dsid.SRType.isDG_AMSR_Match() ||
                (dsid.SRType == InstrType.JSR12))
            {
                i            = 0;
                high_voltage = 0.0;
                do
                {
                    previous_high_voltage = high_voltage;
                    Thread.Sleep(200);  // devnote: time needed for HV operations to settle, much more needed for HHMR
                    if (dsid.SRType == InstrType.PSR || dsid.SRType == InstrType.AMSR || dsid.SRType == InstrType.UNAP)
                    {
                        status = SRLib.Ioctl(dsid.SerialPort, sr_h.SR_PSR_GET_HV,
                                             ref high_voltage);
                    }
                    else if (dsid.SRType == InstrType.JSR12)
                    {
                        status = SRLib.Ioctl(dsid.SerialPort, sr_h.SR_JSR12_GET_HV,
                                             ref high_voltage);
                    }
                    else if (dsid.SRType == InstrType.JSR15) // HHMR
                    {
                        //log.TraceEvent(LogLevels.Warning, 0x4f334, "SR_HHMR_GET_HV aka SRIOR(SR_TYPE_HHMR, 15) times out, skipping HV stabilization step");
                        //status = sr_h.SR_SUCCESS;
                        //break;
                        if (i == 0)
                        {
                            Thread.Sleep(1000); // initially wait for HHMR to settle
                        }
                        status = SRLib.Ioctl(dsid.SerialPort, sr_h.SR_HHMR_GET_HV,
                                             ref high_voltage);
                    }
                    else
                    {
                        status = SRLib.Ioctl(dsid.SerialPort, sr_h.SR_DGSR_GET_HV,
                                             ref high_voltage);
                    }
                    if (i != 0)
                    {
                        Thread.Sleep(1000); // 1 sec
                    }
                    if (status == sr_h.SR_TIMEOUT)
                    {
                        timeouts++;
                    }

                    retry = !(((Math.Abs(high_voltage - previous_high_voltage) > MAX_HV_DELTA) && // while have not matched voltage
                               (status == sr_h.SR_SUCCESS) && (i < 100))) ||                // and the connection is good and we haven't yet tried 100 times
                            timeouts >= 100;                                                      // OR it simply timed out too many times
                    if (retry && ((i % 2) == 0) && status == sr_h.SR_TIMEOUT)
                    {
                        log.TraceEvent(LogLevels.Warning, 0x4f332, "Set voltage attempt {0} of 100, HV {1}, SR status {2}", i + 1, Math.Abs(high_voltage - previous_high_voltage), sr_h.SRFunctionReturnStatusCode(status));
                    }

                    if (lcts.IsCancellationRequested)                      // punt here
                    {
                        break;
                    }
                    i++;
                } while (retry);
                // devnote: When a timeout occurred, the original code exited. Now, check for TIMEOUT condition and try again up to 100 tries hn
                if ((status != sr_h.SR_SUCCESS) || (i >= 100) || (timeouts >= 100))
                {
                    log.TraceEvent(LogLevels.Warning, 0x4f332, "{0} {1}; Unable to set shift register high voltage", sr_h.SRFunctionReturnStatusCode(status), i);
                }
            }

            return(status);
        }