Esempio n. 1
0
    public static int Main(string[] args)
    {
        int i,a=0,j,res,b=0,c=0;
        float d=0;
        byte[] buf1=new byte[libno.davePartnerListSize];

        if (args.Length <1) {
        usage();
        return -1;
        }

        while (args[adrPos][0]=='-') {
        if (args[adrPos].StartsWith("--debug=")) {
        libno.daveSetDebug(Convert.ToInt32(args[adrPos].Substring(8)));
        Console.WriteLine("setting debug to: ",Convert.ToInt32(args[adrPos].Substring(8)));
        } else if (args[adrPos].StartsWith("-d")) {
        libno.daveSetDebug(libno.daveDebugAll);
        } else if (args[adrPos].StartsWith("-s")) {
        doStop=true;
        } else if (args[adrPos].StartsWith("-w")) {
        doWrite=true;
        } else if (args[adrPos].StartsWith("-b")) {
        doBenchmark=true;
        } else if (args[adrPos].StartsWith("--readoutall")) {
        doReadout=true;
        doSFBandSFC=true;
        } else if (args[adrPos].StartsWith("--readout")) {
        doReadout=true;
        } else if (args[adrPos].StartsWith("-r")) {
        doRun=true;
        } else if (args[adrPos].StartsWith("-e")) {
        doExperimental=true;
        } else if (args[adrPos].StartsWith("--local=")) {
        localMPI=Convert.ToInt32(args[adrPos].Substring(8));
        Console.WriteLine("setting local MPI address to: "+localMPI);
        } else if (args[adrPos].StartsWith("--mpi=")) {
        plcMPI=Convert.ToInt32(args[adrPos].Substring(6));
        Console.WriteLine("setting MPI address of PLC to: "+plcMPI);
        } else if (args[adrPos].StartsWith("--mpi2=")) {
        plc2MPI=Convert.ToInt32(args[adrPos].Substring(7));
        Console.WriteLine("setting MPI address of 2md PLC to: "+plc2MPI);
        } else if (args[adrPos].StartsWith("--wbit=")) {
        wbit=Convert.ToInt32(args[adrPos].Substring(7));
        Console.WriteLine("setting bit number: "+wbit);
        doWbit=true;
        } else if (args[adrPos].StartsWith("-z")) {
        doSZLread=true;
        } else if (args[adrPos].StartsWith("-a")) {
        doSZLreadAll=true;
        } else if (args[adrPos].StartsWith("-m")) {
        doMultiple=true;
        } else if (args[adrPos].StartsWith("-c")) {
        doClear=true;
        } else if (args[adrPos].StartsWith("-n")) {
        doNewfunctions=true;
        } else if (args[adrPos].StartsWith("-2")) {
        useProto=libno.daveProtoMPI2;
        } else if (args[adrPos].StartsWith("-3")) {
        useProto=libno.daveProtoMPI3;
        } else if (args[adrPos].StartsWith("-9")) {
         	    speed=libno.daveSpeed9k;
         	} else if (args[adrPos].StartsWith("-19")) {
         	    speed=libno.daveSpeed19k;
         	} else if (args[adrPos].StartsWith("-45")) {
         	    speed=libno.daveSpeed45k;
         	} else if (args[adrPos].StartsWith("-93")) {
         	    speed=libno.daveSpeed93k;
         	} else if (args[adrPos].StartsWith("-500")) {
         	    speed=libno.daveSpeed500k;
         	} else if (args[adrPos].StartsWith("-1500")) {
         	    speed=libno.daveSpeed1500k;
         	}

        adrPos++;
        if (args.Length<=adrPos) {
        usage();
        return -1;
        }
        }

        //        fds.rfd=libno.setPort(args[adrPos],"38400",'O');
        int ix=libno.openSocket(102,args[adrPos]);
        Console.WriteLine("openSocket returned(ix): "+ix);
        //	ix=540;
        fds.rfd=ix;
        Console.WriteLine("socket handle is: "+fds.rfd);
        fds.wfd=fds.rfd;
        Console.WriteLine("copy of socket handle is: "+fds.wfd);
        if (fds.rfd>0) {
        di =new libno.daveInterface(fds, "IF1", localMPI, useProto, speed);
            di.setTimeout(5000000);
            for (i=0; i<3; i++) {
        if (0==di.initAdapter()) {
            initSuccess=1;
        //		    a= libno.daveListReachablePartners(di,buf1);
            a= di.listReachablePartners(buf1);
            Console.WriteLine("daveListReachablePartners List length: "+a);
            if (a>0) {
            for (j=0;j<a;j++) {
                if (buf1[j]==libno.daveMPIReachable)
                Console.WriteLine("Device at address: "+j);
            }
            }
            break;
        } else di.disconnectAdapter();
        }
        if (initSuccess==0) {
            Console.WriteLine("Couldn't connect to Adapter!.\n Please try again. You may also try the option -2 for some adapters.");
            return -3;
        }

        dc = new libno.daveConnection(di, plcMPI, rack, slot);
  //plcMPI doesn't matter in case of ISO_TCP

        if (0==dc.connectPLC()) {;
        res=dc.readBytes(libno.daveFlags, 0, 0, 16, null);
        if (res==0) {
                a=dc.getS32();
                b=dc.getS32();
                c=dc.getS32();
            d=dc.getFloat();
            Console.WriteLine("FD0: " + a);
            Console.WriteLine("FD4: " + b);
            Console.WriteLine("FD8: " + c);
            Console.WriteLine("FD12: " + d);
        } else
            Console.WriteLine("error "+res+" "+libno.daveStrerror(res));

        if(doExperimental) {
            Console.WriteLine("Trying to read outputs");
            res=dc.readBytes(libno.daveOutputs, 0, 0, 2, null);
            Console.WriteLine("function result: "+res+"="+libno.daveStrerror(res)+" "+dc.getAnswLen());
            if (res==0) {
                Console.Write("Bytes:");
            for (b=0; b<dc.getAnswLen(); b++) {
                c=dc.getU8();
                Console.Write(String.Format(" {0:X0}, ",c));
            }
            Console.WriteLine("");
            }
            a=0x01;
            Console.WriteLine("Trying to write outputs");
            res=dc.writeBytes(libno.daveOutputs, 0, 0, 1, BitConverter.GetBytes(a));
            Console.WriteLine("function result: "+res+"="+libno.daveStrerror(res)+" "+dc.getAnswLen());
            libno.daveSetDebug(libno.daveDebugAll);
            res=dc.force200(0,0);
            Console.WriteLine("function result: "+res+"="+libno.daveStrerror(res)+" "+dc.getAnswLen());
            libno.daveSetDebug(0);
            res=dc.force200(0,1);
            Console.WriteLine("function result of force: "+res+"="+libno.daveStrerror(res)+" "+dc.getAnswLen());
            wait();

            res=dc.force200(0,2);
            Console.WriteLine("function result of force: "+res+"="+libno.daveStrerror(res)+" "+dc.getAnswLen());
            wait();
            res=dc.force200(0,3);
            Console.WriteLine("function result of force: "+res+"="+libno.daveStrerror(res)+" "+dc.getAnswLen());
            wait();
            res=dc.force200(1,4);
            Console.WriteLine("function result of force: "+res+"="+libno.daveStrerror(res)+" "+dc.getAnswLen());
            wait();
            res=dc.force200(2,5);
            Console.WriteLine("function result of force: "+res+"="+libno.daveStrerror(res)+" "+dc.getAnswLen());
            wait();
            res=dc.force200(3,7);
            Console.WriteLine("function result of force: "+res+"="+libno.daveStrerror(res)+" "+dc.getAnswLen());
            wait();
            Console.WriteLine("Trying to read outputs again\n");
            res=dc.readBytes(libno.daveOutputs, 0, 0, 4, null);
            Console.WriteLine("function result: "+res+"="+libno.daveStrerror(res)+" "+dc.getAnswLen());
            if (res==0) {
                Console.Write("Bytes:");
            for (b=0; b<dc.getAnswLen(); b++) {
                c=dc.getU8();
                Console.Write(String.Format(" {0:X0}, ",c));
            }
            Console.WriteLine("");
            }
        }

        if(doWrite) {
                Console.WriteLine("Now we write back these data after incrementing the integers by 1,2 and 3 and the float by 1.1.\n");
                wait();
        /*
        Attention! you need to daveSwapIed little endian variables before using them as a buffer for
        daveWriteBytes() or before copying them into a buffer for daveWriteBytes()!
        */
                a=libno.daveSwapIed_32(a+1);
            dc.writeBytes(libno.daveFlags,0,0,4,BitConverter.GetBytes(a));
                    b=libno.daveSwapIed_32(b+2);
            dc.writeBytes(libno.daveFlags,0,4,4,BitConverter.GetBytes(b));
                c=libno.daveSwapIed_32(c+3);
            dc.writeBytes(libno.daveFlags,0,8,4,BitConverter.GetBytes(c));
                    d=libno.toPLCfloat(d+1.1f);
                dc.writeBytes(libno.daveFlags,0,12,4,BitConverter.GetBytes(d));
        /*
         *   Read back and show the new values, so users may notice the difference:
         */
                dc.readBytes(libno.daveFlags,0,0,16, null);
            a=dc.getU32();
                    b=dc.getU32();
            c=dc.getU32();
                d=dc.getFloat();
            Console.WriteLine("FD0: "+a);
            Console.WriteLine("FD4: "+b);
            Console.WriteLine("FD8: "+c);
            Console.WriteLine("FD12: "+d);
        } // doWrite

        if(doClear) {
                Console.WriteLine("Now writing 0 to the bytes FB0...FB15.\n");
        //    		    wait();
            byte[] aa={0,0,0,0};
                dc.writeBytes(libno.daveFlags,0,0,4,aa);
                dc.writeBytes(libno.daveFlags,0,4,4,aa);
            dc.writeBytes(libno.daveFlags,0,8,4,aa);
                dc.writeBytes(libno.daveFlags,0,12,4,aa);
            dc.readBytes(libno.daveFlags,0,0,16, null);
                a=dc.getU32();
                b=dc.getU32();
                c=dc.getU32();
                d=dc.getFloat();
            Console.WriteLine("FD0: "+a);
            Console.WriteLine("FD4: "+b);
            Console.WriteLine("FD8: "+c);
            Console.WriteLine("FD12: "+d);
        } // doClear

        if(doSZLread) {
            readSZL(dc,0x92,0x0);
                readSZL(dc,0xB4,0x1024);
            readSZL(dc,0x111,0x1);
            readSZL(dc,0xD91,0x0);
            readSZL(dc,0x232,0x4);
            readSZL(dc,0x1A0,0x0);
            readSZL(dc,0x0A0,0x0);
        }

        if(doSZLreadAll) {
            readSZLAll(dc);
        }

        if(doStop) {
            dc.stop();
        }
        if(doRun) {
            dc.start();
        }

        if(doNewfunctions) {
            int saveDebug=libno.daveGetDebug();

            Console.WriteLine("Trying to read two consecutive bits from DB11.DBX0.1");;
            res=dc.readBits(libno.daveDB, 11, 1, 2, null);
            Console.WriteLine("function result:" + res+ "="+libno.daveStrerror(res));

            Console.WriteLine("Trying to read no bit (length 0) from DB17.DBX0.1");
            res=dc.readBits(libno.daveDB, 17, 1, 0, null);
            Console.WriteLine("function result:" + res+ "="+libno.daveStrerror(res));

            libno.daveSetDebug(libno.daveGetDebug()|libno.daveDebugPDU);
            Console.WriteLine("Trying to read a single bit from DB17.DBX0.3\n");
            res=dc.readBits(libno.daveDB, 17, 3, 1, null);
            Console.WriteLine("function result:" + res+ "="+libno.daveStrerror(res));

            Console.WriteLine("Trying to read a single bit from E0.2\n");
            res=dc.readBits(libno.daveInputs, 0, 2, 1, null);
            Console.WriteLine("function result:" + res+ "="+libno.daveStrerror(res));

            a=0;
            Console.WriteLine("Writing 0 to EB0\n");
            res=dc.writeBytes(libno.daveOutputs, 0, 0, 1, BitConverter.GetBytes(libno.daveSwapIed_32(a)));
            Console.WriteLine("function result:" + res+ "="+libno.daveStrerror(res));

            a=1;
            Console.WriteLine("Trying to set single bit E0.5\n");
            res=dc.writeBits(libno.daveOutputs, 0, 5, 1, BitConverter.GetBytes(libno.daveSwapIed_32(a)));
            Console.WriteLine("function result:" + res+ "="+libno.daveStrerror(res));

            Console.WriteLine("Trying to read 1 byte from AAW0\n");
            res=dc.readBytes(libno.daveAnaIn, 0, 0, 2, null);
            Console.WriteLine("function result:" + res+ "="+libno.daveStrerror(res));

            a=2341;
            Console.WriteLine("Trying to write 1 word (2 bytes) to AAW0\n");
            res=dc.writeBytes(libno.daveAnaOut, 0, 0, 2, BitConverter.GetBytes(libno.daveSwapIed_32(a)));
            Console.WriteLine("function result:" + res+ "="+libno.daveStrerror(res));

            Console.WriteLine("Trying to read 4 items from Timers\n");
            res=dc.readBytes(libno.daveTimer, 0, 0, 4, null);
            Console.WriteLine("function result:" + res+ "="+libno.daveStrerror(res));
            if(res==0) {
            d=dc.getSeconds();
            Console.WriteLine("Time: %0.3f, ",d);
                d=dc.getSeconds();
            Console.WriteLine("%0.3f, ",d);
            d=dc.getSeconds();
            Console.WriteLine("%0.3f, ",d);
            d=dc.getSeconds();
            Console.WriteLine(" %0.3f\n",d);

                d=dc.getSecondsAt(0);
                Console.WriteLine("Time: %0.3f, ",d);
                d=dc.getSecondsAt(2);
            Console.WriteLine("%0.3f, ",d);
                d=dc.getSecondsAt(4);
                Console.WriteLine("%0.3f, ",d);
                d=dc.getSecondsAt(6);
                Console.WriteLine(" %0.3f\n",d);
            }

            Console.WriteLine("Trying to read 4 items from Counters\n");
            res=dc.readBytes(libno.daveCounter, 0, 0, 4, null);
            Console.WriteLine("function result:" + res+ "="+libno.daveStrerror(res));
            if(res==0) {
                c=dc.getCounterValue();
                Console.WriteLine("Count: %d, ",c);
            c=dc.getCounterValue();
                Console.WriteLine("%d, ",c);
            c=dc.getCounterValue();
            Console.WriteLine("%d, ",c);
            c=dc.getCounterValue();
                Console.WriteLine(" %d\n",c);

            c=dc.getCounterValueAt(0);
                Console.WriteLine("Count: %d, ",c);
                c=dc.getCounterValueAt(2);
                Console.WriteLine("%d, ",c);
                c=dc.getCounterValueAt(4);
                Console.WriteLine("%d, ",c);
                c=dc.getCounterValueAt(6);
                Console.WriteLine(" %d\n",c);
            }

            libno.PDU p=dc.prepareReadRequest();
            p.addVarToReadRequest(libno.daveInputs,0,0,1);
            p.addVarToReadRequest(libno.daveFlags,0,0,4);
            p.addVarToReadRequest(libno.daveDB,6,20,2);
            p.addVarToReadRequest(libno.daveTimer,0,0,4);
            p.addVarToReadRequest(libno.daveTimer,0,1,4);
            p.addVarToReadRequest(libno.daveTimer,0,2,4);
            p.addVarToReadRequest(libno.daveCounter,0,0,4);
            p.addVarToReadRequest(libno.daveCounter,0,1,4);
            p.addVarToReadRequest(libno.daveCounter,0,2,4);
            libno.resultSet rs=new libno.resultSet();
                res=dc.execReadRequest(p, rs);
            libno.daveSetDebug(saveDebug);
        }

        //		System.GarbageCollect();

        if(doMultiple) {
                Console.WriteLine("Now testing read multiple variables.\n"
                    +"This will read 1 Byte from inputs,\n"
                    +"4 bytes from flags, 2 bytes from DB6,\n"
                    +"and other 2 bytes from flags");
                wait();
            libno.PDU p=dc.prepareReadRequest();
            p.addVarToReadRequest(libno.daveInputs,0,0,1);
            p.addVarToReadRequest(libno.daveFlags,0,0,4);
            p.addVarToReadRequest(libno.daveDB,6,20,2);
            p.addVarToReadRequest(libno.daveFlags,0,12,2);
            p.addBitVarToReadRequest(libno.daveFlags, 0, 25 /* 25 is 3.1*/, 1);
            libno.resultSet rs=new libno.resultSet();
            res=dc.execReadRequest(p, rs);

            Console.Write("Input Byte 0: ");
            res=dc.useResult(rs, 0); // first result
            if (res==0) {
            a=dc.getU8();
                Console.WriteLine(a);
            } else
            Console.WriteLine("*** Error: "+libno.daveStrerror(res));

            Console.Write("Flag DWord 0: ");
            res=dc.useResult(rs, 1); // 2nd result
            if (res==0) {
            a=dc.getS16();
                Console.WriteLine(a);
            } else
            Console.WriteLine("*** Error: "+libno.daveStrerror(res));

            Console.Write("DB 6 Word 20: ");
            res=dc.useResult(rs, 2); // 3rd result
            if (res==0) {
            a=dc.getS16();
                Console.WriteLine(a);
            } else
            Console.WriteLine("*** Error: "+libno.daveStrerror(res));

            Console.Write("Flag Word 12: ");
            res=dc.useResult(rs, 3); // 4th result
            if (res==0) {
            a=dc.getU16();
                Console.WriteLine(a);
                } else
                Console.WriteLine("*** Error: "+libno.daveStrerror(res));

            Console.Write("Flag F3.1: ");
            res=dc.useResult(rs, 4); // 4th result
            if (res==0) {
            a=dc.getU8();
                Console.WriteLine(a);
            } else
            Console.WriteLine("*** Error: "+libno.daveStrerror(res));

            Console.Write("non existing result (we read 5 items, but try to use a 6th one): ");
            res=dc.useResult(rs, 5); // 5th result
            if (res==0) {
            a=dc.getU16();
                Console.WriteLine(a);
            } else
            Console.WriteLine("*** Error: "+libno.daveStrerror(res));

            if (doWrite){
            Console.WriteLine("Now testing write multiple variables:\n"
                +"IB0, FW0, QB0, DB6:DBW20 and DB20:DBD24 in a single multiple write.");
            wait();
        //			libno.daveSetDebug(0xffff);
            byte[] aa={0};
                libno.PDU p2=dc.prepareWriteRequest();
            p2.addVarToWriteRequest(libno.daveInputs,0,0,1, aa);
            p2.addVarToWriteRequest(libno.daveFlags,0,4,2, aa);
                p2.addVarToWriteRequest(libno.daveOutputs,0,0,2, aa);
            p2.addVarToWriteRequest(libno.daveDB,6,20,2, aa);
            p2.addVarToWriteRequest(libno.daveDB,20,24,4, aa);
            aa[0]=1;
            p2.addBitVarToWriteRequest(libno.daveFlags, 0, 27 /* 27 is 3.3*/, 1, aa);
            rs =new libno.resultSet();
            res=dc.execWriteRequest(p2, rs);
            Console.WriteLine("Result code for the entire multiple write operation: "+res+"="+libno.daveStrerror(res));
        /*
        //	I could list the single result codes like this, but I want to tell
        //	which item should have been written, so I do it in 5 individual lines:

            for (i=0;i<rs.numResults;i++){
                res=rs.results[i].error;
                Console.WriteLine("result code from writing item %d: %d=%s\n",i,res,libno.libno.daveStrerror(res));
            }
        */
            int err=rs.getErrorOfResult(0);
            Console.WriteLine("Result code for writing IB0:       "+err+"="+libno.daveStrerror(err));
            err=rs.getErrorOfResult(1);
            Console.WriteLine("Result code for writing FW4:       "+err+"="+libno.daveStrerror(err));
            err=rs.getErrorOfResult(2);
            Console.WriteLine("Result code for writing QB0:       "+err+"="+libno.daveStrerror(err));
            err=rs.getErrorOfResult(3);
            Console.WriteLine("Result code for writing DB6:DBW20: "+err+"="+libno.daveStrerror(err));
            err=rs.getErrorOfResult(4);
            Console.WriteLine("Result code for writing DB20:DBD24:"+err+"="+libno.daveStrerror(err));
            err=rs.getErrorOfResult(5);
            Console.WriteLine("Result code for writing F3.3:      "+err+"="+libno.daveStrerror(err));
        /*
         *   Read back and show the new values, so users may notice the difference:
         */
                dc.readBytes(libno.daveFlags,0,0,16, null);
                a=dc.getU32();
                b=dc.getU32();
                c=dc.getU32();
                d=dc.getFloat();
                Console.WriteLine("FD0: %d\n",a);
            Console.WriteLine("FD4: %d\n",b);
            Console.WriteLine("FD8: %d\n",c);
                Console.WriteLine("FD12: %f\n",d);
            } // doWrite
        }

        dc.disconnectPLC();
        }
        di.disconnectAdapter();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Console.WriteLine("Here we are");
        } else {
        Console.WriteLine("Couldn't open serial port "+args[adrPos]);
        return -1;
        }
        return 0;