前段时间用西门子提供的Prodave5.5开发了基于YFIOServer的驱动程序,很可惜以太网方式它不支持,所以只好用西门子的softnet来开发相应的驱动了。
SoftNet的服务程序
目前采用的是同步方式的读写,可支持自恢复功能。
(采用异步方式初次读写可以成功,下次在读写时却失败,不是很明白!)
读写核心代码:
- int fix_field_read(int Addr,int intNum,BYTE *bDataValue,long lngMode,int blockno)
- {
- struct S7_READ_PARA read_para;
- int32 iRet;
- ord16 orderid=0;
- long lngTimeNum=0;
- long lngOK=1;
- read_para.access=S7_ACCESS_SYMB_ADDRESS;
- switch(lngMode)
- {
- case MODE_I:
- sprintf(read_para.var_name,"IB%d,%d",Addr,intNum);
- break;
- case MODE_Q:
- sprintf(read_para.var_name,"QB%d,%d",Addr,intNum);
- break;
- case MODE_M:
- sprintf(read_para.var_name,"MB%d,%d",Addr,intNum);
- break;
- case MODE_DB:
- sprintf(read_para.var_name,"DB%d,B%d,%d",blockno,Addr,intNum);
- break;
- }
- //IOF->ShowMessage(1,1,read_para.var_name,"--");
- iRet=s7_read_req(cp_descr,cp_cref,0,&read_para);
- if(iRet!=S7_OK)
- {
- //IOF->ShowMessage(1,1,read_para.var_name,"s7_read_req");
- return 1000;
- }
- do
- {
- iRet=s7_receive(cp_descr,&cp_cref,&orderid);
- switch(iRet)
- {
- case S7_READ_CNF:
- {
- //--------------------------------
- if(s7_get_read_cnf(( void *)0,(ord16 *)&intNum,(void *)bDataValue)!=S7_OK)
- {
- return 1001;
- }
- lngTimeNum=IOD->Overtime+1;
- //----------------------------------
- lngOK=0;
- }
- break;
- default:
- Sleep(1); //等待1ms
- break;
- }
- } while(lngTimeNum++<IOD->Overtime && iRet!=S7_ABORT_IND);
- return lngOK;
- }
- int fix_field_write(int Addr,int intNum,BYTE *bDataValue,long lngMode,int blockno)
- {
- struct S7_WRITE_PARA write_para;
- int32 iRet;
- ord16 orderid=0;
- long lngTimeNum=0;
- long lngOK=1,i;
- write_para.access=S7_ACCESS_SYMB_ADDRESS;
- switch(lngMode)
- {
- case MODE_I:
- return 999;
- break;
- case MODE_Q:
- sprintf(write_para.var_name,"QB%d,%d",Addr,intNum);
- break;
- case MODE_M:
- sprintf(write_para.var_name,"MB%d,%d",Addr,intNum);
- break;
- case MODE_DB:
- sprintf(write_para.var_name,"DB%d,B%d,%d",blockno,Addr,intNum);
- //IOF->ShowMessage(1,1,write_para.var_name,"fix_field_write");
- break;
- }
- write_para.var_length=intNum;
- for(i=0;i<intNum;i++)
- {
- write_para.value[i]=bDataValue[i];
- }
- iRet=s7_write_req(cp_descr,cp_cref,0,&write_para,(void *)0);
- if(iRet!=S7_OK)
- {
- //IOF->ShowMessage(1,1,"Error:s7_write_req","fix_field_write");
- return 1000;
- }
- do
- {
- iRet=s7_receive(cp_descr,&cp_cref,&orderid);
- switch(iRet)
- {
- case S7_WRITE_CNF:
- {
- //--------------------------------
- if(s7_get_write_cnf()!=S7_OK)
- {
- return 1001;
- }
- lngTimeNum=IOD->Overtime+1;
- //----------------------------------
- lngOK=0;
- }
- break;
- default:
- Sleep(1); //等待1ms
- break;
- }
- } while(lngTimeNum++<IOD->Overtime && iRet!=S7_ABORT_IND);
- return lngOK;
- }
注意:I和Q变量不知为什么,读写失败。
本文出自 “叶帆工作室” 博客,转载请与作者联系!