示例#1
0
        /// <summary>
        ///从站应答
        /// </summary>
        public DataItem_8103(byte [] data)
        {
            //this.identityCode = IdentityCode.读结算日;

            //结算日 一个字节
            this._jieSuanDay = BCD.B2I(data[3]);
        }
示例#2
0
        /// <summary>
        ///从站应答
        /// </summary>
        public DataItem_8104(byte[] data)
        {
            //this.identityCode = IdentityCode.读抄表日;

            //抄表日 一个字节
            this._chapBiaoDay = BCD.B2I(data[3]);
        }
示例#3
0
        /// <summary>
        /// This method reads a 3 byte BCD date from an SCS device.
        /// </summary>
        /// <exception cref="SCSException">
        /// Thrown when the value cannot be retreived from the meter.
        /// </exception>
        /// <remarks >
        /// MM/DD/YY who Version Issue# Description
        /// -------- --- ------- ------ ---------------------------------------
        /// 12/07/06 mah 8.00.00  N/A   Created
        /// </remarks>
        virtual internal void ReadBCDDate(int nBasepageAddress, out int nYear, out int nMonth, out int nDay)
        {
            byte[] byBCDValue;

            // Upload the date string
            SCSProtocolResponse ProtocolResponse = m_SCSProtocol.Upload(nBasepageAddress, 3, out byBCDValue);

            if (SCSProtocolResponse.SCS_ACK == ProtocolResponse)
            {
                nYear  = (int)BCD.BCDtoByte(byBCDValue[0]);
                nMonth = (int)BCD.BCDtoByte(byBCDValue[1]);
                nDay   = (int)BCD.BCDtoByte(byBCDValue[2]);
            }
            else
            {
                nYear  = 0;
                nMonth = 0;
                nDay   = 0;

                SCSException scsException = new SCSException(
                    SCSCommands.SCS_U, ProtocolResponse, nBasepageAddress, "Display date");

                throw scsException;
            }
        }
示例#4
0
    public List <ICompValue> ReadFile()
    {
        List <ICompValue> icv = new List <ICompValue>();
        DPD d;
        BCD b;

        while (br.BaseStream.Position != br.BaseStream.Length)
        {
            byte mode = br.ReadByte();
            uint data = br.ReadUInt32();
            if (mode == 1)//DPD
            {
                d     = new DPD();
                d.Raw = data;
                icv.Add(d);
            }
            else //BCD
            {
                b     = new BCD();
                b.Raw = data;
                icv.Add(b);
            }
        }
        icv.Sort();
        return(icv);
    }
示例#5
0
        /// <summary>
        /// This method reads a 3 byte BCD time from an SCS device.
        /// </summary>
        /// <exception cref="SCSException">
        /// Thrown when the value cannot be retreived from the meter.
        /// </exception>
        /// <remarks >
        /// MM/DD/YY who Version Issue# Description
        /// -------- --- ------- ------ ---------------------------------------
        /// 12/07/06 mah 8.00.00  N/A   Created
        /// </remarks>
        virtual internal void ReadBCDTime(int nBasepageAddress, out int nHour, out int nMinute, out int nSecond)
        {
            byte[] byBCDValue;

            // Upload the date string
            SCSProtocolResponse ProtocolResponse = m_SCSProtocol.Upload(nBasepageAddress, 3, out byBCDValue);

            if (SCSProtocolResponse.SCS_ACK == ProtocolResponse)
            {
                nHour   = (int)BCD.BCDtoByte(byBCDValue[0]);
                nMinute = (int)BCD.BCDtoByte(byBCDValue[1]);
                nSecond = (int)BCD.BCDtoByte(byBCDValue[2]);
            }
            else
            {
                nHour   = 0;
                nMinute = 0;
                nSecond = 0;

                SCSException scsException = new SCSException(
                    SCSCommands.SCS_U, ProtocolResponse, nBasepageAddress, "Display time");

                throw scsException;
            }
        }
示例#6
0
        /// <summary>
        /// This method a 4 byte floating point BCD value from an SCS device.
        /// </summary>
        /// <exception cref="SCSException">
        /// Thrown when the value cannot be retreived from the meter.
        /// </exception>
        /// <remarks >
        /// MM/DD/YY who Version Issue# Description
        /// -------- --- ------- ------ ---------------------------------------
        /// 01/26/07 KRC 8.00.09        Adding Edit Registers
        /// </remarks>
        virtual internal ItronDeviceResult SetFloatingBCDValue(int nBasepageAddress, int nLength, string strValue)
        {
            ItronDeviceResult Result = ItronDeviceResult.SUCCESS;

            byte[] byBCDValue;
            double dblValue;

            // Get the double out of the string (This allows us to format the value as needed when doing the conversion)
            dblValue   = double.Parse(strValue, CultureInfo.CurrentCulture);
            byBCDValue = BCD.DoubleToFloatingBCD(dblValue, nLength);

            // Now that we have the byte array, we can send it to the meter.
            SCSProtocolResponse ProtocolResponse = m_SCSProtocol.Download(nBasepageAddress, nLength, ref byBCDValue);

            if (SCSProtocolResponse.SCS_CAN == ProtocolResponse)
            {
                Result = ItronDeviceResult.SECURITY_ERROR;
            }
            else if (SCSProtocolResponse.SCS_NAK == ProtocolResponse)
            {
                Result = ItronDeviceResult.ERROR;
            }


            return(Result);
        }
示例#7
0
        /// <summary>
        /// Constructs an SCS display item from the bit mapped, 4 byte SCS display item
        /// record.
        /// </summary>
        /// <param name="byDisplayTable"></param>
        /// <param name="nTableOffset"></param>
        /// <param name="boolTestMode"></param>
        /// <remarks >
        /// MM/DD/YY who Version Issue# Description
        /// -------- --- ------- ------ ---------------------------------------
        /// 12/15/06 mah 8.00.00  N/A   Created
        /// </remarks>
        public SCSDisplayItem(ref byte[] byDisplayTable, int nTableOffset, bool boolTestMode)
            : base()
        {
            // If the first byte contains an FF then this is an end of file marker

            m_boolEndOfFile = (byDisplayTable[nTableOffset] == END_OF_TABLE);

            if (!m_boolEndOfFile)
            {
                // make a copy of the unprocessed display item data for later interpretation if needed
                m_abyItemData = new byte[DISPLAYITEM_LENGTH];
                Array.Copy(byDisplayTable, nTableOffset, m_abyItemData, 0, DISPLAYITEM_LENGTH);

                // Decode the register type and class in order to determine what meaning of the
                // display table

                m_byRegisterType  = (byte)((byDisplayTable[nTableOffset] & 0x70) >> 4);
                m_byRegisterClass = (SCSDisplayClass)(byDisplayTable[nTableOffset] & 0x0F);

                // Extract the ID code from the second byte of the display table

                m_byDisplayID = BCD.BCDtoByte(byDisplayTable[nTableOffset + ID_OFFSET]);

                // Note that an ID code of zero indicates that no ID was programmed.  Therefore
                // return an empty string rather than display a 0
                if (m_byDisplayID == 0)
                {
                    m_strDisplayID = "";
                }
                else
                {
                    m_strDisplayID = m_byDisplayID.ToString(CultureInfo.InvariantCulture);
                }

                if (boolTestMode)
                {
                    m_DisplayType = ItronDevice.DisplayMode.TEST_MODE;
                }
                else if ((byDisplayTable[nTableOffset + DISPLAYMODE_OFFSET] & ALTMODE_MASK) != 0)
                {
                    m_DisplayType = ItronDevice.DisplayMode.ALT_MODE;
                }
                else
                {
                    m_DisplayType = ItronDevice.DisplayMode.NORMAL_MODE;
                }

                m_byTOURate = (byte)((byDisplayTable[nTableOffset + TOURATE_OFFSET] & 0x70) >> 4);

                m_byUpperAddress = BCD.BCDtoByte((byte)(byDisplayTable[nTableOffset + ADDRESSBANK_OFFSET] & 0x0F));
                m_byLowerAddress = byDisplayTable[nTableOffset + ADDRESS_OFFSET];
            }
            else
            {
                m_strDescription = "End of Table";
                m_DisplayType    = ItronDevice.DisplayMode.NORMAL_MODE;
            }
        }
示例#8
0
        public DataItem_A012(int ChaoBiaoDay)
        {
            this.ChaoBiaoDay = ChaoBiaoDay;


            //this.identityCode = IdentityCode.写抄表日;
            //this.indexCode = 0x00;
            //this.controlCode = ControlCode.WriteData;
            //this.length = 4;

            this.dataUnits = new byte[1];


            dataUnits[0] = BCD.I2B(this.ChaoBiaoDay);
        }
示例#9
0
        /// <summary>
        /// This method reads a integer BCD value from an SCS device.
        /// </summary>
        /// <exception cref="SCSException">
        /// Thrown when the value cannot be retreived from the meter.
        /// </exception>
        /// <remarks >
        /// MM/DD/YY who Version Issue# Description
        /// -------- --- ------- ------ ---------------------------------------
        /// 12/07/06 mah 8.00.00  N/A   Created
        /// </remarks>
        virtual internal int ReadBCDInteger(int nBasepageAddress, int nLength)
        {
            int nValue = 0;

            byte[] byBCDValue;

            SCSProtocolResponse ProtocolResponse = m_SCSProtocol.Upload(nBasepageAddress, nLength, out byBCDValue);

            if (SCSProtocolResponse.SCS_ACK == ProtocolResponse)
            {
                nValue = BCD.BCDtoInt(ref byBCDValue, nLength);
            }
            else
            {
                SCSException scsException = new SCSException(
                    SCSCommands.SCS_U, ProtocolResponse, nBasepageAddress, "Integer BCD Value");
                throw scsException;
            }

            return(nValue);
        }
示例#10
0
        /// <summary>
        /// This method a 4 byte floating point BCD value from an SCS device.
        /// </summary>
        /// <exception cref="SCSException">
        /// Thrown when the value cannot be retreived from the meter.
        /// </exception>
        /// <remarks >
        /// MM/DD/YY who Version Issue# Description
        /// -------- --- ------- ------ ---------------------------------------
        /// 12/07/06 mah 8.00.00  N/A   Created
        /// </remarks>
        virtual internal String ReadFloatingBCDValue(int nBasepageAddress, int nLength)
        {
            String strValue = "";

            byte[] byBCDValue;

            SCSProtocolResponse ProtocolResponse = m_SCSProtocol.Upload(nBasepageAddress, nLength, out byBCDValue);

            if (SCSProtocolResponse.SCS_ACK == ProtocolResponse)
            {
                strValue = BCD.FloatingBCDtoString(ref byBCDValue, nLength);
            }
            else
            {
                SCSException scsException = new SCSException(
                    SCSCommands.SCS_U, ProtocolResponse, nBasepageAddress, "Floating BCD Value");
                throw scsException;
            }

            return(strValue);
        }
示例#11
0
文件: Program.cs 项目: nao-sky/BCD
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            BCD a = BCD.Parse("10000000");
            BCD b = BCD.Parse("-1");
            BCD c = BCD.Parse("3");

            BCD ans = a / c;

            Console.WriteLine($"ans={ans}");


            List <BCD> num = new List <BCD>();

            num.Add(BCD.Parse("2"));

            for (BCD i = BCD.Parse("2"); BCD.Parse("100") > i; i += BCD.One)
            {
                BCD an = BCD.Zero;

                foreach (BCD n in num)
                {
                    BCD ab = i / n;
                    if (!BCD.IsZero(ab.Rem))
                    {
                        an = i;
                    }
                }
                if (!BCD.IsZero(an))
                {
                    Console.WriteLine(an);
                }
            }

            BCD aq = BCD.One;

            for (BCD bi = BCD.One; BCD.Parse("10000") > aq; bi += BCD.One)
            {
                aq *= bi;
                Console.WriteLine(aq);
            }

            Console.WriteLine(a);
            Console.WriteLine(b);

            Console.WriteLine(a - b + c);

            for (int i = -5; 15 >= i; i++)
            {
                for (int j = -3; 15 >= j; j++)
                {
                    Console.WriteLine($"{j} * {i} = {BCD.Parse(j.ToString()) * BCD.Parse(i.ToString())}");
                }
            }

            var sw = Console.Out;

            try
            {
                //new StreamWriter(@"D:\workspace\TEST_FACTORIAL2\factorial-A1.txt");
                BCD  ans1 = BCD.Parse("1");
                long f    = 100;
                for (long i = 1; f >= i; i++)
                {
                    if (i % 10000 == 0)
                    {
                        sw.Flush();
                        sw.Close();
                        //sw = new StreamWriter($@"D:\workspace\TEST_FACTORIAL2\factorial-A{i}.txt");
                        Console.Write('■');
                    }
                    ans1 = ans1 * BCD.Parse(i.ToString());
                    sw.WriteLine($"{i}! = {ans1}");
                    sw.Flush();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.WriteLine("stop.");
            }
            finally
            {
                sw.Flush();
                sw.Close();
                Console.ReadLine();
            }


            BCD az  = BCD.Zero;
            int len = 100;

            for (int i = 0; len > i; i++)
            {
                if (i % (len / 10) == 0)
                {
                    Console.Write('■');
                }
                az = az + BCD.Parse(i.ToString());
            }
            Console.WriteLine(az);
        }
示例#12
0
        private void Execute(Opcode param)
        {
            switch (param.Nibble)
            {
            case 0x0000:
                if (param.Value == 0x00e0)
                {
                    display.ClearDisplay();
                }
                else if (param.Value == 0x00ee)
                {
                    var address = ram.ReturnFromSubroutine();
                    register.JumpToAddress(address);
                    break;
                }
                else
                {
                    throw new InvalidOperationException("opcode not found");
                }
                break;

            case 0x1000:
                register.JumpToAddress(param.NNN);
                break;

            case 0x2000:
                ram.CallSubroutine(register.PC);
                register.JumpToAddress(param.NNN);
                break;

            case 0x3000:
                if (register[param.X] == param.NN)
                {
                    register.IncrementPC();
                }
                break;

            case 0x4000:
                if (register[param.X] != param.NN)
                {
                    register.IncrementPC();
                }
                break;

            case 0x5000:
                if (register[param.X] == register[param.Y])
                {
                    register.IncrementPC();
                }
                break;

            case 0x6000:
                register.StoreValue(param.X, param.NN);
                break;

            case 0x7000:
                register.IncrementRegisterValue(param.X, param.NN);
                break;

            case 0x8000:
                byte last = (byte)(param.Value & 0x000F);

                switch (last)
                {
                case 0: register.StoreValue(param.X, register[param.Y]); break;

                case 1: register.CalculateRegistersAndStoreValue(param.X, param.X, param.Y, BitOperationType.OR); break;

                case 2: register.CalculateRegistersAndStoreValue(param.X, param.X, param.Y, BitOperationType.AND); break;

                case 3: register.CalculateRegistersAndStoreValue(param.X, param.X, param.Y, BitOperationType.XOR); break;

                case 4:
                    register.StoreValueOnRegister0xF((byte)(register[param.X] + register[param.Y] > 255 ? 1 : 0));
                    register.StoreValue(param.X, (byte)((register[param.X] + register[param.Y]) & 0x00FF));
                    break;

                case 5:
                    register.StoreValueOnRegister0xF((byte)(register[param.X] > register[param.Y] ? 1 : 0));
                    register.StoreValue(param.X, (byte)((register[param.X] - register[param.Y]) & 0x00FF));
                    break;

                case 6:

                    register.StoreValueOnRegister0xF((byte)(register[param.X] & 0x0001));
                    register.StoreValue(param.X, (byte)(register[param.X] >> 1));
                    break;

                case 7:
                    register.StoreValueOnRegister0xF((byte)(register[param.Y] > register[param.X] ? 1 : 0));
                    register.StoreValue(param.X, (byte)((register[param.Y] - register[param.X]) & 0x00FF));
                    break;

                case 0xE:
                    register.StoreValueOnRegister0xF((byte)((register[param.X] & 0x80) == 0x80 ? 1 : 0));
                    register.StoreValue(param.X, (byte)(register[param.X] << 1));
                    break;

                default:
                    throw new InvalidOperationException("opcode not found");
                }

                break;

            case 0x9000:
                if (register[param.X] != register[param.Y])
                {
                    register.IncrementPC();
                }
                break;

            case 0xA000:
                register.SetRegisterI(param.NNN); break;

            case 0xB000:
                register.JumpToAddress((ushort)(register[0] + param.NNN)); return;

            case 0xC000:
                register.StoreValue(param.X, (byte)((byte)Random.Next(0, byte.MaxValue) & param.NN));
                break;

            case 0xE000:
                last = (byte)(param.Value & 0x000F);

                switch (last)
                {
                case 14:
                    if (register[param.X] == Keyboard)
                    {
                        register.IncrementPC();
                    }
                    break;

                case 1:
                    if (register[param.X] != Keyboard)
                    {
                        register.IncrementPC();
                    }
                    break;

                default:
                    throw new InvalidOperationException("opcode not found");
                }
                break;

            case 0xD000:
                var sprites = ram.GetValues(register.I, param.N);
                display.Draw(sprites, register[param.X], register[param.Y], out var overridePixel);
                register.StoreValueOnRegister0xF(overridePixel);
                break;

            case 0xF000:
                last = (byte)(param.Value & 0x00FF);

                switch (last)
                {
                case 0x7:         //FX07
                    register.StoreValue(param.X, timer.DelayTimer);
                    break;

                case 0x0A:
                    WaitingForKeyboardEvent.WaitOne();
                    register.StoreValue(param.X, Keyboard);
                    break;

                case 0x15:
                    timer.SetTimer(TimerType.Delay, register[param.X]);
                    break;

                case 0x18:         //FX18
                    timer.SetTimer(TimerType.Sound, register[param.X]);
                    break;

                case 0x1E:
                    register.IncreaseRegisterI(register[param.X]);
                    break;

                case 0x29:
                    register.SetRegisterI(ram.GetFontAddress(register[param.X]));
                    break;

                case 0x33:
                    var bcd = BCD.GetBytes(register[param.X]);
                    ram.CopyValuesToRam(bcd, register.I);
                    break;

                case 0x55:
                    ram.CopyValuesToRam(register.GetRegisters(0, param.X), register.I);
                    break;

                case 0x65:
                    register.CopyValuesToRegisters(ram.GetValues(register.I, param.X, inclusive: true), 0);
                    break;

                default:
                    throw new InvalidOperationException("opcode not found");
                }
                break;

            default:
                throw new Exception("opcode not found");
            }

            Thread.Sleep(1);
        }
示例#13
0
        public static bool CheckFrame(byte[] data, string adress)
        {
            /*
             * 数据帧的结构。
             *  FEH	        1	    帧前导符
             *  68H         1       帧起始符
             *  TYPE        1       仪表类型
             *  ADRESS	    7       地址域
             *  C	        1	    控制码
             *  L	        2	    数据长度
             *  DATA	    变长	数据域
             *  CS	        1	    校验码
             *  16H	        1	    结束码
             *
             * */

            MemoryStream memStream = new MemoryStream();

            try
            {
                //数据长度
                if (data.Length < 15)
                {
                    return(false);
                }

                //帧前导符 起始符 仪表类型
                if (data[0] != 0xfe || data[1] != 0x68 || data[2] != 0x30)
                {
                    return(false);
                }

                // 结束符
                if (data[data.Length - 1] != 0x16)
                {
                    return(false);
                }

                //校验码
                memStream = new MemoryStream();
                memStream.Write(data, 0, data.Length);

                byte[] csData = new byte[data.Length - 3];
                memStream.Read(csData, 1, data.Length - 3);

                byte cs = 0x00;
                for (int i = 0; i < csData.Length; i++)
                {
                    cs += csData[i];
                }
                if (cs != data[data.Length - 1])
                {
                    return(false);
                }

                //地址
                byte[] adressData = new byte[7];
                memStream.Read(adressData, 3, 7);

                string temp_adress = BCD.B2S(adressData);

                if (temp_adress != adress)
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(false);
            }
            finally
            {
                memStream.Close();
            }
        }