通过MODBUSTCP读写PLC源码功能模块一:读写PLC主模块usingSystem;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Windows.Forms;///summary///通过modbusTCP读写PLC数据,//////设计:张东启,2011.4.9///2011.5.13更新,加入Application.DoEvents();以防连续读写时引起前台反应迟顿,同时让PLC有处理等待时间/////////////summarypublicclassPLCFunction{publicstaticboolConnected=false;publicstaticinttrytimes=0;//内部使用变量privatestaticbyte[]sendBuf={0,0,0,0,0,06,255,0,0,0,0,0,0,0,0,0,0,0,0,0};//共20个字节privatestaticbyte[]recBuf={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//共20个字节privatestaticinthi,low,bytes,hi1,low1;privatestaticSocketzdqSocket;privatestaticstringtiaoshi=N;privatestaticstringplcAddress=127.0.0.1;publicstaticintReadWord(intmwAddress){intrtValue=-99;if(init_plc()==0){try{hi=mwAddress/256;low=mwAddress-hi*256;sendBuf[7]=3;sendBuf[8]=(byte)hi;sendBuf[9]=(byte)low;sendBuf[10]=0;sendBuf[11]=1;//发送查询zdqSocket.Send(sendBuf,12,0);Application.DoEvents();bytes=zdqSocket.Receive(recBuf,11,0);//返回11个字节if(bytes==11){hi=recBuf[9];low=recBuf[10];rtValue=hi*256+low;}trytimes=0;}catch(Exceptionte){if(tiaoshi==Y)MessageBox.Show(te.ToString());trytimes++;}}returnrtValue;}publicstaticvoidWriteWord(intmwAddress,intmwValue){if(init_plc()==0){try{hi=mwAddress/256;low=mwAddress-hi*256;hi1=mwValue/256;low1=mwValue-hi*256;sendBuf[7]=6;sendBuf[8]=(byte)hi;sendBuf[9]=(byte)low;sendBuf[10]=(byte)hi1;sendBuf[11]=(byte)low1;zdqSocket.Send(sendBuf,12,0);Application.DoEvents();bytes=zdqSocket.Receive(recBuf,recBuf.Length,0);//写一个字返回几个?trytimes=0;}catch(Exceptionte){if(tiaoshi==Y)MessageBox.Show(te.ToString());trytimes++;}}}publicstaticintGetMemory(intmwAddress){intrtValue=-99;if(init_plc()==0){try{hi=mwAddress/256;low=mwAddress-hi*256;sendBuf[7]=2;sendBuf[8]=(byte)hi;sendBuf[9]=(byte)low;sendBuf[10]=0;sendBuf[11]=1;//发送查询zdqSocket.Send(sendBuf,12,0);Application.DoEvents();bytes=zdqSocket.Receive(recBuf,10,0);//返回11个字节if(bytes==10){hi=recBuf[9];rtValue=hi;}trytimes=0;}catch(Exceptionte){if(tiaoshi==Y)MessageBox.Show(te.ToString());trytimes++;}}returnrtValue;}publicstaticvoidSetMemory(intmwAddress,intmFlag){if(init_plc()==0){//线圈置位与复位intmBit=0;if(mFlag==1)mBit=255;elsemBit=0;try{hi=mwAddress/256;low=mwAddress-hi*256;sendBuf[7]=5;sendBuf[8]=(byte)hi;sendBuf[9]=(byte)low;sendBuf[10]=(byte)mBit;sendBuf[11]=0;zdqSocket.Send(sendBuf,12,0);Application.DoEvents();bytes=zdqSocket.Receive(recBuf,recBuf.Length,0);trytimes=0;}catch(Exceptionte){if(tiaoshi==Y)MessageBox.Show(te.ToString());trytimes++;}}}privatestaticintinit_plc(){//初始化intzdqrt=0;if(trytimes3){//超过3次自动恢复连接if(zdqSocket.Connected==true)zdqSocket.Disconnect(false);zdqSocket=null;trytimes=0;}try{if(zdqSocket==null){zdqSocket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);zdqSocket.ReceiveTimeout=int.Parse(zdqpro.WriteOrReadINI(系统设置,接收超时));plcAddress=zdqpro.WriteOrReadINI(系统设置,PLC地址);}if(zdqSocket.Connected==false){//重新连接tiaoshi=zdqpro.WriteOrReadINI(系统设置,PLC调试);zdqSocket.Connect(plcAddress,502);}}catch(Exceptionte){if(tiaoshi==Y)MessageBox.Show(te.ToString());trytimes++;zdqrt=-1;}Connected=zdqSocket.Connected;//是否已经连接Application.DoEvents();returnzdqrt;}}读写配置文件功能代码publicstaticstringWriteOrReadINI(stringrootNod,stringvalueNod){//读取配置文件XmlDocumentmydoc=newXmlDocument();mydoc.Load(zdqsys.ini);stringstr1=rootNod+/+valueNod;stringrt=mydoc.SelectSingleNode(str1).InnerText;returnrt;}publicstaticvoidWriteOrReadINI(stringrootNod,stringvalueNod,stringvalueStr){//写入配置文件XmlDocumentmydoc=newXmlDocument();mydoc.Load(zdqsys.ini);stringstr1=rootNod+/+valueNod;XmlNodemynod=mydoc.SelectSingleNode(str1);mynod.InnerText=valueStr;mydoc.Save(zdqsys.ini);}配置文件zdqsys.ini内容如下:?xmlversion=1.0encoding=GB2312?系统设置PLC地址192.168.1.200/PLC地址接收超时10/接收超时PLC调试N/PLC调试/系统设置希望能与工控爱好者或相关开发人员相互交流切搓,不当之处敬请指教。张东启2011.5.15