예제 #1
0
파일: ProtobufTest.cs 프로젝트: CDEApp/CDE
        public void Serialize_TestBool1_CheckLength()
        {
            var tbClass = new TestBool1();

            var ms = new MemoryStream();

            Serializer.Serialize(ms, tbClass);

            var b = ms.ToArray();

            Console.WriteLine($"b.Length {b.Length}");
            Console.WriteLine($"b {ByteArrayHelper.ByteArrayToString(b)}");
            //Assert.That(b.Length, Is.EqualTo(23));

            var tbClass2 = new TestBool1_Flags();

            ms = new MemoryStream();

            Serializer.Serialize(ms, tbClass2);

            b = ms.ToArray();

            Console.WriteLine($"b.Length {b.Length}");
            Console.WriteLine($"b {ByteArrayHelper.ByteArrayToString(b)}");
            //Assert.That(b.Length, Is.EqualTo(13));
        }
예제 #2
0
        protected override void AdsResponseIsChanged()
        {
            adsDeviceInfo = new AdsDeviceInfo();
            adsDeviceInfo.MajorVersion = this.AdsResponse[4];
            adsDeviceInfo.MinorVersion = this.AdsResponse[5];
            adsDeviceInfo.VersionBuild = BitConverter.ToUInt16(this.AdsResponse, 6);
            var deviceNameArray = new byte[16];

            Array.Copy(this.AdsResponse, 8, deviceNameArray, 0, 16);
            adsDeviceInfo.DeviceName = ByteArrayHelper.ByteArrayToString(deviceNameArray);
        }
예제 #3
0
        public void Serialize_RootEntry()
        {
            re1 = CommonEntryTest_TraverseTree.NewTestRootEntry(out de2a, out de2b, out de2c, out de3a, out de4a);

            var ms = new MemoryStream();

            Serializer.Serialize(ms, re1);
            var b = ms.ToArray();

            Console.WriteLine($"b.Length {b.Length}");
            Console.WriteLine($"b {ByteArrayHelper.ByteArrayToString(b)}");
            Assert.That(b.Length, Is.EqualTo(118));
        }
예제 #4
0
파일: ProtobufTest.cs 프로젝트: CDEApp/CDE
        public void Serialize_TestByte_CheckLength()
        {
            var tbClass = new TestByte();

            var ms = new MemoryStream();

            Serializer.Serialize(ms, tbClass);
            ms.Close();
            var b = ms.ToArray();

            Console.WriteLine($"b.Length {b.Length}");
            Console.WriteLine($"b {ByteArrayHelper.ByteArrayToString(b)}");
            Assert.That(b.Length, Is.EqualTo(2));
        }
예제 #5
0
파일: ProtobufTest.cs 프로젝트: CDEApp/CDE
        public void Serialize_TestStringIs15_CheckLength()
        {
            var tbClass = new TestStringField();

            tbClass.f1 = "Wubba0123456789";

            var ms = new MemoryStream();

            Serializer.Serialize(ms, tbClass);

            var b = ms.ToArray();

            Console.WriteLine($"b.Length {b.Length}");
            Console.WriteLine($"b {ByteArrayHelper.ByteArrayToString(b)}");
            Assert.That(b.Length, Is.EqualTo(22));
        }
예제 #6
0
파일: Hash16_Test.cs 프로젝트: CDEApp/CDE
        public void Constructor_ValueComesOutAgainAsExpected_OK()
        {
            // ReSharper disable RedundantExplicitArraySize
            var b = new byte[16]
            {
                0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88,
                0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00,
            };
            // ReSharper restore RedundantExplicitArraySize
            var s1 = ByteArrayHelper.ByteArrayToString(b);
            var s2 = new Hash16(b).HashAsString;

            Console.WriteLine($"s1 {s1}");
            Console.WriteLine($"s2 {s2}");
            Assert.That(s2, Is.EqualTo("ffeeddccbbaa99887766554433221100"));
        }
예제 #7
0
        private IList <IAdsSymhandle> GetSymbolsFromBytes(byte[] data)
        {
            int symbolStartPos = 0;
            var symbols        = new List <IAdsSymhandle>();

            while (symbolStartPos < data.Length)
            {
                int           pos    = symbolStartPos;
                IAdsSymhandle symbol = new AdsSymhandle();

                int readLength = (int)BitConverter.ToUInt32(data, pos);
                symbolStartPos += readLength;

                symbol.IndexGroup  = BitConverter.ToUInt32(data, pos + 4);
                symbol.IndexOffset = BitConverter.ToUInt32(data, pos + 8);
                //symbol.Size = BitConverter.ToUInt32(result.Data, pos + 12);
                //symbol.Type = BitConverter.ToUInt32(result.Data, pos + 16);  ADST_... type constants

                //BitConverter.ToUInt32(result.Data, pos + 20); ???

                UInt16 nameLength = BitConverter.ToUInt16(data, pos + 24);
                nameLength++;
                UInt16 typeLength = BitConverter.ToUInt16(data, pos + 26);
                typeLength++;
                UInt16 commentLength = BitConverter.ToUInt16(data, pos + 28);
                commentLength++;

                byte[] nameBytes = new byte[nameLength];
                Array.Copy(data, pos + 30, nameBytes, 0, nameLength);
                pos           += 30 + nameLength;
                symbol.VarName = ByteArrayHelper.ByteArrayToString(nameBytes);

                byte[] typeBytes = new byte[typeLength];
                Array.Copy(data, pos, typeBytes, 0, typeLength);
                pos            += typeLength;
                symbol.TypeName = ByteArrayHelper.ByteArrayToString(typeBytes);

                byte[] commentBytes = new byte[commentLength];
                Array.Copy(data, pos, commentBytes, 0, commentLength);
                pos           += commentLength;
                symbol.Comment = ByteArrayHelper.ByteArrayToString(commentBytes);

                symbols.Add(symbol);
            }

            return(symbols);
        }
예제 #8
0
        /// <summary>
        /// Get an xml description of the plc
        /// You can use XDocument.Parse(xml).ToString() to make the xml more readable
        /// </summary>
        /// <returns></returns>
        public string GetTargetDesc()
        {
            var amsSpecial = new Ams(ams.AmsSocket);

            amsSpecial.AmsNetIdSource = ams.AmsNetIdSource;
            amsSpecial.AmsNetIdTarget = ams.AmsNetIdTarget;
            amsSpecial.AmsPortTarget  = 10000;
            AdsReadCommand adsCommand = new AdsReadCommand(0x000002bc, 0x00000001, 4);
            var            result     = adsCommand.Run(amsSpecial);
            uint           length     = BitConverter.ToUInt32(result.Data, 0);

            adsCommand = new AdsReadCommand(0x000002bc, 0x00000001, length);
            result     = adsCommand.Run(amsSpecial);
            string xml = ByteArrayHelper.ByteArrayToString(result.Data);

            return(xml);
        }
예제 #9
0
        public void DeSerialize_JustRootEntryThatHasTree()
        {
            re1 = CommonEntryTest_TraverseTree.NewTestRootEntry(out de2a, out de2b, out de2c, out de3a, out de4a);

            var ms = new MemoryStream();

            //Serializer.Serialize(ms, re1);
            Serializer.SerializeWithLengthPrefix(ms, re1, PrefixStyle.Base128, 1);
            Serializer.SerializeWithLengthPrefix(ms, re1, PrefixStyle.Base128, 1);

            var b = ms.ToArray();

            Console.WriteLine($"b.Length {b.Length}");
            Console.WriteLine($"b {ByteArrayHelper.ByteArrayToString(b)}");

            var newMS = new MemoryStream(b);

            // var test = Serializer.ListItemTag;
            var iter  = Serializer.DeserializeItems <RootEntry>(newMS, PrefixStyle.Base128, 1);
            var first = iter.FirstOrDefault();

            if (first != null)
            {
                Console.WriteLine("first.RootPath " + first.Path);
            }

            var second = iter.FirstOrDefault();

            if (second != null)
            {
                Console.WriteLine("second.RootPath " + second.Path);
            }
            var third = iter.FirstOrDefault();

            if (third != null)
            {
                Console.WriteLine("third.RootPath " + third.Path);
            }
            else
            {
                Console.WriteLine("third.RootPath doesnt exist.");
            }
        }
예제 #10
0
파일: ProtobufTest.cs 프로젝트: CDEApp/CDE
        public void Serialize_TestByteArray10Long_CheckLength()
        {
            var tbClass = new TestByteClass();

            tbClass.f1 = new byte[10];
            for (int i = 0; i < 10; i++)
            {
                tbClass.f1[i] = (byte)(16 + i);
            }

            var ms = new MemoryStream();

            Serializer.Serialize(ms, tbClass);

            var b = ms.ToArray();

            Console.WriteLine($"b.Length {b.Length}");
            Console.WriteLine($"b {ByteArrayHelper.ByteArrayToString(b)}");
            Assert.That(b.Length, Is.EqualTo(17));
        }
예제 #11
0
        /// <summary>
        /// Get the current routes
        /// </summary>
        /// <returns></returns>
        public IList <string> GetCurrentRoutes()
        {
            var amsSpecial = new Ams(ams.AmsSocket);

            amsSpecial.AmsNetIdSource = ams.AmsNetIdSource;
            amsSpecial.AmsNetIdTarget = ams.AmsNetIdTarget;
            amsSpecial.AmsPortTarget  = 10000;
            bool ok     = true;
            uint index  = 0;
            var  routes = new List <string>();

            while (ok)
            {
                try
                {
                    AdsReadCommand adsCommand = new AdsReadCommand(0x00000323, index++, 0x0800);
                    var            result     = adsCommand.Run(amsSpecial);
                    int            length     = result.Data.Length - 44;
                    byte[]         routeBytes = new byte[length];
                    Array.Copy(result.Data, 44, routeBytes, 0, length);
                    string routeString   = ByteArrayHelper.ByteArrayToString(routeBytes);
                    int    stringLlength = routeString.Length + 1;
                    Array.Copy(routeBytes, stringLlength, routeBytes, 0, length - stringLlength);
                    routeString += " " + ByteArrayHelper.ByteArrayToString(routeBytes);
                    routes.Add(routeString);
                }
                catch (AdsException ex)
                {
                    if (ex.ErrorCode == 1814)
                    {
                        ok = false;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            return(routes);
        }
예제 #12
0
파일: Hash16_Test.cs 프로젝트: CDEApp/CDE
        public void HashAsString_Init1_OK()
        {
            // ReSharper disable RedundantExplicitArraySize
            var b = new byte[16]
            {
                0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88,
                0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00,
            };

            // ReSharper restore RedundantExplicitArraySize

            byte[] hash;
            using (var md5 = MD5.Create())
            {
                hash = md5.ComputeHash(b);
            }
            var h16 = new Hash16(hash);

            Console.WriteLine($"h16.HashAsString {h16.HashAsString}");
            Console.WriteLine($"         old hex {ByteArrayHelper.ByteArrayToString(hash)}");

            Assert.That(h16.HashAsString, Is.EqualTo("a4bd60352d683c3eac5f826528bbfd02"));
        }