void myConnection_PDURecieved(Pdu pdu)
        {
            byte[] pdu_bt = pdu.ToBytes();
            byte[] data = new byte[pdu_bt.Length + 3]; //+3 Bytes (COTP)
            Array.Copy(pdu_bt, 0, data, 3, pdu_bt.Length);

            //COTP
            data[0] = 2;
            data[1] = 0xf0;
            data[2] = 0x80;

            listBox1.Invoke(
                (MethodInvoker) delegate() { listBox1.Items.Add("Send (TCP): " + ByteExtensions.ToHexString(data)); });
            
            
            myTCPServer.SendData(data);
        }
        private Pdu ExchangePdu(Pdu myPdu)
        {
            ushort pduNrInt;
            byte[] ret;
            lock (lockpdu)
            {
                pduNr++;
                if (pduNr >= ushort.MaxValue)
                    pduNr = 1;

                pduNrInt = pduNr;

                myPdu.header.number = pduNr;

                //int res;
                //*(dc->msgOut + 6) = 0x80;
                //*(dc->msgOut + 5) = 0xf0;
                //*(dc->msgOut + 4) = 0x02;
                //_daveSendISOPacket(dc, 3 + p->hlen + p->plen + p->dlen);
                byte[] pdu_b = myPdu.ToBytes();
                byte[] _message = new byte[pdu_b.Length + 3];
                _message[0] = 0x02;
                _message[1] = 0xf0;
                _message[2] = 0x80;
                Array.Copy(pdu_b, 0, _message, 3, pdu_b.Length);
                sendISOPacket(_message);
                ret = ReceiveData();
                if (ret != null && ret.Length == 7)
                {
                    //if (daveDebug & daveDebugByte)
                    //    LOG1("CPU sends funny 7 byte packets.\n");
                    ret = ReceiveData();
                }
                if (ret == null) return new Pdu(); //daveResTimeout;
                /*if (daveDebug & daveDebugExchange)
                {
                    LOG3("%s _daveExchangeTCP res from read %d\n", dc->iface->name, res);
                }*/
                if (ret.Length <= daveConst.ISOTCPminPacketLength) return new Pdu(); //daveResShortPacket; 


                //Interface.ExchangePdu(myPdu, this);
            }

            //Todo: maybe implement a Timeout here!
            //while (RecievedPdus[pduNrInt] == null)
            //{ }
            byte[] res = new byte[ret.Length - 7];
            Array.Copy(ret, 7, res, 0, ret.Length - 7);
            Pdu retVal = new Pdu(res);
            //RecievedPdus.Remove(pduNrInt);
            return retVal;
        }
Esempio n. 3
0
 public void SendPdu(Pdu myPdu)
 {
     Interface.SendPdu(myPdu, this);
 }
Esempio n. 4
0
        public void PLCStop()
        {
            Pdu pdu = new Pdu(1);
            byte[] para = new byte[] {0x29, 0, 0, 0, 0, 0, 9, (byte) 'P', (byte) '_', (byte) 'P', (byte) 'R', (byte) 'O', (byte) 'G', (byte) 'R', (byte) 'A', (byte) 'M'};
            pdu.Param.AddRange(para);

            Pdu rec = ExchangePdu(pdu);
        }
Esempio n. 5
0
        public void PLCCompress()
        {
            Pdu pdu = new Pdu(1);
            byte[] para = new byte[] {0x28, 0, 0, 0, 0, 0, 0, 0xFD, 0, 0, 5, (byte) '_', (byte) 'G', (byte) 'A', (byte) 'R', (byte) 'B'};
            pdu.Param.AddRange(para);

            Pdu rec = ExchangePdu(pdu);
        }
        void myTCPServer_TelegrammRecieved(byte[] telegramm, TcpClient tcpClient)
        {

            //Dann kommt COPT
            //Dann S7 PDU
            byte[] coptPart = new byte[telegramm[0] + 1];
            Array.Copy(telegramm, 0, coptPart, 0, coptPart.Length);

            listBox1.Items.Add("Recieved (TCP): " + ByteExtensions.ToHexString(telegramm));
            try
            {
                if (coptPart[1] == 0xe0 /* 0xe0 = CR = Connection Request */)
                {
                    // Auf Anfrage mit CC Connect Confirm antworten
                    byte[] cc = {
                                    // *** fixed part ***
                                    0x11, // Length 17
                                    0x0D, // 0xD0 = CC Connect Confirm
                                    0x00, 0x01, // 2, 3: Dest.Reference
                                    0x00, 0x01, // 4, 5: Source Reference
                                    0x00, // 6 :Class Option
                                    // *** variable part ***
                                    0xC0, // 7: Param. Code: tdpu-size
                                    0x01, // 8: Param. length 1
                                    0x09, // 9: TPDU size
                                    0xC1, // 10: Param. Code:scr-tsap
                                    0x02, // 11: Param. length 2
                                    0x01, // 12:
                                    0x00, // 13:
                                    0xC2, // 14: Param. Code: dst-tsap
                                    0x02, // 15: Param. length 2
                                    0x03, // 16:
                                    0x02 // 17:
                                };
                    cc[2] = coptPart[4];
                    cc[3] = coptPart[5];
                    listBox1.Items.Add("Send (TCP): " + ByteExtensions.ToHexString(cc));
            
                    myTCPServer.SendData(cc);

                }
                else if (coptPart[1] == 0xf0 /* 0xf0 = DT = Data */)
                {
                    byte[] pduBytes = new byte[telegramm.Length - coptPart.Length];
                    Array.Copy(telegramm, coptPart.Length, pduBytes, 0, pduBytes.Length);
                    Pdu recPdu = new Pdu(pduBytes);

                    if (recPdu.Param[0] == 0xf0)
                    {
                        //Negotiate PDU Length request 
                    }
                    else
                        myConnection.SendPdu(recPdu);
                }
            }
            catch (Exception ex)
            {
                listBox1.Items.Add(ex.Message);
            }

        }
        public int deleteProgramBlock(int blockType, int number)
        {
            //int DECL2 daveDeleteProgramBlock(daveConnection*dc, int blockType, int number) {
            Pdu p = new Pdu();
            byte[] paDelete = {
                	0x28,0,0,0,0,0,0,0xFD,0,
                	0x0a,0x01,0x00,
                	(byte)'0',(byte)'C', //Block type in ASCII (0C = FC)
                	(byte)'0',(byte)'0',(byte)'0',(byte)'0',(byte)'1', //Block Number in ASCII
                	(byte)'B', //Direction?
                	0x05, //Length of Command
                	(byte)'_',(byte)'D',(byte)'E',(byte)'L',(byte)'E' //Command Delete	
                	};

            paDelete[13] = (byte)blockType;
            //sprintf((char*)(paDelete+14),"%05d",number);
            sprintf(paDelete, 14, 5, number);
            paDelete[19] = (byte)'B'; //This is overriden by sprintf via 0x00 as String seperator!

            //p.header=dc->msgOut+dc->PDUstartO;
            //_daveInitPDUheader(&p, 1);
            //_daveAddParam(&p, paDelete, sizeof(paDelete));
            p.Param.AddRange(paDelete);
            //res = _daveExchange(dc, &p);
            var p2 = ExchangePdu(p);
            //    if (res == daveResOK)
            //    {
            //		res=_daveSetupReceivedPDU(dc, &p2);
            //		if (daveDebug & daveDebugPDU) {
            //			_daveDumpPDU(&p2);
            //		}
            //	}

            //Retval of 0x28 in Recieved PDU Parameter Part means delete was sucessfull.
            //This needs to be implemneted and also error Codes Like Block does not exist or block is locked and so on...
            return 0;
        }
 private int daveGetPDUerror(Pdu p)
 {
     if (p.header.type == 2 || p.header.type == 3)
     {
         return ByteFunctions.getU16from(p.header.result, 0);
     }
     else
         return 0;
 }
        private int BuildAndSendPDU(Pdu p2, byte[] pa, byte[] ud, byte[] ud2)
        {
            //int res;
            Pdu p = new Pdu(7);
            byte[] dn;
            //p.header=dc->msgOut+dc->PDUstartO;
            //_daveInitPDUheader(&p, 7);
            //_daveAddParam(&p, pa, psize);
            p.Param.AddRange(pa);

            //_daveAddUserData(&p, ud, usize);
            p.UData.AddRange(ud);
            //    LOG2("*** here we are: %d\n",p.dlen);
            //p3=&p;
            //dn= p3->data+p3->dlen;
            //p3->dlen+=usize2;
            //memcpy(dn, ud2, usize2);
            p.UData.AddRange(ud2);

            //((PDUHeader*)p3->header)->dlen=daveSwapIed_16(p3->dlen);

            Pdu rez = ExchangePdu(p);
            if (rez != null)
            {
                p2.Data = rez.Data;
                p2.Param = rez.Param;
                p2.initUData();

                return p2.testPGReadResult();
            }
            return -1;
        }
        public int _daveBuildAndSendPDU(Pdu myPDU, byte[] para, byte[] data, bool nullD)
        {
            //int res;
            Pdu p = new Pdu(7);
            byte[] nullData = { 0x0a, 0, 0, 0 };
            //p.header=dc->msgOut+dc->PDUstartO;
            //_daveInitPDUheader(&p, 7);

            //_daveAddParam(&p, pa, psize);

            p.Param.AddRange(para);

            if (data != null) p.UData.AddRange(data);
            else
                if (nullD) p.Data.AddRange(nullData);//_daveAddData(&p, nullData, 4);

            Pdu rez = ExchangePdu(p);
            if (rez != null)
            {
                myPDU.Data = rez.Data;
                myPDU.Param = rez.Param;
                myPDU.initUData();

                return myPDU.testPGReadResult();
            }
            return -1;
        }
 public int daveBuildAndSendPDU(Pdu myPDU, byte[] para, byte[] data)
 {
     return _daveBuildAndSendPDU(myPDU, para, data, false);
 }
        public int daveSetPLCTime(DateTime tm)
        {
            byte[] buffer = new byte[] { 0x00, 0x19, 0x05, 0x08, 0x23, 0x04, 0x10, 0x23, 0x67, 0x83, };
            ByteFunctions.putBCD8at(buffer, 2, tm.Year % 100);
            ByteFunctions.putBCD8at(buffer, 3, tm.Month);
            ByteFunctions.putBCD8at(buffer, 4, tm.Day);
            ByteFunctions.putBCD8at(buffer, 5, tm.Hour);
            ByteFunctions.putBCD8at(buffer, 6, tm.Minute);
            ByteFunctions.putBCD8at(buffer, 7, tm.Second);
            ByteFunctions.putBCD8at(buffer, 8, tm.Millisecond / 10);
            ByteFunctions.putBCD8at(buffer, 9, (tm.Millisecond % 10) << 4);

            int res;
            Pdu p2 = new Pdu(1);
            byte[] pa = { 0, 1, 18, 4, 17, (byte)'G', 2, 0 };
            //len=0;
            res = daveBuildAndSendPDU(p2, pa, buffer);
            AnswLen = p2.UData.Count;
            return res;
        }
 public DateTime daveReadPLCTime()
 {
     //	int res, len;
     Pdu p2 = new Pdu(7);
     byte[] pa = { 0, 1, 18, 4, 17, (byte)'G', 1, 0 };
     //	len=0;
     _daveBuildAndSendPDU(p2, pa, null, true);
     /*	if (res==daveResOK) {
             dc->resultPointer=p2.udata;
             dc->_resultPointer=p2.udata;
             len=p2.udlen;
         } else {
             if(daveDebug & daveDebugPrintErrors)
                 LOG3("daveGetTime: %04X=%s\n",res, daveStrerror(res));
         }
         dc->AnswLen=len;*/
     AnswLen = p2.Data.Count - 4;
     int year, month, day, hour, minute, second, millisecond;
     //getU8();
     //getU8();
     //byte[] tmp = new byte[1];
     //tmp[0] = Convert.ToByte(getU8());
     byte[] res = p2.UData.ToArray();
     year = ByteFunctions.getBCD8from(res, 2);
     year += year >= 90 ? 1900 : 2000;
     //tmp[0] = Convert.ToByte(getU8());
     //month = getBCD8from(tmp, 0);
     month = ByteFunctions.getBCD8from(res, 3);
     //tmp[0] = Convert.ToByte(getU8());
     //day = getBCD8from(tmp, 0);
     day = ByteFunctions.getBCD8from(res, 4);
     //tmp[0] = Convert.ToByte(getU8());
     //hour = getBCD8from(tmp, 0);
     hour = ByteFunctions.getBCD8from(res, 5);
     //tmp[0] = Convert.ToByte(getU8());
     //minute = getBCD8from(tmp, 0);
     minute = ByteFunctions.getBCD8from(res, 6);
     //tmp[0] = Convert.ToByte(getU8());
     //second = getBCD8from(tmp, 0);
     second = ByteFunctions.getBCD8from(res, 7);
     //tmp[0] = Convert.ToByte(getU8());
     //millisecond = getBCD8from(tmp, 0) * 10;
     millisecond = ByteFunctions.getBCD8from(res, 8);
     //tmp[0] = Convert.ToByte(getU8());
     //tmp[0] = Convert.ToByte(tmp[0] >> 4);
     //millisecond += getBCD8from(tmp, 0);
     millisecond += ByteFunctions.getBCD8from(res, 9) >> 4;
     return new DateTime(year, month, day, hour, minute, second, millisecond);
 }
        private int NegPDUlengthRequest()
        {
            byte[] pa = { 0xF0, 0, 0, 1, 0, 1, 3, 0xC0 };
            int CpuPduLimit = 0;
            Pdu p1 = new Pdu(1);

            p1.Param.AddRange(pa);

            var p2 = ExchangePdu(p1);
            if (p2 != null)
                CpuPduLimit = ByteFunctions.getU16from(p2.Param.ToArray(), 6);

            // daveGetU16from(p2.param+6);
            return CpuPduLimit;
        }
        public int start()
        {
            Pdu pdu = new Pdu(1);
            byte[] para = new byte[] { 0x28, 0, 0, 0, 0, 0, 0, 0xFD, 0, 0x00, 9, (byte)'P', (byte)'_', (byte)'P', (byte)'R', (byte)'O', (byte)'G', (byte)'R', (byte)'A', (byte)'M' };
            pdu.Param.AddRange(para);

            Pdu rec = ExchangePdu(pdu);

            return 0;
        }
        private int doUpload(ref int more, byte[] buffer, ref int len, int uploadID)
        {
            Pdu p1 = new Pdu();
            int netLen;
            _daveConstructDoUpload(p1, uploadID);
            Pdu ret = ExchangePdu(p1);
            //res=_daveExchange(dc, &p1);
            more = 0;

            //	if(res!=daveResOK) return res;
            //res=_daveSetupReceivedPDU(dc, &p2);
            more = ret.Param[1];
            //if(res!=daveResOK) return res;
            //    netLen=p2.data[1] /* +256*p2.data[0]; */ /* for long PDUs, I guess it is so */;
            netLen = ret.Data[1] + 256 * ret.Data[0]; /* some user confirmed my guess... */;
            if (buffer != null)
            {
                Array.Copy(ret.UData.ToArray(), 0, buffer, len, netLen);
                //memcpy(*buffer,p2.data+4,netLen);
                //*buffer+=netLen;?????????????????????????????????
            }
            len += netLen;
            return 0;
        }
        private int endUpload(int uploadID)
        {
            Pdu p1 = new Pdu();
            int res;

            //p1.header=dc->msgOut+dc->PDUstartO;
            _daveConstructEndUpload(p1,uploadID);
            Pdu ret = ExchangePdu(p1);
            //res=_daveExchange(dc, &p1);
            //if(res!=daveResOK) return res;	
            //res=_daveSetupReceivedPDU(dc, &p2);
            return 0;
        }
        public int daveRecieveData(out byte[] rdata, out byte[] rparam)
        {
            //int res;
            byte[] msgBuffer;
            getGetResponse(out msgBuffer);

            Pdu myPDU = new Pdu(msgBuffer);
            //if (IntPtr.Size == 8)
            //    _daveSetupReceivedPDU64(pointer, myPDU.pointer);
            //else
            //    _daveSetupReceivedPDU32(pointer, myPDU.pointer);

            //byte[] tmp1;// = new byte[65536];
            //byte[] tmp2;// = new byte[65536];
            //int ltmp1 = 0;
            //int ltmp2 = 0;

            //if (IntPtr.Size == 8)
            //    res = daveGetPDUData64(pointer, myPDU.pointer, tmp1, ref ltmp1, tmp2, ref ltmp2);
            //else
            //    res = daveGetPDUData32(pointer, myPDU.pointer, tmp1, ref ltmp1, tmp2, ref ltmp2);
            return /*res =*/ daveGetPDUData(myPDU, out rdata, out rparam);

            //rdata = new byte[ltmp1];
            //rparam = new byte[ltmp2];
            //Array.Copy(tmp1, data, ltmp1);
            //Array.Copy(tmp2, param, ltmp2);
            //return res;
        }
        public int putProgramBlock(int blockType, int blknumber, byte[] buffer, ref int length)
        {
            //int DECL2 davePutProgramBlock(daveConnection * dc, int blockType, int blknumber, char* buffer, int * length) {
            int maxPBlockLen = 0xDe;	// real maximum 222 bytes

            int res = 0;
            int cnt = 0;
            int size = 0;
            int blockNumber, rawLen, netLen, blockCont;
            int number = 0;

            byte[] pup = {			// Load request
            		0x1A,0,1,0,0,0,0,0,9,
            		0x5F,0x30,0x42,0x30,0x30,0x30,0x30,0x34,0x50, // block type code and number
            		//     _    0    B   0     0    0    0    4    P
            		//		SDB		
            		0x0D,
            		0x31,0x30,0x30,0x30,0x32,0x30,0x38,0x30,0x30,0x30,0x31,0x31,0x30,0	// file length and netto length
            		//     1   0     0    0    2    0    8    0    0    0    1    1    0
            	};
            byte[] paInsert = {		// sended after transmission of a complete block,
            	// I guess this makes the CPU link the block into a program.
            	0x28,0,0,0,0,0,0,0xFD,0,0x0A,1,0,0x30,0x42,0x30,0x30,0x30,0x30,0x34,0x50, // block type code and number	
            	0x05,(byte)'_',(byte)'I',(byte)'N',(byte)'S',(byte)'E'
            };

            Pdu p = new Pdu();

            byte[] pablock = {	// parameters for parts of a block
            		0x1B,0
            	};

            byte[] progBlock = new byte[maxPBlockLen + 4];
            progBlock[0] = 0;
            progBlock[1] = (byte)maxPBlockLen;
            progBlock[2] = 0;
            progBlock[3] = 0xFB;
            //{		0,maxPBlockLen,0,0xFB,	// This seems to be a fix prefix for program blocks	};

            pup[11] = (byte)blockType;
            paInsert[13] = (byte)blockType;
            /*pup[12] = number / (10*10*10*10);
            pup[13] = (number - (pup[12] * 10*10*10*10 )) / (10*10*10);
            pup[14] = (number - (pup[13] * 10*10*10)) / (10*10);
            pup[15] = (number - (pup[14] * 10*10)) / (10);
            pup[16] = (number - (pup[15] * 10));
	
            pup[12] = pup[12] + 0x30;
            pup[13] = pup[13] + 0x30;
            pup[14] = pup[14] + 0x30;
            pup[15] = pup[15] + 0x30;
            pup[16] = pup[16] + 0x30;*/

            //memcpy(progBlock+4,buffer,maxPBlockLen);
            Array.Copy(buffer, 0, progBlock, 4, maxPBlockLen);

            progBlock[9] = (byte)(blockType + 0x0A - 'A'); //Convert 'A' to 0x0A
            if (blockType == '8') progBlock[9] = 0x08;

            progBlock[10] = (byte)(blknumber / 0x100);
            progBlock[11] = (byte)(blknumber - (progBlock[10] * 0x100));


            rawLen = ByteFunctions.getU16from(progBlock, 14);
            netLen = ByteFunctions.getU16from(progBlock, 38);

            //sprintf((char*)pup+19,"1%06d%06d",rawLen,netLen);
            pup[19] = (byte)'1';
            sprintf(pup, 20, 6, rawLen);
            sprintf(pup, 26, 6, netLen);

            //sprintf((char*)pup+12,"%05d",blknumber);
            sprintf(pup, 12, 5, blknumber);
            //sprintf((char*)paInsert+14,"%05d",blknumber);
            sprintf(paInsert, 14, 5, blknumber);

            pup[17] = (byte)'P';
            paInsert[19] = (byte)'P';

            //p.header=dc->msgOut+dc->PDUstartO;
            //_daveInitPDUheader(&p, 1);
            //_daveAddParam(&p, pup, sizeof(pup)-1);
            p.Param.AddRange(pup);

            var p2 = ExchangePdu(p);
            //	if (res==daveResOK) {
            //res=_daveSetupReceivedPDU(dc, &p2);
            res = daveGetPDUerror(p2);

            if (res == 0)
            {
                blockCont = 1;
                byte[] msgBuffer;

                res = getGetResponse(out msgBuffer);
                //res=_daveSetupReceivedPDU(dc, &p2);
                p2 = new Pdu(msgBuffer);

                cnt = 0;

                do
                {
                    res = 0;
                    //res=_daveSetupReceivedPDU(dc, &p2);
                    //p2 = new Pdu(msgIn);

                    number = p2.header.number;
                    if (p2.Param[0] == 0x1B)
                    {
                        //READFILE
                        Array.Copy(buffer, cnt * maxPBlockLen, progBlock, 4, maxPBlockLen);
                        //memcpy(progBlock+4,buffer+(cnt*maxPBlockLen),maxPBlockLen);

                        if (cnt == 0)
                        {
                            progBlock[9] = (byte)(blockType + 0x0A - 'A'); //Convert 'A' to 0x0A
                            if (blockType == '8') progBlock[9] = 0x08;

                            progBlock[10] = (byte)(blknumber / 0x100);
                            progBlock[11] = (byte)(blknumber - (progBlock[10] * 0x100));
                        }

                        //p.header=dc->msgOut+dc->PDUstartO;
                        //_daveInitPDUheader(&p, 3);
                        p = new Pdu(3);
                        size = maxPBlockLen;

                        if (length > ((cnt + 1) * maxPBlockLen))
                            pablock[1] = 1;
                        else
                        {
                            size = length - (cnt * maxPBlockLen);
                            pablock[1] = 0;	//last block
                            blockCont = 0;
                        }

                        progBlock[1] = (byte)size;
                        //_daveAddParam(&p, pablock, sizeof(pablock));
                        p.Param.AddRange(pablock);
                        //_daveAddData(&p, progBlock, size + 4 /* size of block) */);
                        byte[] dataA = new byte[size + 4];
                        Array.Copy(progBlock, dataA, size + 4);
                        p.Data.AddRange(dataA);

                        p.header.number = (ushort)number;
                        //_daveExchange(dc,&p);
                        p2 = ExchangePdu(p);
                    }
                    cnt++;
                } while (blockCont != 0);

                //res=_daveSetupReceivedPDU(dc, &p2);
                number = p2.header.number;
                if (p2.Param[0] == 0x1C)
                {
                    //p.header=dc->msgOut+dc->PDUstartO;

                    //_daveInitPDUheader(&p, 3);
                    p = new Pdu(3);
                    //_daveAddParam(&p, p2.param,1);
                    p.Param.AddRange(p2.Param);
                    p.header.number = (ushort)number;
                    //_daveExchange(dc,&p);
                    ExchangePdu(p);

                    //p.header=dc->msgOut+dc->PDUstartO;
                    //_daveInitPDUheader(&p, 1);
                    p = new Pdu(1);
                    //_daveAddParam(&p, paInsert, sizeof(paInsert));
                    p.Param.AddRange(paInsert);
                    //res=_daveExchange(dc, &p);
                    //res=_daveSetupReceivedPDU(dc, &p2);
                    p2 = ExchangePdu(p);
                    res = daveGetPDUerror(p2);
                }
            }
            else
            {
                throw new Exception(string.Format("CPU doesn't accept load request:{0:4X}\n", res));
            }
            return res;
        }
        public int daveGetPDUData(Pdu mPDU, out byte[] rdata, out byte[] rparam)
        {
            Pdu myPDU = mPDU as Pdu;
            //int res = 0;
            //memcpy(data, p2->data, p2->dlen);
            //*ldata = p2->dlen;
            rdata = myPDU.Data.ToArray();
            //Array.Copy(myPDU.Data.ToArray(), rdata, myPDU.Data.Count);
            //memcpy(param, p2->param, p2->plen);
            //*lparam = p2->plen;
            rparam = myPDU.Param.ToArray();

            return 0;
        }
        public int readSZL(int id, int index, byte[] buffer)
        {
            //int  daveReadSZL(daveConnection * dc, int ID, int index, void * buffer, int buflen) {
            int res, len, cpylen;
            byte pa7;
            //    int pa6;
            Pdu p2 = new Pdu();
            byte[] pa = { 0, 1, 18, 4, 17, 68, 1, 0 };
            byte[] da = { 1, 17, 0, 1 };

            byte[] pam = { 0, 1, 18, 8, 18, 68, 1, 1, 0, 0, 0, 0 };
            //    uc dam[]={10,0,0,0};

            da[0] = (byte)(id / 0x100);
            da[1] = (byte)(id % 0x100);
            da[2] = (byte)(index / 0x100);
            da[3] = (byte)(index % 0x100);
            res = daveBuildAndSendPDU(p2, pa, da);
            if (res != Connection.daveResOK) return res; 	// bugfix from Natalie Kather

            len = 0;
            pa7 = p2.Param[7];
            //    pa6=p2.param[6];
            while (p2.Param[9] != 0)
            {
                if (buffer != null)
                {
                    cpylen = p2.UData.Count;
                    if (len + cpylen > buffer.Length) cpylen = buffer.Length - len;
                    if (cpylen > 0) Array.Copy(p2.UData.ToArray(), 0, buffer, len, cpylen);// memcpy((uc *)buffer+len,p2.udata,cpylen);
                }
                //dc->resultPointer=p2.udata;
                //dc->_resultPointer=p2.udata;
                len += p2.UData.Count;
                pam[7] = pa7;
                ////		res=daveBuildAndSendPDU(dc, &p2,pam, sizeof(pam), NULL, sizeof(dam));
                res = _daveBuildAndSendPDU(p2, pam, null, true);
                if (res != Connection.daveResOK) return res; 	// bugfix from Natalie Kather
            }


            if (buffer != null)
            {
                cpylen = p2.UData.Count;
                if (len + cpylen > buffer.Length) cpylen = buffer.Length - len;
                if (cpylen > 0) Array.Copy(p2.UData.ToArray(), 0, buffer, len, cpylen);//memcpy((uc *)buffer+len,p2.udata,cpylen);
            }
            //dc->resultPointer=p2.udata;
            //dc->_resultPointer=p2.udata;
            len += p2.UData.Count;
            AnswLen = len;
            return 0;
        }
        public int ListBlocksOfType(int blockType, byte[] buffer)
        {
            //int DECL2 daveListBlocksOfType(daveConnection * dc,uc type,daveBlockEntry * buf) {
            int res, i, len;
            Pdu p2 = new Pdu();
            //uc * buffer=(uc*)buf;
            byte[] pa = { 0, 1, 18, 4, 17, 67, 2, 0 };
            byte[] da = { (byte)'0', (byte)'0' };
            byte[] pam = { 0, 1, 18, 8, 0x12, 0x43, 2, 1, 0, 0, 0, 0 };
            da[1] = (byte)blockType;
            res = daveBuildAndSendPDU(p2, pa, da);
            if (res != Connection.daveResOK) return -res;
            len = 0;
            while (p2.Param[9] != 0)
            {
                if (buffer != null) Array.Copy(p2.UData.ToArray(), 0, buffer, len, p2.UData.Count);// memcpy(buffer + len, p2.udata, p2.udlen);
                //dc->resultPointer=p2.udata;
                //dc->_resultPointer=p2.udata;
                len += p2.UData.Count;
                //printf("more data\n");
                pam[7] = p2.Param[7];
                res = _daveBuildAndSendPDU(p2, pam, null, true);
                if (res == 0xa) break;
                if (res != Connection.daveResOK) return res;
            }


            //if (res==daveResOK) {
            if (buffer != null) Array.Copy(p2.UData.ToArray(), 0, buffer, len, p2.UData.Count); //memcpy(buffer + len, p2.udata, p2.udlen);
            //dc->resultPointer=p2.udata;
            //dc->_resultPointer=p2.udata;
            len += p2.UData.Count;
            //} else {
            //	if(daveDebug & daveDebugPrintErrors)
            //		LOG3("daveListBlocksOfType: %d=%s\n",res, daveStrerror(res));
            //}
            AnswLen = len;
            res = len / System.Runtime.InteropServices.Marshal.SizeOf(typeof(daveBlockEntry));

            //for (i = 0; i < res; i++)
            //{
            //    buf[i].number=daveSwapIed_16(buf[i].number);
            // }
            return res;
        }
Esempio n. 23
0
        public Pdu ExchangePdu(Pdu myPdu)
        {
            ushort pduNrInt;
            lock (lockpdu)
            {
                while (RecievedPdus.ContainsKey(pduNr))
                {
                    pduNr++;
                    if (pduNr >= ushort.MaxValue)
                        pduNr = 1;
                }

                pduNrInt = pduNr;

                myPdu.header.number = pduNr;

                RecievedPdus.Add(1, null);

                Interface.ExchangePdu(myPdu, this);
            }

            //Todo: maybe implement a Timeout here!
            while (RecievedPdus[pduNrInt] == null)
            { }

            Pdu retVal = RecievedPdus[pduNrInt];
            RecievedPdus.Remove(pduNrInt);
            return retVal;
        }
        private int initUpload(byte blockType, int blockNr, ref int uploadID)
        {
            Pdu p1 = new Pdu(); ;
            //p1.header=dc->msgOut+dc->PDUstartO;
            _daveConstructUpload(p1, blockType, blockNr);
            Pdu ret = ExchangePdu(p1);

            //if(res!=daveResOK) return res;
            //res=daveSetupReceivedPDU(dc, &p2);
            //if(res!=daveResOK) return res;
            uploadID = ret.Param[7];
            return 0;
        }
Esempio n. 25
0
        public void PLCCopyRamToRom()
        {
            Pdu pdu = new Pdu(1);
            byte[] para = new byte[] {0x28, 0, 0, 0, 0, 0, 0, 0xFD, 0, 2, (byte) 'E', (byte) 'P', 5, (byte) '_', (byte) 'M', (byte) 'O', (byte) 'D', (byte) 'U'};
            pdu.Param.AddRange(para);

            Pdu rec = ExchangePdu(pdu);
        }
 /*
 Functions to load blocks from PLC:
 */
 private void _daveConstructUpload(Pdu p, byte blockType, int blockNr)
 {
     byte[] pa =	{0x1d,
          0,0,0,0,0,0,0,9,0x5f,0x30,0x41,48,48,48,48,49,65};
     pa[11] = blockType;
     //sprintf((char*)(pa+12),"%05d",blockNr);
     sprintf(pa, 12, 5, blockNr);
     pa[17] = (byte)'A';
     //_daveInitPDUheader(p,1);
     //_daveAddParam(p, pa, sizeof(pa));
     p.Param.AddRange(pa);
 }
Esempio n. 27
0
        public byte[] ReadSZL(int SZL_ID, int SZL_Index)
        {
            Pdu pdu = new Pdu(1);
            byte[] para_1 = new byte[] {0, 1, 18, 4, 17, 68, 1, 0};
            pdu.Param.AddRange(para_1);

            byte[] user = new byte[4];
            user[0] = (byte) (SZL_ID/0x100);
            user[1] = (byte) (SZL_ID%0x100);
            user[2] = (byte) (SZL_Index/0x100);
            user[3] = (byte) (SZL_Index%0x100);
            pdu.Data.AddRange(user);

            Pdu rec = ExchangePdu(pdu);

            byte[] para_2 = {0, 1, 18, 8, 18, 68, 1, 1, 0, 0, 0, 0};
            para_2[7] = rec.Param[7];
            pdu.Param.Clear();
            pdu.Param.AddRange(para_2);

            List<byte> retVal = new List<byte>();

            while (rec.Param[9] != 0)
            {
                retVal.AddRange(rec.UData);
                rec = ExchangePdu(pdu);
            }

            retVal.AddRange(rec.UData);

            return retVal.ToArray();
        }
 private void _daveConstructEndUpload(Pdu p, int uploadID)
 {
     byte[] pa = { 0x1f, 0, 0, 0, 0, 0, 0, 1 };
     pa[7] = (byte)uploadID;
     //_daveInitPDUheader(p,1);
     //_daveAddParam(p, pa, sizeof(pa));
     p.Param.AddRange(pa);
 }
Esempio n. 29
0
        internal void SetRecievedPdu(Pdu data)
        {
            if (PDURecieved != null)
                PDURecieved(data);

            if (data.header.number != 0)
                RecievedPdus[data.header.number] = data;
            else
            {
                //Asynchronous Data arrived, call Event
                //Event should be used in PLC Connection!
            }
        }
        public int force200(int area, int start, int val)
        {
            //int res;
            Pdu p2 = new Pdu();
            //    uc pa[]={0,1,18,4,17,67,2,0};
            //    uc da[]={'0','0'};

            //32,7,0,0,0,0,0,c,0,16,

            byte[] pa = { 0, 1, 18, 8, 18, 72, 14, 0, 0, 0, 0, 0 };
            byte[] da ={0,1,0x10,2,
                		0,1,
                		0,0,
                        0,		// area
                		0,0,0,		// start
                    	};
            byte[] da2 = { 0, 4, 0, 8, 0, 0, };
            //    uc da2[]={0,4,0,8,7,0,};

            if ((area == daveConst.daveAnaIn) || (area == daveConst.daveAnaOut) /*|| (area==daveP)*/)
            {
                da[3] = 4;
                start *= 8;			/* bits */
            }
            else if ((area == daveConst.daveTimer) || (area == daveConst.daveCounter) || (area == daveConst.daveTimer200) || (area == daveConst.daveCounter200))
            {
                da[3] = (byte)area;
            }
            else
            {
                start *= 8;
            }
            /*    else {
            if(isBit) {
            pa[3]=1;
            } else {
            start*=8;			
            }    
            }
            */
            da[8] = (byte)area;
            da[9] = (byte)(start / 0x10000);
            da[10] = (byte)((start / 0x100) & 0xff);
            da[11] = (byte)(start & 0xff);


            da2[4] = (byte)(val % 0x100);
            da2[5] = (byte)(val / 0x100);
            return /*res =*/ BuildAndSendPDU(p2, pa, da, da2);
            //return res;
        }