Example #1
0
        public void processAPDURead(List <byte> apduPkt)
        {
            stationConsole.Text += "processAPDURead" + Environment.NewLine;
            CancellationToken ct;
            //We know this is a read, we need to find out what the indices are and return the values
            //we also need to know the Group and Variation to perform the action
            //we need to know the qualifier depending on this we make sense of the range values
            APDU        responseAPDU = new APDU(); //prepare to respond
            TPDU        responseTPDU = new TPDU();
            DPDU        responseDPDU = new DPDU();
            List <byte> dnpResponse  = new List <byte>();

            Console.Write("processAPDURead" + Environment.NewLine);
            FormBasedTCPListenMaster.APDU.groupID        group       = (FormBasedTCPListenMaster.APDU.groupID)apduPkt[2];
            FormBasedTCPListenMaster.APDU.variationID    var         = (FormBasedTCPListenMaster.APDU.variationID)apduPkt[3];
            FormBasedTCPListenMaster.APDU.prefixAndRange prefixRange = (FormBasedTCPListenMaster.APDU.prefixAndRange)apduPkt[4];

            if (group == FormBasedTCPListenMaster.APDU.groupID.G1)
            {
                if (var == FormBasedTCPListenMaster.APDU.variationID.V1)
                {
                    //this is a group 1 variation 1, report the state of one or more binary points
                    //since this is a read function code, we need to return values of the binary points

                    //now get the qualifier
                    if (prefixRange == FormBasedTCPListenMaster.APDU.prefixAndRange.IndexOneOctetObjectSize)
                    {
                        //this means range field contains one octet count of objects and
                        //each object is preceded by its index
                    }
                    else if (prefixRange == FormBasedTCPListenMaster.APDU.prefixAndRange.NoIndexOneOctetStartStop)
                    {
                        //this means objects are NOT preceded by an index but the range field contains
                        //one start octet with start index value and one stop octet with stop index value
                        //example 00-02-01-01-01  means start index is 00, end index is 02
                        //index 00 = 01, index 01=01 index 02=01
                        byte startIndex = apduPkt[5];
                        byte stopIndex  = apduPkt[6];

                        //params should be in the following order
                        //Confirm, Unsolicited, function, group, variation, prefixQualifier, [range] OR [start index, stop index]
                        responseAPDU.buildAPDU(ref dnpResponse, 0x00, 0x00, 0x81, (byte)group, (byte)var, (byte)prefixRange, startIndex, stopIndex, apdu.binaryOutput[0],
                                               apdu.binaryOutput[1], apdu.binaryOutput[2]);

                        responseTPDU.buildTPDU(ref dnpResponse);
                        responseDPDU.buildDPDU(ref dnpResponse, 0xC4, 65519, 1); //dst=1, src=65519
                        byte[] msgBytes = dnpResponse.ToArray();

                        string msg = BitConverter.ToString(msgBytes);
                        stationConsole.Text += msg + Environment.NewLine;
                        //await sendReadData(msgBytes, ct);
                    }
                }
            }
        }
Example #2
0
        public void processAPDUWrite(List <byte> apduPkt)
        {
            //We know this is a write, we need to find out what the indices are and values
            //we also need to know the Group and Variation to perform the action
            //we need to know the qualifier depending on this we make sense of the range values
            stationConsole.Text += "processAPDUWrite" + Environment.NewLine;
            FormBasedTCPListenMaster.APDU.groupID        group       = (FormBasedTCPListenMaster.APDU.groupID)apduPkt[2];
            FormBasedTCPListenMaster.APDU.variationID    var         = (FormBasedTCPListenMaster.APDU.variationID)apduPkt[3];
            FormBasedTCPListenMaster.APDU.prefixAndRange prefixRange = (FormBasedTCPListenMaster.APDU.prefixAndRange)apduPkt[4];

            if (group == FormBasedTCPListenMaster.APDU.groupID.GA)
            {
                if (var == FormBasedTCPListenMaster.APDU.variationID.V1)
                {
                    //this is a group 10 variation 1, control or report the state of one or more binary points
                    //since this is a write function code, we need to write values into the binary points

                    //now get the qualifier
                    if (prefixRange == FormBasedTCPListenMaster.APDU.prefixAndRange.IndexOneOctetObjectSize)
                    {
                        //this means range field contains one octet count of objects and
                        //each object is preceded by its index
                    }
                    else if (prefixRange == FormBasedTCPListenMaster.APDU.prefixAndRange.NoIndexOneOctetStartStop)
                    {
                        //this means objects are NOT preceded by an index but the range field contains
                        //one start octet with start index value and one stop octet with stop index value
                        //example 00-02-01-01-01  means start index is 00, end index is 01
                        //index 00 = 01, index 01=01 index 02=01
                        byte startIndex = apduPkt[5];
                        byte stopIndex  = apduPkt[6];
                        int  maxObjects = stopIndex - startIndex; //number of values needed
                        int  apduCount  = apduPkt.Count();        // total elements
                        if (apduCount < (7 + maxObjects))
                        {
                            Console.WriteLine("Error, insufficient objects");
                        }
                        else
                        {
                            //get the object values and write it in the database
                            for (byte i = startIndex, j = 0; i <= stopIndex; i++)
                            {
                                apdu.binaryOutput[startIndex + i] = apduPkt[7 + j];
                                j++;
                            }


                            //updateRadios(apdu.binaryOutput);
                        }
                    }
                }
            }
        } //processAPDUWrite