示例#1
0
        /// <summary>
        /// Create the bytes-package to request data from the PLC. You have to specify the memory type (dataType),
        /// the address of the memory, the address of the byte and the bytes count.
        /// </summary>
        /// <param name="dataType">MemoryType (DB, Timer, Counter, etc.)</param>
        /// <param name="db">Address of the memory to be read</param>
        /// <param name="startByteAdr">Start address of the byte</param>
        /// <param name="count">Number of bytes to be read</param>
        /// <returns></returns>
        private static void BuildReadDataRequestPackage(System.IO.MemoryStream stream, DataType dataType, int db, int startByteAdr, int count = 1)
        {
            //single data req = 12
            stream.WriteByteArray(new byte[] { 0x12, 0x0a, 0x10 });
            switch (dataType)
            {
            case DataType.Timer:
            case DataType.Counter:
                stream.WriteByte((byte)dataType);
                break;

            default:
                stream.WriteByte(0x02);
                break;
            }

            stream.WriteByteArray(Word.ToByteArray((ushort)(count)));
            stream.WriteByteArray(Word.ToByteArray((ushort)(db)));
            stream.WriteByte((byte)dataType);
            var overflow = (int)(startByteAdr * 8 / 0xffffU); // handles words with address bigger than 8191

            stream.WriteByte((byte)overflow);
            switch (dataType)
            {
            case DataType.Timer:
            case DataType.Counter:
                stream.WriteByteArray(Types.Word.ToByteArray((ushort)(startByteAdr)));
                break;

            default:
                stream.WriteByteArray(Types.Word.ToByteArray((ushort)((startByteAdr) * 8)));
                break;
            }
        }
示例#2
0
 /// <summary>
 /// Creates the header to read bytes from the PLC
 /// </summary>
 /// <param name="amount"></param>
 /// <returns></returns>
 private static void BuildHeaderPackage(System.IO.MemoryStream stream, int amount = 1)
 {
     //header size = 19 bytes
     stream.WriteByteArray(new byte[] { 0x03, 0x00 });
     //complete package size
     stream.WriteByteArray(Types.Int.ToByteArray((short)(19 + (12 * amount))));
     stream.WriteByteArray(new byte[] { 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00 });
     //data part size
     stream.WriteByteArray(Types.Word.ToByteArray((ushort)(2 + (amount * 12))));
     stream.WriteByteArray(new byte[] { 0x00, 0x00, 0x04 });
     //amount of requests
     stream.WriteByte((byte)amount);
 }