/// <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); }
public void SendValues() { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); int numberOfDatagrams = this.CountNumberOfDatagrams(dataType.Count) + 1; int numberOfTagsInDatagram = 0; int datagramCounter = 0; int tagCounter = 0; numberOfTags = dataType.Count; byte[] send_buffer; CTelegram cTelegramSend = new CTelegram(); IPAddress broadcast = IPAddress.Parse(ipAddress); IPEndPoint endPoint = new IPEndPoint(broadcast, port); s.EnableBroadcast = true; for (int k = 0; k < numberOfDatagrams; k++) { if (sendCounter >= 65535) { sendCounter = 0; } else { sendCounter++; } numberOfTagsInDatagram = 0; tagCounter++; numberOfTagsInDatagram++; while ((this.CountNumberOfDatagrams(tagCounter) == datagramCounter) & (tagCounter != dataType.Count)) { numberOfTagsInDatagram++; tagCounter++; } if (this.CountNumberOfDatagrams(tagCounter) > datagramCounter) { tagCounter--; numberOfTagsInDatagram--; } #region createTelegramInformatiom send_buffer = new byte[20]; UInt32[] uintarray = new UInt32[1]; Int32[] intarray = new Int32[1]; UInt16[] uint16array = new UInt16[1]; Int16[] int16array = new Int16[1]; byte[] bytearray = new byte[1]; sbyte[] sbytearray = new sbyte[1]; float[] floatarray = new float[1]; double[] doublearray = new double[1]; Buffer.BlockCopy(cTelegramSend.Identity, 0, send_buffer, 0, 4); uintarray[0] = cTelegramSend.ID; Buffer.BlockCopy(uintarray, 0, send_buffer, 4, 4); cTelegramSend.Index = (ushort)cobID; uint16array[0] = cTelegramSend.Index; Buffer.BlockCopy(uint16array, 0, send_buffer, 8, 2); cTelegramSend.SubIndex = (ushort)datagramCounter; uint16array[0] = cTelegramSend.SubIndex; Buffer.BlockCopy(uint16array, 0, send_buffer, 10, 2); cTelegramSend.Items = (ushort)numberOfTagsInDatagram; uint16array[0] = cTelegramSend.Items; Buffer.BlockCopy(uint16array, 0, send_buffer, 12, 2); cTelegramSend.Counter = (ushort)sendCounter; uint16array[0] = cTelegramSend.Counter; Buffer.BlockCopy(uint16array, 0, send_buffer, 16, 2); send_buffer[18] = cTelegramSend.Flags; send_buffer[19] = cTelegramSend.Checksum; int byteCount = 0; for (int i = 0; i < numberOfTagsInDatagram; i++) { switch (dataType[i + this.CountNumberOfDatagrams(tagCounter)].DataTypes) { case DataTypes.booltype: Array.Resize(ref send_buffer, send_buffer.Length + 1); send_buffer[20 + byteCount] = Convert.ToByte(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); byteCount++; break; case DataTypes.bytetype: Array.Resize(ref send_buffer, send_buffer.Length + 1); send_buffer[20 + byteCount] = Convert.ToByte(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); byteCount++; break; case DataTypes.wordtype: Array.Resize(ref send_buffer, send_buffer.Length + 2); uint16array[0] = Convert.ToUInt16(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); Buffer.BlockCopy(uint16array, 0, send_buffer, 20 + byteCount, 2); byteCount = byteCount + 2; break; case DataTypes.dwordtype: Array.Resize(ref send_buffer, send_buffer.Length + 4); uintarray[0] = Convert.ToUInt32(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); Buffer.BlockCopy(uintarray, 0, send_buffer, 20 + byteCount, 4); byteCount = byteCount + 4; break; case DataTypes.sinttype: Array.Resize(ref send_buffer, send_buffer.Length + 1); sbytearray[0] = Convert.ToSByte(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); Buffer.BlockCopy(sbytearray, 0, send_buffer, 20 + byteCount, 1); byteCount = byteCount + 1; break; case DataTypes.usintType: Array.Resize(ref send_buffer, send_buffer.Length + 1); send_buffer[20 + byteCount] = Convert.ToByte(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); byteCount++; break; case DataTypes.inttype: Array.Resize(ref send_buffer, send_buffer.Length + 2); int16array[0] = Convert.ToInt16(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); Buffer.BlockCopy(int16array, 0, send_buffer, 20 + byteCount, 2); byteCount = byteCount + 2; break; case DataTypes.uinttype: Array.Resize(ref send_buffer, send_buffer.Length + 2); uint16array[0] = Convert.ToUInt16(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); Buffer.BlockCopy(uint16array, 0, send_buffer, 20 + byteCount, 2); byteCount = byteCount + 2; break; case DataTypes.dinttype: Array.Resize(ref send_buffer, send_buffer.Length + 4); intarray[0] = Convert.ToInt32(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); Buffer.BlockCopy(intarray, 0, send_buffer, 20 + byteCount, 4); byteCount = byteCount + 4; break; case DataTypes.udinttype: Array.Resize(ref send_buffer, send_buffer.Length + 4); uintarray[0] = Convert.ToUInt32(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); Buffer.BlockCopy(uintarray, 0, send_buffer, 20 + byteCount, 4); byteCount = byteCount + 4; break; case DataTypes.realtype: Array.Resize(ref send_buffer, send_buffer.Length + 4); floatarray[0] = (float)Convert.ToDouble(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); Buffer.BlockCopy(floatarray, 0, send_buffer, 20 + byteCount, 4); byteCount = byteCount + 4; break; case DataTypes.lrealtype: Array.Resize(ref send_buffer, send_buffer.Length + 8); doublearray[0] = Convert.ToDouble(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue); Buffer.BlockCopy(doublearray, 0, send_buffer, 20 + byteCount, 8); byteCount = byteCount + 8; break; case DataTypes.stringtype: char[] charArray = new char[dataType[i + this.CountNumberOfDatagrams(tagCounter)].FieldLength + 1]; byte[] byteArray = new byte[dataType[i + this.CountNumberOfDatagrams(tagCounter)].FieldLength + 1]; charArray = Convert.ToString(dataType[i + this.CountNumberOfDatagrams(tagCounter)].SendValue).ToCharArray(); int oldLength = charArray.Length; if (charArray.Length <= dataType[i + this.CountNumberOfDatagrams(tagCounter)].FieldLength) { Array.Resize(ref charArray, (dataType[i + this.CountNumberOfDatagrams(tagCounter)].FieldLength + 1)); } charArray[oldLength] = (char)0x0; Array.Resize(ref send_buffer, send_buffer.Length + charArray.Length); for (int j = 0; j < charArray.Length; j++) { byteArray[j] = (byte)charArray[j]; } Buffer.BlockCopy(byteArray, 0, send_buffer, 20 + byteCount, (byteArray.Length)); byteCount = byteCount + charArray.Length; break; default: break; } } cTelegramSend.Length = (ushort)send_buffer.Length; uint16array[0] = cTelegramSend.Length; Buffer.BlockCopy(uint16array, 0, send_buffer, 14, 2); #endregion s.SendTo(send_buffer, endPoint); datagramCounter++; } }