コード例 #1
0
        public static void Main(string[] args)
        {
            NetVars srCodesysWriteVars = null;
            NetVars srCodesysNetVars   = null;

            srCodesysWriteVars          = new NetVars();
            srCodesysWriteVars.IPAdress = "192.168.178.34";
            srCodesysWriteVars.CobID    = 2;
            CDataTypeCollection collect;

            //srCodesysWriteVars.Port = 1203;

            collect = new CDataTypeCollection(-35, EasyNetVars.DataTypes.inttype);
            collect.VariableName = "rcvInt";
            srCodesysWriteVars.dataTypeCollection.Add(collect);

            collect = new CDataTypeCollection(0.123, EasyNetVars.DataTypes.realtype);
            collect.VariableName = "rcvReal";
            srCodesysWriteVars.dataTypeCollection.Add(collect);

            collect = new CDataTypeCollection(0x1010, EasyNetVars.DataTypes.wordtype);
            collect.VariableName = "rcvWord";
            srCodesysWriteVars.dataTypeCollection.Add(collect);

            collect = new CDataTypeCollection(true, EasyNetVars.DataTypes.booltype);
            collect.VariableName = "rcvBool";
            srCodesysWriteVars.dataTypeCollection.Add(collect);

            for (int i = 0; i < 300; i++)
            {
                collect = new CDataTypeCollection(123, EasyNetVars.DataTypes.bytetype);
                srCodesysWriteVars.dataTypeCollection.Add(collect);
            }

            //---nur für Konfiguration SPS erforderlich?
            srCodesysWriteVars.CreateGVLFile("c:\\GVLFile.GVL");
            srCodesysWriteVars.SendValues();

            srCodesysNetVars          = new NetVars();
            srCodesysNetVars.Port     = 1204;
            srCodesysNetVars.IPAdress = "192.168.178.34";

            for (int i = 0; i < 514; i++)
            {
                srCodesysNetVars.dataTypeCollection.Add(new CDataTypeCollection(EasyNetVars.DataTypes.bytetype));
            }
            ArrayList ReceiveVar = srCodesysNetVars.ReadValues();

            Console.WriteLine(ReceiveVar[513].ToString());
            Console.ReadKey();
        }
コード例 #2
0
        /// <summary>
        /// Reads values from CoDeSys PLC. Datatypes are listed in "dataTypeCollection".
        /// </summary>
        /// <returns>ArrayList which contains the values from CoDeSys PLC</returns>
        public ArrayList ReadValues()
        {
            numberOfTags = dataType.Count;
            DateTime dt1 = DateTime.Now;
            DateTime dt2;

            byte[] receiveBytes = new byte[12];
            int    elementCount;
            bool   elementZeroDetected = false;
            bool   waitForNextElement  = true;
            int    lastTelegramCount   = 0;
            UInt16 subIndex            = 1;

            returnList = new ArrayList();
            if (udpClient == null)
            {
                this.connect();
            }
            while (((receiveBytes[8] != cobID) | receiveBytes == null | waitForNextElement | elementZeroDetected == false | returnList.Count != dataType.Count))
            {
                dt2 = DateTime.Now;
                if ((dt2.Ticks - dt1.Ticks) > 100000000)
                {
                    throw new Exception("Error receiving UDP-Messages; Check cob-ID, Port and IP-Address");
                }

                receiveBytes = udpClient.Receive(ref iPEndPoint);

                elementCount = 20;
                #region createTelegramInformation
                cTelegramReceive = new CTelegram();
                Buffer.BlockCopy(receiveBytes, 0, cTelegramReceive.Identity, 0, 4);
                UInt32[] uintarray   = new UInt32[1];
                UInt16[] uint16array = new UInt16[1];
                byte[]   bytearray   = new byte[1];

                Buffer.BlockCopy(receiveBytes, 4, uintarray, 0, 4);
                cTelegramReceive.ID = uintarray[0];
                Buffer.BlockCopy(receiveBytes, 8, uint16array, 0, 2);
                cTelegramReceive.Index = uint16array[0];
                Buffer.BlockCopy(receiveBytes, 10, uint16array, 0, 2);
                cTelegramReceive.SubIndex = uint16array[0];
                subIndex = cTelegramReceive.SubIndex;
                Buffer.BlockCopy(receiveBytes, 12, uint16array, 0, 2);
                cTelegramReceive.Items = uint16array[0];
                Buffer.BlockCopy(receiveBytes, 14, uint16array, 0, 2);
                cTelegramReceive.Length = uint16array[0];
                Buffer.BlockCopy(receiveBytes, 16, uint16array, 0, 2);
                cTelegramReceive.Counter = uint16array[0];
                Buffer.BlockCopy(receiveBytes, 18, bytearray, 0, 1);
                cTelegramReceive.Flags = bytearray[0];
                Buffer.BlockCopy(receiveBytes, 19, bytearray, 0, 1);
                cTelegramReceive.Checksum = bytearray[0];
                cTelegramReceive.Data     = new byte[receiveBytes.Length - 20];
                Buffer.BlockCopy(receiveBytes, 20, cTelegramReceive.Data, 0, cTelegramReceive.Data.Length);
                #endregion
                if ((cTelegramReceive.SubIndex == 0) & (receiveBytes[8] == cobID))
                {
                    elementZeroDetected = true;
                    returnList.Clear();
                    lastTelegramCount = 0;
                }

                if ((lastTelegramCount + 1) != (cTelegramReceive.SubIndex) & lastTelegramCount != 0 & (receiveBytes[8] == cobID))
                {
                    elementZeroDetected = false;
                    returnList.Clear();
                }
                if (cobID == cTelegramReceive.Index)
                {
                    lastTelegramCount = cTelegramReceive.SubIndex;
                }
                if ((lastTelegramCount) != (CountNumberOfDatagrams(returnList.Count) + 1) & lastTelegramCount != 0 & (receiveBytes[8] == cobID))
                {
                    elementZeroDetected = false;
                    returnList.Clear();
                }
                if (receiveBytes[8] == cobID & elementZeroDetected)
                {
                    for (int i = 0; i < cTelegramReceive.Items; i++)
                    {
                        CDataTypeCollection cDataTypeCollection = (CDataTypeCollection)dataType[returnList.Count];
                        if (cDataTypeCollection.DataTypes == DataTypes.booltype)
                        {
                            bool boolValue = Convert.ToBoolean(receiveBytes[elementCount]);
                            returnList.Add(boolValue);
                            elementCount = elementCount + 1;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.bytetype)
                        {
                            byte byteValue = receiveBytes[elementCount];
                            returnList.Add(byteValue);
                            elementCount = elementCount + 1;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.wordtype)
                        {
                            UInt16 wordValue = Convert.ToUInt16(receiveBytes[elementCount]
                                                                | (receiveBytes[elementCount + 1] << 8));
                            returnList.Add(wordValue);
                            elementCount = elementCount + 2;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.dwordtype)
                        {
                            UInt32 dwordValue = ((UInt32)receiveBytes[elementCount]
                                                 | ((UInt32)receiveBytes[elementCount + 1] << 8)
                                                 | ((UInt32)receiveBytes[elementCount + 2] << 16)
                                                 | ((UInt32)receiveBytes[elementCount + 3] << 24));
                            returnList.Add(dwordValue);
                            elementCount = elementCount + 4;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.sinttype)
                        {
                            sbyte sintValue = (sbyte)receiveBytes[elementCount];
                            returnList.Add(sintValue);
                            elementCount = elementCount + 1;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.usintType)
                        {
                            byte usintValue = receiveBytes[elementCount];
                            returnList.Add(usintValue);
                            elementCount = elementCount + 1;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.inttype)
                        {
                            Int16 intValue = BitConverter.ToInt16(receiveBytes, elementCount);
                            returnList.Add(intValue);
                            elementCount = elementCount + 2;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.uinttype)
                        {
                            UInt16 usintValue = Convert.ToUInt16(receiveBytes[elementCount]
                                                                 | (receiveBytes[elementCount + 1] << 8));
                            returnList.Add(usintValue);
                            elementCount = elementCount + 2;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.udinttype)
                        {
                            UInt32 udintValue = ((UInt32)receiveBytes[elementCount]
                                                 | ((UInt32)receiveBytes[elementCount + 1] << 8)
                                                 | ((UInt32)receiveBytes[elementCount + 2] << 16)
                                                 | ((UInt32)receiveBytes[elementCount + 3] << 24));
                            returnList.Add(udintValue);
                            elementCount = elementCount + 4;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.dinttype)
                        {
                            Int32 dintValue = BitConverter.ToInt32(receiveBytes, elementCount);
                            returnList.Add(dintValue);
                            elementCount = elementCount + 4;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.realtype)
                        {
                            float floatValue = BitConverter.ToSingle(receiveBytes, elementCount);
                            returnList.Add(floatValue);
                            elementCount = elementCount + 4;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.lrealtype)
                        {
                            double dfloatValue = BitConverter.ToDouble(receiveBytes, elementCount);
                            returnList.Add(dfloatValue);
                            elementCount = elementCount + 8;
                        }
                        if (cDataTypeCollection.DataTypes == DataTypes.stringtype)
                        {
                            string stringValue      = System.Text.Encoding.UTF8.GetString(receiveBytes, elementCount, cDataTypeCollection.FieldLength);
                            int    nullSignPosition = stringValue.Length;
                            for (int j = 0; j < stringValue.Length; j++)
                            {
                                if (receiveBytes[elementCount + j] == 0)
                                {
                                    nullSignPosition = j;
                                    break;
                                }
                            }
                            stringValue = stringValue.Substring(0, nullSignPosition);
                            returnList.Add(stringValue);
                            elementCount = elementCount + cDataTypeCollection.FieldLength + 1;
                        }
                    }
                }

                if (((this.CountNumberOfDatagrams(dataType.Count)) <= subIndex) & elementZeroDetected & receiveBytes[8] == cobID)
                {
                    waitForNextElement = false;
                }
            }
            return(returnList);
        }