Advertisement
Guest User

libnodaveTest

a guest
Nov 7th, 2011
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. #define BCCWIN
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "nodave.h"
  5. #include "openS7online.h"
  6. /*
  7. dont forget to set the correct path (to the library libnodave.lib for the linker
  8. and the header files for the compiler) in your IDE when using this code
  9. */
  10. int main(int argc, char ** argv)
  11. {
  12.     char buf1 [davePartnerListSize];
  13.     unsigned char * buffer;
  14.     unsigned char * buffer2;
  15.     int j,i, a, initSuccess,
  16.     res;
  17.     daveInterface * di;
  18.     daveInterface * di2;
  19.     daveConnection * dc;
  20.     daveConnection * dc2;
  21.     _daveOSserialType fds;
  22.  
  23.     //import the s7online.dll and set the handle
  24.     fds.rfd=openS7online("S7Online",0);
  25.     fds.wfd=fds.rfd;
  26.     initSuccess=0;
  27.  
  28.     /*buffer for reading/writing to the PLC, increase on demand but keep in mind
  29.       that the maximum size of a block in S7-Communication is limited by the size
  30.       of a message structure calledPDU. Each call to daveReadBytes causes a the
  31.       exchange of a request and a response PDU. The result data must fit into the
  32.       "payload" area of a response PDU. This means a maximum block length is PDU
  33.       size -18 bytes for read. A typical PDU size is 240 Byte, limiting read calls
  34.       to 222 byte result length. If you need more data, you need to use multiple calls
  35.       to daveReadBytes().
  36.     */
  37.     buffer=(unsigned char*)malloc(64);
  38.     buffer2=(unsigned char*)malloc(64);
  39.  
  40.     if (fds.rfd>=0)
  41.     {   //initialize the Interface
  42.         di =daveNewInterface(fds, "IF1", 0, daveProtoS7online, daveSpeed187k);
  43.         daveSetTimeout(di,5000000);
  44.         for (i=0; i<3; i++)
  45.         {
  46.             if (0==daveInitAdapter(di))
  47.             {
  48.                 initSuccess=1;
  49.                 break;
  50.             }
  51.             else daveDisconnectAdapter(di);
  52.         }
  53.         if (!initSuccess)
  54.         {
  55.             printf("Couldn't connect to Adapter!.\n Please try again. You may also try the option -2 for some adapters.\n");
  56.             return -3;
  57.         }
  58.         //for debug purposes, list all available devices on the MPI bus
  59.         a= daveListReachablePartners(di,buf1);
  60.         printf("daveListReachablePartners List length: %d\n",a);
  61.  
  62.         if (a>0) {
  63.             for (j=0;j<a;j++) {
  64.             if (buf1[j]==daveMPIReachable) printf("Active device at address:%d\n",j);
  65.             if (buf1[j]==daveMPIPassive) printf("Passive device at address:%d\n",j);
  66.             }
  67.         }
  68.  
  69.  
  70.         //if interface was successfully initialized, create a connection to it
  71.         dc =daveNewConnection(di, 7, 0, 0);
  72.         dc2 =daveNewConnection(di, 6, 0, 0);
  73.  
  74.  
  75.        
  76.         printf("\nConnectPLC\n");
  77.         daveConnectPLC(dc);
  78.         //res=daveReadBytes(dc, daveDB, 120, 8, 2,buffer);
  79.             res=daveReadBytes(dc,daveInputs,0,0,2,buffer);
  80.             printf("%x\n", res);
  81.             a=daveGetU16from(buffer);
  82.  
  83.             //statuscode res==0 = read successful
  84.             if (res==0)
  85.             {
  86.  
  87.                 printf("PLC (GetU16 value): %x\n", a);
  88.             }
  89.             else
  90.                 printf("error %d=%s\n", res, daveStrerror(res));
  91.  
  92.            
  93.             daveFree(dc);
  94.        
  95.         printf("\nConnectPLC2\n");
  96.         if (0==daveConnectPLC(dc2))
  97.         {
  98.             res=0;
  99.             a=0;
  100.             //res=daveReadBytes(dc, daveDB, 120, 8, 2,buffer);
  101.             res=daveReadBytes(dc2,daveInputs,0,0,2,buffer2);
  102.             printf("%x\n", res);
  103.             a=daveGetU16from(buffer2);
  104.  
  105.             //statuscode res==0 = read successful
  106.             if (res==0)
  107.             {
  108.  
  109.                 printf("PLC (GetU16 value): %x\n", a);
  110.             }
  111.             else
  112.                 printf("error %d=%s\n", res, daveStrerror(res));
  113.  
  114.             //a=0x0600;
  115.            // daveWriteBytes(dc, daveFlags,120,150,2,&a);
  116.  
  117.             free(buffer);
  118.             daveDisconnectAdapter(di);
  119.             daveFree(di);
  120.             return 0;
  121.         }
  122.         else
  123.         {
  124.             printf("Couldn't connect to PLC.\n Please try again. You may also try the option -2 for some adapters.\n");
  125.             daveDisconnectAdapter(di);
  126.             daveDisconnectAdapter(di);
  127.             daveDisconnectAdapter(di);
  128.             return -2;
  129.         }
  130.     }
  131.     else
  132.     {
  133.         printf("Couldn't open access point %s\n",argv[1]);
  134.         return -1;
  135.     }
  136.  
  137. }
  138.  
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement