#pragma once /************************************************************************************ FPX PLC ͨѶͷÎļþ Version: V1.02 Date: 2015-06-30 Description; 2015-08-30 Ôö¼ÓÁËÄ£Ä⹦ÄÜ 1.03 2016-05-23 Ôö¼ÓÁÙ½çÇø ************************************************************************************/ #include "omp.h" #include "../SerialCom/SerialCom.hpp" const unsigned short HexMask[16]= { 0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80, 0x100,0x200,0x400,0x800,0x1000,0x2000,0x4000,0x8000 }; static inline int xtoi(const char * hexstr, int len=0) { int i,j,k; unsigned char ch; k=0;j=0; int len1=(int)strlen(hexstr); if (len>0) len1=len; for (i=0;i='0'&&ch<='9') { k=ch-'0'; j=j*16+k; continue; } if (ch>='A'&&ch<='F') { k=ch-'A'+10; j=j*16+k; continue; } if (ch>='a'&&ch<='f') { k=ch-'a'+10; j=j*16+k; continue; } if (ch==' '||ch==' ') { continue; } break; } return j; } #pragma pack(push) #pragma pack(2) struct bitfield16 { short int b0:1; short int b1:1; short int b2:1; short int b3:1; short int b4:1; short int b5:1; short int b6:1; short int b7:1; short int b8:1; short int b9:1; short int ba:1; short int bb:1; short int bc:1; short int bd:1; short int be:1; short int bf:1; }; #pragma pack() #pragma pack(pop) class FPXPLC { public: int Connected; CSerialCom SerialPlc1; int Station; char buffer1[4000]; char buffer2[4000]; int SimFunc; //Ä£Ä⹦ÄÜ int simresult1; //Ä£Ä⹦ÄÜÊýÖµ CStringA LastRecvString; double SendTimeMin; //·¢ËÍʱ¼ä×îС double SendTimeMax; //·¢ËÍʱ¼ä×î´ó double SendTimeCur; //·¢ËÍʱ¼äµ±Ç° double SendTimeAvg; //·¢ËÍʱ¼äƽ¾ù double RecvTimeMin; //½ÓÊÕʱ¼ä×îС double RecvTimeMax; //½ÓÊÕʱ¼ä×î´ó double RecvTimeCur; //½ÓÊÕʱ¼äµ±Ç° double RecvTimeAvg; //½ÓÊÕʱ¼äƽ¾ù double TransTimeMin; //´«Êäʱ¼ä£¬×îС double TransTimeMax; //´«Êäʱ¼ä×î´ó double TransTimeCur; //´«Êäʱ¼äµ±Ç° double TranstimeAvg; //´«Êäʱ¼äƽ¾ù #pragma pack(push) #pragma pack(2) union uPLCINFO { struct //stPLCINFO { char PlcType[2]; char Version[2]; char PrgSize[2]; char RunMode[2]; char LinkInfo[2]; char ErrMark[2]; char SelfTestInfo[4]; }; unsigned char bytes[16]; }InfoBytes; #pragma pack() #pragma pack(1) struct stPLCINFO { unsigned char PlcType; unsigned char Version; unsigned char PrgSize; union{ unsigned char RunMode; struct { unsigned char Running:1; unsigned char Test:1; unsigned char BRK:1; unsigned char BRKALW:1; unsigned char ReFresh:1; unsigned char SigleStep:1; unsigned char MSG:1; unsigned char REM:1; }stRunMode; }; unsigned char LinkInfo; union{ unsigned char Errs; struct{ unsigned char SelfTestErr:1; unsigned char PowerDown:1; unsigned char Fuse:1; unsigned char UnitErr:1; unsigned char IOChkErr:1; unsigned char BetteryErr:1; unsigned char BetteryErrKeep:1; unsigned char RunErr:1; }stErrs; }; unsigned short SelfTestInfo; }PlcInfo; #pragma pack() #pragma pack(pop) volatile bitfield16 * R; //λ·ÃÎÊ volatile short WX[110]; //WX0-WX109; volatile short WY[110]; //WY0-WY109; volatile short WL[128]; //WL0-WL127; volatile short WR[256]; //WR0-WR255; volatile short SWR[15]; //WR900-WR915; volatile short DT[32768]; //DT0-DT32767; volatile short SDT[512]; //DT90000-DT90499; volatile short LD[256]; //LD0-LD255; CCriticalSection myCriticalSection1; //ÁÙ½çÇø FPXPLC() { SimFunc=0; R=(bitfield16 *)WR; RecvTimeAvg=RecvTimeCur=RecvTimeMin=RecvTimeMax=0; SendTimeAvg=SendTimeCur=SendTimeMin=SendTimeMax=0; TranstimeAvg=TransTimeCur=TransTimeMin=TransTimeMax=0; }; ~FPXPLC(){}; int BCC(const char * value) { int i,j; char k; k=0; j=(int)strlen(value); for (i=0;iSendTimeMax) {SendTimeMax=dif;} if (dif0) { buffer[j]=0; LastRecvString=buffer; } if (RecvTimeCur==0) { RecvTimeCur=RecvTimeAvg=RecvTimeMax=RecvTimeMin=dif; }else { RecvTimeCur=dif; if (dif>RecvTimeMax) {RecvTimeMax=dif;} if (dif=9) { buffer2[j]=0; s1=buffer2; for (int i=0;i<16;i++) { InfoBytes.bytes[i]=buffer2[6+i]; } PlcInfo.PlcType=xtoi(InfoBytes.PlcType,2); PlcInfo.Version=xtoi(InfoBytes.Version,2); PlcInfo.PrgSize=xtoi(InfoBytes.PrgSize,2); PlcInfo.RunMode=xtoi(InfoBytes.RunMode,2); PlcInfo.LinkInfo=xtoi(InfoBytes.LinkInfo,2); PlcInfo.Errs=xtoi(InfoBytes.ErrMark,2); PlcInfo.SelfTestInfo=xtoi(InfoBytes.SelfTestInfo,4); return 1; } else { return 0; } } int RCS(const char * typeaddr,int &value, int Station2=0) //RCS Read single point of contact information ¶ÁÈ¡µ¥¸ö´¥µãµÄ״̬ÐÅÏ¢ X Y R L T C { if (SimFunc) { myCriticalSection1.Lock(INFINITE); CStringA stypeaddr; stypeaddr=typeaddr; CStringA type; CStringA addr,Laddr,Raddr; type=typeaddr[0]; addr="0000"+stypeaddr.Mid(1); addr=addr.Right(4); Laddr=addr.Left(3); Raddr=addr.Right(1); int Laddrv=atoi(Laddr); int Raddrv=xtoi(Raddr); if (type.MakeUpper()=="R") { value=(WR[Laddrv]&HexMask[Raddrv]); simresult1=1; }else {simresult1=0;} myCriticalSection1.Unlock(); return simresult1; } if (!Connected) {return 0;} CStringA stypeaddr; stypeaddr=typeaddr; CStringA type; CStringA addr; type=typeaddr[0]; addr="0000"+stypeaddr.Mid(1); addr=addr.Right(4); int len; CStringA s1; int station3; if (Station2==0) station3=Station; else station3=Station2; s1.Format("%%%02X#RCS%c%s",station3,type[0],addr); s1.AppendFormat("%02X\r",(unsigned char)(BCC(s1))); len=s1.GetLength(); myCriticalSection1.Lock(INFINITE); SendPLCData(s1,len); int j; j=ReadPLCCom(buffer2,9+2); myCriticalSection1.Unlock(); if (j>=9) { value=buffer2[6]-48; return 1; } else { return 0; } } int WCS(const char * typeaddr, int value) { if (SimFunc) { myCriticalSection1.Lock(INFINITE); CStringA stypeaddr; stypeaddr=typeaddr; CStringA type; CStringA addr,Laddr,Raddr; type=typeaddr[0]; addr="0000"+stypeaddr.Mid(1); addr=addr.Right(4); Laddr=addr.Left(3); Raddr=addr.Right(1); int Laddrv=atoi(Laddr); int Raddrv=xtoi(Raddr); if (type.MakeUpper()=="R") { if (value) { WR[Laddrv]|=HexMask[Raddrv]; } else { WR[Laddrv]&=~HexMask[Raddrv]; } simresult1=1; }else simresult1=0; myCriticalSection1.Unlock(); return simresult1; } if (!Connected) {return 0;} CStringA s1; CStringA type; CStringA addr; CStringA stypeaddr; stypeaddr=typeaddr; type=typeaddr[0]; addr="0000"+stypeaddr.Mid(1); addr=addr.Right(4); s1.Format("%%%02X#WCS%C%s%1X",Station,type[0],addr,value); s1.AppendFormat("%02X\r",(unsigned char)(BCC(s1))); int len; len=s1.GetLength(); myCriticalSection1.Lock(INFINITE); SendPLCData(s1,len); len=ReadPLCCom(buffer2,9+2); myCriticalSection1.Unlock(); CString s1T; if (len<9) { s1T.Format(_T("WCS faild\r\n"),type); return 0; } return 1; } int RCC(const char * typeaddr,int count,short int value[]) //count ×î´ó32 { if (SimFunc) { myCriticalSection1.Lock(INFINITE); CStringA stypeaddr,type,addr; stypeaddr=typeaddr; type=typeaddr[0]; addr=stypeaddr.Mid(1); int addrv=atoi(addr); if (type.MakeUpper()=="R") { for (int i=0;i32) {count=32;} s1.Format("%%%02X#RCC%c%04d%04d",Station,type[0],addr,addr+count-1); s1.AppendFormat("%02X\r",(unsigned char)(BCC(s1))); int len=s1.GetLength(); myCriticalSection1.Lock(INFINITE); SendPLCData(s1,len); len=ReadPLCCom(s1.GetBuffer(512),9+count*4+1); myCriticalSection1.Unlock(); s1.ReleaseBuffer(len); int i; if (len>9) { for (i=0;i0) { }else { s1T.Format(_T("WCC %c%d %d failed \r\n"),type[0],addr,count); } return 1; } int RD(const char * typeaddr,int count,short int value[]) { if (SimFunc) { myCriticalSection1.Lock(INFINITE); CStringA stypeaddr,type,addr; stypeaddr=typeaddr; type=typeaddr[0]; addr=stypeaddr.Mid(1); int addrv=atoi(addr); if (type.MakeUpper()=="D") { if (addrv+count<32768) { for (int i=0;i=90000) { for (int i=0;i32) {count=32;} s1.Format("%%%02X#RD%c%05d%05d",Station,type[0],addr,addr+count-1); s1.AppendFormat("%02X\r",(unsigned char)BCC(s1)); int len=s1.GetLength(); myCriticalSection1.Lock(INFINITE); SendPLCData(s1,len); len=ReadPLCCom(s1.GetBuffer(512),9+count*4+1); myCriticalSection1.Unlock(); s1.ReleaseBuffer(len); if (len>9) { int i; for (i=0;i=90000) { for (int i=0;i0) { return 1; } else{ s1T.Format(_T("WD %c%d %d failed \r\n"),type[0],addr,count); return 0; } return 1; } };