public void buildPkt(ref List <byte> dnpPkt, byte fn) { APDU myApdu = new APDU(); TPDU myTpdu = new TPDU(); if (fn == (byte)APDU.functionCode.READ) { //params should be in the following order for read //Confirm, Unsolicited, function, group, variation, prefixQualifier, [range] OR [start index, stop index] //myApdu.buildAPDU(ref dnpPkt, 0x00, 0x00, 0x01, 0x0A, 0x01, 0x00, 0x00, 0x02, 0x01, 0x01, 0x01); myApdu.buildAPDU(ref dnpPkt, 0x00, 0x00, fn, 0x01, 0x01, 0x00, 0x00, 0x02); } else if (fn == (byte)APDU.functionCode.WRITE) { //params should be in the following order for write //Confirm, Unsolicited, function, group, variation, prefixQualifier, [range] OR [start index, stop index], [Data-1],[Data-2] etc //myApdu.buildAPDU(ref dnpPkt, 0x00, 0x00, 0x01, 0x0A, 0x01, 0x00, 0x00, 0x02, 0x01, 0x01, 0x01); myApdu.buildAPDU(ref dnpPkt, 0x00, 0x00, fn, 0x01, 0x01, 0x00, 0x00, 0x02, dataToSend[0], dataToSend[1], dataToSend[2]); //hardcode data for now } myTpdu.buildTPDU(ref dnpPkt); DPDU myDpdu = new DPDU(); myDpdu.buildDPDU(ref dnpPkt, 0xC4, 1, 65519); //dst=1, src=65519 }
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); } } } }