Example #1
0
 /** Decodes a packet and returns any data returned. */
 public static S7Data[] decodeMultiPacket(byte[] packet, int count)
 {
     try {
         S7Data[] data = new S7Data[count];
         for (int a = 0; a < count; a++)
         {
             data[a] = new S7Data();
         }
         int  offset = 0;
         TPKT tpkt   = new TPKT();
         tpkt.read(packet, offset);
         offset += tpkt.size();
         COTP cotp = new COTP();
         cotp.read(packet, offset);
         if (cotp.PDU_type == COTP.type_connect)
         {
             return(data);
         }
         if (cotp.PDU_type == COTP.type_connect_ack)
         {
             return(data);
         }
         offset += cotp.size();
         S7Header header = new S7Header();
         header.read(packet, offset);
         offset += header.size();
         S7Params _params = new S7Params();
         _params.read(packet, offset, data);
         return(data);
     } catch (Exception e) {
         Console.WriteLine(e.ToString());
     }
     return(null);
 }
Example #2
0
        /** Creates a packet to connect at COTP level (connect step1). */
        public static byte[] makeConnectPacket1()
        {
            TPKT tpkt = new TPKT();
            COTP cotp = new COTP(COTP.type_connect);

            byte[] data;

            int size = tpkt.size() + cotp.size();

            data = new byte[size];
            int dataoff = 0;

            tpkt.write(data, dataoff, (short)size);
            dataoff += tpkt.size();
            cotp.write(data, dataoff);
            return(data);
        }
Example #3
0
        /** Creates a packet to read data from S7. */
        public static byte[] makeReadPacket(S7Data[] s7)
        {
            TPKT     tpkt    = new TPKT();
            COTP     cotp    = new COTP(COTP.type_data);
            S7Header header  = new S7Header();
            S7Params _params = new S7Params();

            byte[] data;

            _params.makeRead(s7);
            int size = tpkt.size() + cotp.size() + header.size() + _params.size();

            data = new byte[size];
            int dataoff = 0;

            tpkt.write(data, dataoff, (short)size);
            dataoff += tpkt.size();
            cotp.write(data, dataoff);
            dataoff += cotp.size();
            header.write(data, dataoff, (short)_params.size(), (short)0);
            dataoff += header.size();
            _params.write(data, dataoff);
            return(data);
        }
Example #4
0
        /** Creates a packet to write data to S7. */
        public static byte[] makeWritePacket(S7Data type)
        {
            TPKT     tpkt    = new TPKT();
            COTP     cotp    = new COTP(COTP.type_data);
            S7Header header  = new S7Header();
            S7Params _params = new S7Params();

            byte[] data;

            _params.makeWrite(type.block_type, type.block_number, type.data_type, type.offset, type.length, type.data);
            int size = tpkt.size() + cotp.size() + header.size() + _params.size();

            data = new byte[size];
            int dataoff = 0;

            tpkt.write(data, dataoff, (short)size);
            dataoff += tpkt.size();
            cotp.write(data, dataoff);
            dataoff += cotp.size();
            header.write(data, dataoff, (short)(_params.size() - 4 - type.length), (short)(4 + type.length));
            dataoff += header.size();
            _params.write(data, dataoff);
            return(data);
        }