Esempio n. 1
0
            public Measurement(String meterPacket)
            {
                packet = meterPacket;
                if (meterPacket.Length != 12)
                {
                    throw (new ArgumentException("incorrect format input string should be 12 char long"));
                }

                //convert range char to number
                range = (int)char.GetNumericValue(meterPacket[0]);

                var displayDigits = meterPacket.Substring(1, 5);

                SwitchPosition = (SwitchPositions)meterPacket[6];

                status = (StatusFlags)(0x0f & meterPacket[7]);

                //colate all option flags into one enum
                options  = (OptionsFlags)(0x0f & meterPacket[8]);
                options |= (OptionsFlags)((0x0f & meterPacket[9]) << 4);
                options |= (OptionsFlags)((0x0f & meterPacket[11]) << 12);
                options |= (OptionsFlags)((0x0f & meterPacket[10]) << 8);

                var measInfo = new UT61EMeter.UnitScaling(SwitchPosition, range, status, options);

                units = measInfo.units;

                if (!decimal.TryParse(displayDigits, out value))
                {
                    nonNumvalue = displayDigits;
                    value       = 99999;
                }
                else
                {
                    nonNumvalue = null;
                    value      *= measInfo.valueMuntiplyer;
                }
            }
Esempio n. 2
0
            public Measurement(String meterPacket)
            {
                packet = meterPacket;
                if (meterPacket.Length != 9)
                {
                    throw (new ApplicationException("incorrect format input string  should be 9 char long"));
                }
                //todo validate string content where posable (vaild char set ? 0,1,2,3,4,5,6,7,8,9,:,H,<,....)
                var displayDigits = meterPacket.Substring(0, 5);

                //convert range char to number
                range          = (0x0f & meterPacket[5]);
                SwitchPosition = (SwitchPositions)meterPacket[6];
                coupling       = (CouplingFlags)(0x0f & meterPacket[7]);
                status         = (StatusFlags)(0x0f & meterPacket[8]);

                var measInfo = new UT804Meter.UnitScaling(SwitchPosition, coupling, status, range);

                units = measInfo.units;
                if (displayDigits[0] == '<' || displayDigits[0] == '?' || displayDigits[0] == ':')
                {
                    nonNumvalue = displayDigits;
                    value       = 99999;
                }
                else
                {
                    nonNumvalue = null;
                    try
                    {
                        value = decimal.Parse(displayDigits) * measInfo.valueMuntiplyer;
                    }
                    catch (FormatException)
                    {
                        throw (new ApplicationException("digits string dosn't contail the correct format"));
                    }
                }
            }
            public UnitScaling(SwitchPositions switchPosition, int range, StatusFlags status, OptionsFlags options)
            {
                //todo could add lots of data validation here to check settings match behavior but beyond the scope of intial version

                switch (switchPosition)
                {
                case SwitchPositions.V:
                    if (options.HasFlag(OptionsFlags.VoltAmpHz))
                    {
                        if (status.HasFlag(StatusFlags.dutyCycle))
                        {
                            units = "% V";
                            //todo
                        }
                        else
                        {
                            units = "Hz V";
                            //todo
                        }
                    }
                    else
                    {
                        if (range == 4)
                        {
                            units           = "mV";
                            valueMuntiplyer = 0.01M;
                        }
                        else
                        {
                            units           = "V";
                            valueMuntiplyer = 1 / (decimal)Math.Pow(10, 4 - range);
                        }
                        if (status.HasFlag(StatusFlags.negertive))
                        {
                            valueMuntiplyer *= -1;
                        }
                        if (options.HasFlag(OptionsFlags.AC))
                        {
                            units += " AC";
                        }
                    }
                    break;

                case SwitchPositions.Ohms:
                    units = "Ohms";
                    break;

                case SwitchPositions.F:
                    units = "F";
                    //todo
                    break;


                case SwitchPositions.uA:
                    if (options.HasFlag(OptionsFlags.VoltAmpHz))
                    {
                        //todo
                        if (status.HasFlag(StatusFlags.dutyCycle))
                        {
                            units = "% uA";
                            //todo
                        }
                        else
                        {
                            units = "Hz uA";
                            //todo
                        }
                    }
                    else
                    {
                        units           = "uA";
                        valueMuntiplyer = 1 / (decimal)Math.Pow(10, 2 - range);
                        if (status.HasFlag(StatusFlags.negertive))
                        {
                            valueMuntiplyer *= -1;
                        }
                        if (options.HasFlag(OptionsFlags.AC))
                        {
                            units += " AC";
                        }
                    }
                    break;

                case SwitchPositions.mA:
                    if (options.HasFlag(OptionsFlags.VoltAmpHz))
                    {
                        //todo
                        if (status.HasFlag(StatusFlags.dutyCycle))
                        {
                            units = "% mA";
                            //todo
                        }
                        else
                        {
                            units = "Hz mA";
                            //todo
                        }
                    }
                    else
                    {
                        units           = "mA";
                        valueMuntiplyer = 1 / (decimal)Math.Pow(10, 3 - range);
                        if (status.HasFlag(StatusFlags.negertive))
                        {
                            valueMuntiplyer *= -1;
                        }
                        if (options.HasFlag(OptionsFlags.AC))
                        {
                            units += " AC";
                        }
                    }
                    break;

                case SwitchPositions.A:
                    if (options.HasFlag(OptionsFlags.VoltAmpHz))
                    {
                        if (status.HasFlag(StatusFlags.dutyCycle))
                        {
                            units = "% A";
                            //todo
                        }
                        else
                        {
                            units = "Hz A";
                            //todo
                        }
                    }
                    else
                    {
                        units           = "A";
                        valueMuntiplyer = 0.001M;
                        if (status.HasFlag(StatusFlags.negertive))
                        {
                            valueMuntiplyer *= -1;
                        }
                        if (options.HasFlag(OptionsFlags.AC))
                        {
                            units += " AC";
                        }
                    }
                    break;

                case SwitchPositions.BEEP:
                    units = "BEEP";
                    //todo - future task not important
                    break;

                case SwitchPositions.DIODE:
                    units = "DIODE";
                    //todo - future task not important
                    break;

                case SwitchPositions.HZdutycycle:
                    if (status.HasFlag(StatusFlags.dutyCycle))
                    {
                        units = "%";
                        //todo
                    }
                    else
                    {
                        units = "Hz";
                        //todo
                    }
                    //todo

                    break;

                default:
                    throw (new ArgumentException("switch positon given is invaid"));
                }
            }
            public UnitScaling(SwitchPositions switchPosition, CouplingFlags coupling, StatusFlags status, int range)
            {
                bool negertiveValue = status.HasFlag(StatusFlags.neg);

                //todo could add lots of data validation here to check settings match behavior but beyond the scope of intial version

                switch (switchPosition)
                {
                case SwitchPositions.V:
                    units           = "V";
                    valueMuntiplyer = 1 / (decimal)Math.Pow(10, 5 - range);
                    if (negertiveValue)
                    {
                        valueMuntiplyer *= -1;
                    }
                    break;

                case SwitchPositions.VAC:
                    units           = "V AC";
                    valueMuntiplyer = 1 / (decimal)Math.Pow(10, 5 - range);
                    break;

                case SwitchPositions.mV:
                    units           = "mV";
                    valueMuntiplyer = 0.01M;
                    if (negertiveValue)
                    {
                        valueMuntiplyer *= -1;
                    }
                    break;

                case SwitchPositions.Ohms:
                    switch (range)
                    {
                    case 1:
                        units           = "Ohms";
                        valueMuntiplyer = 0.01M;
                        break;

                    case 2:
                        units           = "KOhms";
                        valueMuntiplyer = 0.0001M;
                        break;

                    case 3:
                        units           = "KOhms";
                        valueMuntiplyer = 0.001M;
                        break;

                    case 4:
                        units           = "KOhms";
                        valueMuntiplyer = 0.01M;
                        break;

                    case 5:
                        units           = "MOhms";
                        valueMuntiplyer = 0.0001M;
                        break;

                    case 6:
                        units           = "MOhms";
                        valueMuntiplyer = 0.001M;
                        break;

                    case 7:
                        units           = "MOhms";
                        valueMuntiplyer = 0.01M;
                        break;
                    }
                    break;

                case SwitchPositions.F:
                    //meter doen't like this mode stops outputing values to RS232 not sure why
                    units = "F";
                    //todo
                    break;

                case SwitchPositions.degC:
                    units           = "°C";
                    valueMuntiplyer = 0.1M;
                    if (negertiveValue)
                    {
                        valueMuntiplyer *= -1;
                    }
                    break;

                case SwitchPositions.uA:
                    units           = "uA";
                    valueMuntiplyer = 1 / (decimal)Math.Pow(10, 2 - range);
                    if (negertiveValue & !coupling.HasFlag(CouplingFlags.AC))
                    {
                        valueMuntiplyer *= -1;
                    }
                    break;

                case SwitchPositions.mA:
                    units           = "mA";
                    valueMuntiplyer = 1 / (decimal)Math.Pow(10, 3 - range);
                    if (negertiveValue & !coupling.HasFlag(CouplingFlags.AC))
                    {
                        valueMuntiplyer *= -1;
                    }
                    break;

                case SwitchPositions.A:
                    units           = "A";
                    valueMuntiplyer = 0.001M;
                    if (negertiveValue)
                    {
                        valueMuntiplyer *= -1;
                    }
                    break;

                case SwitchPositions.BEEP:
                    units = "BEEP";
                    //todo - future task not important
                    break;

                case SwitchPositions.DIODE:
                    units = "DIODE";
                    //todo - future task not important
                    break;

                case SwitchPositions.HZdutycycle:
                    if (negertiveValue)
                    {
                        units           = "duty cycle %";
                        valueMuntiplyer = 0.01M;
                    }
                    else
                    {
                        switch (range)
                        {
                        case 0:
                            units           = "Hz";
                            valueMuntiplyer = 0.001M;
                            break;

                        case 1:
                            units           = "Hz";
                            valueMuntiplyer = 0.01M;
                            break;

                        case 2:
                            units           = "KHz";
                            valueMuntiplyer = 0.0001M;
                            break;

                        case 3:
                            units           = "KHz";
                            valueMuntiplyer = 0.001M;
                            break;

                        case 4:
                            units           = "KHz";
                            valueMuntiplyer = 0.01M;
                            break;

                        case 5:
                            units           = "MHz";
                            valueMuntiplyer = 0.0001M;
                            break;

                        case 6:
                            units           = "MHz";
                            valueMuntiplyer = 0.001M;
                            break;

                        case 7:
                            units           = "MHz";
                            valueMuntiplyer = 0.01M;
                            break;
                        }
                    }
                    break;

                case SwitchPositions.degF:
                    units           = "°F";
                    valueMuntiplyer = 0.1M;
                    if (negertiveValue)
                    {
                        valueMuntiplyer *= -1;
                    }
                    break;

                case SwitchPositions.Percent4To20mA:
                    units           = "%";
                    valueMuntiplyer = 0.01M;
                    if (negertiveValue)
                    {
                        valueMuntiplyer *= -1;
                    }
                    break;

                default:
                    throw (new ApplicationException("switch positon given is invaid"));
                }
            }