まず、私はC++の経験がほとんどない大学生です。その権利を何回聞いたことがありますか?libnodaveライブラリのテストプログラムtestISO_TCP(簡易バージョン)を使用しています。このプログラムは、seimens 300 PLCに接続されている間、フラグ値とデータブロックの単純な読み取りを行います。プログラム自体はエラーを発生させません。私がやろうとしているのは、読み取りがクラッシュしないように保護するコードをこのプログラムに追加することです。もう少し詳しく説明させてください。たとえば、コードに多くの読み取りが実装されているとします。現在のところ、読み取りは2つだけです。最終的には、このコードをさらに多くの読み取りで実行します。ここで、テストプログラムを実行していて、何らかの理由でPLCへの接続が失われたとします。プログラムに次の2つのいずれかを実行させたいと思います。1)接続が失われたら、一定回数接続を再試行し、試行回数がなくなったら終了します。または2)すべてが完了するまで、どういうわけかPLCから読み取りを続けます。
これが助けを得るのに十分な情報であることを願っています。これを効果的に行う方法がわからないまま、私が長い間見てきたコードを投稿します。よろしくお願いします。
#define PLAY_WITH_KEEPALIVE
#include <stdlib.h>
#include <stdio.h>
#include "nodavesimple.h"
#include "openSocket.h"
#ifdef PLAY_WITH_KEEPALIVE
#include <winsock.h>
#endif
int main(int argc, char **argv) {
int a,b,c,res, doRun, doStop, doRead, doreadFlag, useProtocol, useSlot;
#ifdef PLAY_WITH_KEEPALIVE
int opt;
#endif
float d;
daveInterface * di;
daveConnection * dc;
_daveOSserialType fds;
doRun=0;
doStop=0;
doRead=0;
doreadFlag=0;
useProtocol=daveProtoISOTCP;
useSlot=2;
fds.rfd=openSocket(102, argv[1]);
#ifdef PLAY_WITH_KEEPALIVE
errno=0;
opt=1;
//res=setsockopt(fds.rfd, SOL_SOCKET, SO_KEEPALIVE, &opt, 4);
//LOG3("setsockopt %s %d\n", strerror(errno),res);
#endif
fds.wfd=fds.rfd;
if (fds.rfd>0)
{
di =daveNewInterface(fds,"IF1",0, daveProtoISOTCP, daveSpeed187k);
daveSetTimeout(di,5000000);
dc =daveNewConnection(di,2,0, 2); // insert your rack and slot here
if (0==daveConnectPLC(dc))
{
printf("Connected.\n");
res=daveReadBytes(dc,daveFlags,0,0,16,NULL);
if (0==res)
{
a=daveGetU32(dc);
b=daveGetU32(dc);
c=daveGetU32(dc);
d=daveGetFloat(dc);
printf("FD0: %d\n",a);
printf("FD4: %d\n",b);
printf("FD8: %d\n",c);
printf("FD12: %f\n",d);
}//end 0==res
}//end daveConnectPLC
else
{
printf("Couldn't connect to PLC.\n Please make sure you use the -2 option with a CP243 but not with CPs 343 or 443.\n");
//closeSocket(fds.rfd);
//return -2;
}
}//end fds.rfd
fds.rfd=openSocket(102, argv[1]);
fds.wfd=fds.rfd;
if (fds.rfd>0)
{
di =daveNewInterface(fds,"IF1",0, daveProtoISOTCP, daveSpeed187k);
daveSetTimeout(di,5000000);
dc =daveNewConnection(di,2,0, 2); // insert your rack and slot here
if (0==daveConnectPLC(dc))
{
printf("Connected.\n");
res=daveReadBytes(dc,daveDB,1,0,64,NULL);
if (0==res)
{
a=daveGetU16(dc);
printf("DB1:DW0: %d\n",a);
a=daveGetU16(dc);
printf("DB1:DW1: %d\n...\n",a);
a=daveGetU16At(dc,62);
printf("DB1:DW32: %d\n",a);
}//end 0==res
return 0;
}//end daveConnectPLC
else
{
printf("Couldn't connect to PLC.\n Please make sure you use the -2 option with a CP243 but not with CPs 343 or 443.\n");
closeSocket(fds.rfd);
return -2;
}
}//end fds.rfd
else
{
printf("Couldn't open TCP port. \nPlease make sure a CP is connected and the IP address is ok. \n");
return -1;
}
}// end main