Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BCCWIN
- #include <stdio.h>
- #include <stdlib.h>
- #include "nodave.h"
- #include "openS7online.h"
- /*
- dont forget to set the correct path (to the library libnodave.lib for the linker
- and the header files for the compiler) in your IDE when using this code
- */
- int main(int argc, char ** argv)
- {
- char buf1 [davePartnerListSize];
- unsigned char * buffer;
- unsigned char * buffer2;
- int j,i, a, initSuccess,
- res;
- daveInterface * di;
- daveInterface * di2;
- daveConnection * dc;
- daveConnection * dc2;
- _daveOSserialType fds;
- //import the s7online.dll and set the handle
- fds.rfd=openS7online("S7Online",0);
- fds.wfd=fds.rfd;
- initSuccess=0;
- /*buffer for reading/writing to the PLC, increase on demand but keep in mind
- that the maximum size of a block in S7-Communication is limited by the size
- of a message structure calledPDU. Each call to daveReadBytes causes a the
- exchange of a request and a response PDU. The result data must fit into the
- "payload" area of a response PDU. This means a maximum block length is PDU
- size -18 bytes for read. A typical PDU size is 240 Byte, limiting read calls
- to 222 byte result length. If you need more data, you need to use multiple calls
- to daveReadBytes().
- */
- buffer=(unsigned char*)malloc(64);
- buffer2=(unsigned char*)malloc(64);
- if (fds.rfd>=0)
- { //initialize the Interface
- di =daveNewInterface(fds, "IF1", 0, daveProtoS7online, daveSpeed187k);
- daveSetTimeout(di,5000000);
- for (i=0; i<3; i++)
- {
- if (0==daveInitAdapter(di))
- {
- initSuccess=1;
- break;
- }
- else daveDisconnectAdapter(di);
- }
- if (!initSuccess)
- {
- printf("Couldn't connect to Adapter!.\n Please try again. You may also try the option -2 for some adapters.\n");
- return -3;
- }
- //for debug purposes, list all available devices on the MPI bus
- a= daveListReachablePartners(di,buf1);
- printf("daveListReachablePartners List length: %d\n",a);
- if (a>0) {
- for (j=0;j<a;j++) {
- if (buf1[j]==daveMPIReachable) printf("Active device at address:%d\n",j);
- if (buf1[j]==daveMPIPassive) printf("Passive device at address:%d\n",j);
- }
- }
- //if interface was successfully initialized, create a connection to it
- dc =daveNewConnection(di, 7, 0, 0);
- dc2 =daveNewConnection(di, 6, 0, 0);
- printf("\nConnectPLC\n");
- daveConnectPLC(dc);
- //res=daveReadBytes(dc, daveDB, 120, 8, 2,buffer);
- res=daveReadBytes(dc,daveInputs,0,0,2,buffer);
- printf("%x\n", res);
- a=daveGetU16from(buffer);
- //statuscode res==0 = read successful
- if (res==0)
- {
- printf("PLC (GetU16 value): %x\n", a);
- }
- else
- printf("error %d=%s\n", res, daveStrerror(res));
- daveFree(dc);
- printf("\nConnectPLC2\n");
- if (0==daveConnectPLC(dc2))
- {
- res=0;
- a=0;
- //res=daveReadBytes(dc, daveDB, 120, 8, 2,buffer);
- res=daveReadBytes(dc2,daveInputs,0,0,2,buffer2);
- printf("%x\n", res);
- a=daveGetU16from(buffer2);
- //statuscode res==0 = read successful
- if (res==0)
- {
- printf("PLC (GetU16 value): %x\n", a);
- }
- else
- printf("error %d=%s\n", res, daveStrerror(res));
- //a=0x0600;
- // daveWriteBytes(dc, daveFlags,120,150,2,&a);
- free(buffer);
- daveDisconnectAdapter(di);
- daveFree(di);
- return 0;
- }
- else
- {
- printf("Couldn't connect to PLC.\n Please try again. You may also try the option -2 for some adapters.\n");
- daveDisconnectAdapter(di);
- daveDisconnectAdapter(di);
- daveDisconnectAdapter(di);
- return -2;
- }
- }
- else
- {
- printf("Couldn't open access point %s\n",argv[1]);
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement