MainPro.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624
  1. #include "stdafx.h"
  2. #include "ascprocess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include "MainPro.h"
  6. #include "rtuprocess.h"
  7. #include "tcpprocess.h"
  8. #include "icpdasprocess.h"
  9. #include "SnmpProcess.h"
  10. #include "upsParadigmProcess.h"
  11. #include "stulzprocess.h"
  12. #include "NoticeQueue.h"
  13. #include "DaiKinProcess.h"
  14. #include "PowerwareProcess.h"
  15. #include "greeprocess.h"
  16. #include "CarelProcess.h"
  17. #include "Client2SrvType.h"
  18. #include "SysLib.h"
  19. #include "Separate.h"
  20. #include "MostDevice.h"
  21. #include "Const.h"
  22. #include "ping.h"
  23. CRITICAL_SECTION g_csDevOnlineReadOneData;
  24. #define DATA_CHANGE_FLAG 0.01//0.00001
  25. #if IS_USER_MUL_THREAD_REQUEST
  26. HANDLE g_hRequestDataThread[MAX_DLL_TYPE] = {0};
  27. CString g_strPort[MAX_DLL_TYPE];
  28. #else
  29. HANDLE g_hRequestDataThread = NULL;
  30. #endif
  31. HANDLE g_hReConnectCommThread = NULL;
  32. INT g_nRequestDataThreadCount = 0;
  33. INT g_nTimingSendFinished = 0;
  34. HANDLE g_hTimingSendSmsThread = NULL;
  35. HANDLE g_hDevOnlineThread = NULL;
  36. HANDLE g_hVoiceNoticeThread = NULL;
  37. HANDLE g_hOutputThread = NULL;
  38. HANDLE g_hGetDatePlanThread = NULL;
  39. INT g_nRtuThreadIndex = RTU_THREAD_START; //预留100个Modbus Rtu设备,每个串口一个处理线程
  40. INT g_nAscThreadIndex = ASC_THREAD_START; //预留100个Modbus Asc设备,每个串口一个处理线程
  41. INT g_nTcpThreadIndex = TCP_THREAD_START; //预留100个Modbus Tcp设备,每个IP一个处理线程
  42. INT g_nSnmpThreadIndex = SNMP_THREAD_START; //预留200个Snmp设备,每个IP一个处理线程
  43. INT g_nMostDeviceIndex = MOSTDEVICE_THREAD_START; //预留100个MostDevice类型有设备,每个IP一个处理线程
  44. INT g_nIcpdasIndex = ICPDAS_THREAD_START; //预留10个泓格设备,每个串口一个处理线程
  45. INT g_nParadigmIndex = PARADIGM_THREAD_START; //预留10个Paradigm设备,每个串口一个处理线程
  46. INT g_nStulzIndex = STULZ_THREAD_START; //预留10个Stulz设备,每个串口一个处理线程
  47. INT g_nDaiKinIndex = DAIKIN_THREAD_START; //预留10个Dinkin设备,每个串口一个处理线程
  48. INT g_nPowerwareIndex = POWERWARE_THREAD_START; //预留10个Powerware设备,每个串口一个处理线程
  49. INT g_nGreeIndex = GREE_THREAD_START; //预留10个Gree设备,每个串口一个处理线程
  50. INT g_nCarelIndex = CAREL_THREAD_START; //预留10个Carel设备,每个串口一个处理线程
  51. INT g_nScantakIndex = SCANTAK_THREAD_START; //预留10个Scantak设备,每个串口一个处理线程
  52. INT g_nDeltaIndex = DELTA_THREAD_START; //预留10个Delta设备,每个串口一个处理线程
  53. // 该结构体用于网络设备;
  54. typedef struct __TCP_THREAD_STRUCT
  55. {
  56. int nTcpTheadIndex; // 每一个网络设备对应的一个线程ID索引;(每一个ip就一个线程)
  57. CString strUid; // 与索引相联的网络设备UID;
  58. }TcpThreadStruct;
  59. INT g_nTcpThreadStructIndex = 0; // 供g_tcpThreadStruct数组使用,下标号;
  60. TcpThreadStruct g_tcpThreadStruct[MAX_TCP_THREAD];
  61. DWORD WINAPI RequestDataThreadProc(void *pPara);
  62. DWORD WINAPI ReConnectCommThreadProc(void *Para);
  63. DWORD WINAPI TimingSendSmsThread(void *pPara);
  64. DWORD WINAPI DevOnlineThreadProc(void *pPara);
  65. DWORD WINAPI VoiceNoticeThreadProc(void *pPara);
  66. DWORD WINAPI OutputThreadProc(void *pPara);
  67. DWORD WINAPI GetDatePlanThreadProc(void *pPara);
  68. // 获取实际波特率
  69. INT GetCommRate(int nRate);
  70. // 获取实际数据位
  71. INT GetCommDataBit(int nDataBitIndex);
  72. // 以下查找串口是否使用的13个函数,由函数主函数StartMain调用;
  73. BOOL FindRtuCommPort(CString strPort);
  74. BOOL FindAscCommPort(CString strPort);
  75. BOOL FindTcpCommPort(CString strPort);
  76. BOOL FindMostDeviceCommPort(CString strPort);
  77. BOOL FindIcpdasCommPort(CString strPort);
  78. BOOL FindParadigmCommPort(CString strPort);
  79. BOOL FindStulzCommPort(CString strPort);
  80. BOOL FindDaiKinCommPort(CString strPort);
  81. BOOL FindPowerwareCommPort(CString strPort);
  82. BOOL FindGreeCommPort(CString strPort);
  83. BOOL FindCarelCommPort(CString strPort);
  84. BOOL FindScanTakCommPort(CString strPort);
  85. BOOL FindDeltaCommPort(CString strPort);
  86. INT StartMain()
  87. {
  88. //INT iCount = GetDevProperty();
  89. // 初始化该结构体数组 , 每个网络设备的信息;
  90. for( int i = 0; i < MAX_TCP_THREAD; i++ )
  91. {
  92. // g_tcpThreadStruct存放TCP线程索引和设备uid;
  93. g_tcpThreadStruct[i].nTcpTheadIndex = 0;
  94. g_tcpThreadStruct[i].strUid = "";
  95. }
  96. // 从数据库中获取设备信息;
  97. CDBInterface::GetInstancePtr()->GetDevInfo();
  98. // 初始化设备在线采集数据 互斥量;
  99. InitializeCriticalSection( &g_csDevOnlineReadOneData );
  100. // **********************************************加载各种采集I/O设备动态库**********************************************;
  101. // 改进:加载动态库时,应该考虑如何根据设备信息来动态加载,而不是一次全部加载;
  102. LoadAscDLL(g_strDirectory);
  103. LoadRtuDll(g_strDirectory);
  104. LoadTcpDll(g_strDirectory);
  105. LoadIcpdasDll(g_strDirectory);
  106. LoadSnmpDll(g_strDirectory);
  107. LoadParadigmDll(g_strDirectory);
  108. LoadStulzDll(g_strDirectory);
  109. LoadDaiKinDll(g_strDirectory);
  110. LoadPowerwareDll(g_strDirectory);
  111. LoadGreeDll(g_strDirectory);
  112. LoadCarelDll(g_strDirectory);
  113. LoadSeparateDll(g_strDirectory);
  114. LoadMostDeviceDll(g_strDirectory);
  115. // 获取设备总数;
  116. int iSize = (int)g_pDevicesManager->m_Devices.GetSize();
  117. //int iVarCount = 0;
  118. #if IS_USER_MUL_THREAD_REQUEST // 是否使用多线程请求; 现在 == 1, 未知这段代码有何用?;
  119. int nDllType[MAX_DLL_TYPE] = {0};
  120. for( int i = 0; i < MAX_DLL_TYPE; i++ )
  121. {
  122. nDllType[i] = 0; // ?未知该变量意义 ;
  123. g_hRequestDataThread[i] = NULL; // 线程句柄;
  124. g_strPort[i] = ""; // 通信端口;
  125. }
  126. #endif
  127. /************************************************************************/
  128. /*
  129. 0.循环所有设备->next;
  130. 1.判断设备类型->next;
  131. 2.打开串口 ->next;
  132. 3.判断是否使用->next;
  133. 4.创建串口线程->end;
  134. */
  135. /************************************************************************/
  136. int iVarCount = 0; // 存储所有变量总数;
  137. // 遍历所有设备,创建对应串口线程,进行设备采集;
  138. int nBaudRate, nDataBit;
  139. for( int i = 0; i < iSize; i++ )
  140. {
  141. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  142. char chDllName[MAX_PATH] = {0};
  143. char szIniName[MAX_PATH] = {0};
  144. strcpy(chDllName, (char *)(LPCTSTR)pDev->m_strProtocoldllname);
  145. strcpy(szIniName, (char *)(LPCTSTR)pDev->m_strIniName);
  146. StrLower(chDllName,chDllName);
  147. StrLower(szIniName, szIniName);
  148. //LOG4C((LOG_NOTICE, "Devive = %s, IniName = %s", pDev->m_strDeviceName, szIniName));
  149. nBaudRate = GetCommRate( pDev->m_iBaudrate );
  150. nDataBit = GetCommDataBit( pDev->m_iDatabit );
  151. iVarCount += pDev->m_Vars.GetSize();
  152. /************************************************************************/
  153. /*
  154. 1.判断设备类型->next;
  155. 2.打开串口 ->next;
  156. 3.判断是否使用->next;
  157. 4.创建串口线程->end;
  158. */
  159. /************************************************************************/
  160. // == 0 RTU设备;
  161. if( pDev->m_bySpecialdevice == 0 )
  162. {
  163. if (!InitRtuComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate, nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  164. pDev->m_bCommStatus = FALSE; // 打开串口失败;
  165. else
  166. pDev->m_bCommStatus = TRUE; // 打开串口成功;
  167. // 如果该 设备变量个数大于0,进行数据采集,创建线程;
  168. if( pDev->m_Vars.GetSize() >= 1 )
  169. {
  170. DWORD dwThreadId;
  171. CString strPort;
  172. strPort.Format("%d", pDev->m_iPort);
  173. // 判断串口是否已经使用(是否存在数组里),未使用继续执行代码;
  174. if( FALSE == FindRtuCommPort(strPort) )
  175. {
  176. g_strPort[g_nRtuThreadIndex] = strPort;
  177. if( g_hRequestDataThread[g_nRtuThreadIndex] == NULL )
  178. {
  179. // 一个串口一个线程;
  180. MTVERIFY( g_hRequestDataThread[g_nRtuThreadIndex] =
  181. CreateThread(
  182. NULL,
  183. 0,
  184. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  185. (char *)(LPCTSTR)g_strPort[g_nRtuThreadIndex],
  186. 0,
  187. &dwThreadId) );
  188. g_nRtuThreadIndex++;
  189. }
  190. }
  191. }
  192. }
  193. // == 1,ASC设备;
  194. else if( pDev->m_bySpecialdevice == 1 )
  195. {
  196. if (!InitAscComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  197. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  198. {
  199. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  200. pDev->m_bCommStatus = FALSE;
  201. }
  202. else
  203. {
  204. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  205. pDev->m_bCommStatus = TRUE;
  206. }
  207. if( pDev->m_Vars.GetSize() >= 1 )
  208. {
  209. CString strPort;
  210. strPort.Format("%d", pDev->m_iPort);
  211. if( FALSE == FindAscCommPort(strPort) )
  212. {
  213. g_strPort[g_nAscThreadIndex].Format("%d", pDev->m_iPort);
  214. DWORD dwThreadId;
  215. MTVERIFY( g_hRequestDataThread[g_nAscThreadIndex] =
  216. CreateThread(
  217. NULL,
  218. 0,
  219. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  220. (char *)(LPCTSTR)g_strPort[g_nAscThreadIndex],
  221. 0,
  222. &dwThreadId) );
  223. g_nAscThreadIndex++;
  224. }
  225. }
  226. }
  227. // == 2,网络设备;
  228. else if( pDev->m_bySpecialdevice == 2 )
  229. {
  230. if (!InitTcpNet(pDev->m_iDevideaddr, pDev->m_iIpport, (char *)(LPCTSTR)pDev->m_strIp))
  231. {
  232. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  233. pDev->m_bCommStatus = FALSE;
  234. }
  235. else
  236. {
  237. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  238. pDev->m_bCommStatus = TRUE;
  239. }
  240. if( pDev->m_Vars.GetSize() >= 1 )
  241. {
  242. DWORD dwThreadId;
  243. MTVERIFY( g_hRequestDataThread[g_nTcpThreadIndex] =
  244. CreateThread(
  245. NULL,
  246. 0,
  247. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  248. (char *)(LPCTSTR)pDev->m_strUid,
  249. 0,
  250. &dwThreadId) );
  251. g_tcpThreadStruct[g_nTcpThreadStructIndex].nTcpTheadIndex = g_nTcpThreadIndex;
  252. g_tcpThreadStruct[g_nTcpThreadStructIndex].strUid = pDev->m_strUid;
  253. g_nTcpThreadStructIndex++;
  254. g_nTcpThreadIndex++;
  255. }
  256. }
  257. else if( pDev->m_bySpecialdevice == 3 )
  258. {
  259. int iPingResult = 0;
  260. // ping 10 次,如果都是timeout, 认为设备已断线
  261. for( int iPingIndex = 0; iPingIndex < 10; iPingIndex++ )
  262. {
  263. if( Ping(2000, pDev->m_strIp) == 0 )
  264. {
  265. iPingResult = 0;
  266. break;
  267. }
  268. else
  269. {
  270. iPingResult = 1;
  271. }
  272. }
  273. if( 1 == iPingResult ) // 设备已断线
  274. {
  275. pDev->m_bCommStatus = FALSE;
  276. }
  277. else
  278. {
  279. pDev->m_bCommStatus = TRUE;
  280. }
  281. if( pDev->m_Vars.GetSize() >= 1 )
  282. {
  283. DWORD dwThreadId;
  284. MTVERIFY( g_hRequestDataThread[g_nSnmpThreadIndex] =
  285. CreateThread(
  286. NULL,
  287. 0,
  288. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  289. (char *)(LPCTSTR)pDev->m_strUid,
  290. 0,
  291. &dwThreadId) );
  292. g_nSnmpThreadIndex++;
  293. }
  294. }
  295. else if( pDev->m_bySpecialdevice == 4 )
  296. {
  297. if ( strcmp(chDllName, "icpdas.dll") == 0 )
  298. {
  299. if (!InitIcpdasComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  300. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  301. {
  302. LOG4C((LOG_NOTICE, "device name:%s, port:%d, addr=%d, baudrate=%d, databit=%d, stopbit=%d, paritybit=%d open com lost!",
  303. pDev->m_strDeviceName, pDev->m_iPort, pDev->m_iDevideaddr, nBaudRate, nDataBit, pDev->m_iStopbit, pDev->m_iParitybit));
  304. pDev->m_bCommStatus = FALSE;
  305. }
  306. else
  307. {
  308. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  309. pDev->m_bCommStatus = TRUE;
  310. }
  311. CString strPort;
  312. strPort.Format("%d", pDev->m_iPort);
  313. if( FALSE == FindIcpdasCommPort(strPort) )
  314. {
  315. g_strPort[g_nIcpdasIndex].Format("%d", pDev->m_iPort);
  316. if( pDev->m_Vars.GetSize() >= 1 )
  317. {
  318. //LOG4C((LOG_NOTICE, "create thread by hg module port = com%s", g_strPort[g_nIcpdasIndex] ));
  319. DWORD dwThreadId;
  320. MTVERIFY( g_hRequestDataThread[g_nIcpdasIndex] =
  321. CreateThread(
  322. NULL,
  323. 0,
  324. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  325. (char *)(LPCTSTR)g_strPort[g_nIcpdasIndex],
  326. 0,
  327. &dwThreadId) );
  328. g_nIcpdasIndex++;
  329. }
  330. }
  331. }
  332. else if ( strcmp(chDllName, "upsparadigm.dll") == 0 )
  333. {
  334. if (!InitParadigmComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  335. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  336. {
  337. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  338. pDev->m_bCommStatus = FALSE;
  339. }
  340. else
  341. {
  342. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  343. pDev->m_bCommStatus = TRUE;
  344. }
  345. CString strPort;
  346. strPort.Format("%d", pDev->m_iPort);
  347. if( FALSE == FindParadigmCommPort(strPort) )
  348. {
  349. g_strPort[g_nParadigmIndex].Format("%d", pDev->m_iPort);
  350. if( pDev->m_Vars.GetSize() >= 1 )
  351. {
  352. DWORD dwThreadId;
  353. MTVERIFY( g_hRequestDataThread[g_nParadigmIndex] =
  354. CreateThread(
  355. NULL,
  356. 0,
  357. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  358. (char *)(LPCTSTR)g_strPort[g_nParadigmIndex],
  359. 0,
  360. &dwThreadId) );
  361. g_nParadigmIndex++;
  362. }
  363. }
  364. }
  365. else if ( strcmp(chDllName, "stulz.dll") == 0 )
  366. {
  367. if (!InitStulzComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  368. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  369. {
  370. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  371. pDev->m_bCommStatus = FALSE;
  372. }
  373. else
  374. {
  375. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  376. pDev->m_bCommStatus = TRUE;
  377. }
  378. CString strPort;
  379. strPort.Format("%d", pDev->m_iPort);
  380. if( FALSE == FindStulzCommPort(strPort) )
  381. {
  382. g_strPort[g_nStulzIndex].Format("%d", pDev->m_iPort);
  383. if( pDev->m_Vars.GetSize() >= 1 )
  384. {
  385. DWORD dwThreadId;
  386. MTVERIFY( g_hRequestDataThread[g_nStulzIndex] =
  387. CreateThread(
  388. NULL,
  389. 0,
  390. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  391. (char *)(LPCTSTR)g_strPort[g_nStulzIndex],
  392. 0,
  393. &dwThreadId) );
  394. g_nStulzIndex++;
  395. }
  396. }
  397. }
  398. else if ( strcmp(chDllName, "daikin.dll") == 0 )
  399. {
  400. if( !InitDaiKinComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  401. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000) )
  402. {
  403. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  404. pDev->m_bCommStatus = FALSE;
  405. }
  406. else
  407. {
  408. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  409. pDev->m_bCommStatus = TRUE;
  410. }
  411. CString strPort;
  412. strPort.Format("%d", pDev->m_iPort);
  413. if( FALSE == FindDaiKinCommPort(strPort) )
  414. {
  415. g_strPort[g_nDaiKinIndex].Format("%d", pDev->m_iPort);
  416. if( pDev->m_Vars.GetSize() >= 1 )
  417. {
  418. DWORD dwThreadId;
  419. MTVERIFY( g_hRequestDataThread[g_nDaiKinIndex] =
  420. CreateThread(
  421. NULL,
  422. 0,
  423. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  424. (char *)(LPCTSTR)g_strPort[g_nDaiKinIndex],
  425. 0,
  426. &dwThreadId) );
  427. g_nDaiKinIndex++;
  428. }
  429. }
  430. }
  431. else if ( strcmp(chDllName, "powerware.dll") == 0 )
  432. {
  433. if( !InitPowerwareComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  434. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000) )
  435. {
  436. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  437. pDev->m_bCommStatus = FALSE;
  438. }
  439. else
  440. {
  441. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  442. pDev->m_bCommStatus = TRUE;
  443. }
  444. CString strPort;
  445. strPort.Format("%d", pDev->m_iPort);
  446. if( FALSE == FindPowerwareCommPort(strPort) )
  447. {
  448. g_strPort[g_nPowerwareIndex].Format("%d", pDev->m_iPort);
  449. if( pDev->m_Vars.GetSize() >= 1 )
  450. {
  451. DWORD dwThreadId;
  452. MTVERIFY( g_hRequestDataThread[g_nPowerwareIndex] =
  453. CreateThread(
  454. NULL,
  455. 0,
  456. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  457. (char *)(LPCTSTR)g_strPort[g_nPowerwareIndex],
  458. 0,
  459. &dwThreadId) );
  460. g_nPowerwareIndex++;
  461. }
  462. }
  463. }
  464. else if ( strcmp(chDllName, "gree.dll") == 0 )
  465. {
  466. if (!InitGreeComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  467. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  468. {
  469. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  470. pDev->m_bCommStatus = FALSE;
  471. }
  472. else
  473. {
  474. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  475. pDev->m_bCommStatus = TRUE;
  476. }
  477. CString strPort;
  478. strPort.Format("%d", pDev->m_iPort);
  479. if( FALSE == FindGreeCommPort(strPort) )
  480. {
  481. g_strPort[g_nGreeIndex].Format("%d", pDev->m_iPort);
  482. if( pDev->m_Vars.GetSize() >= 1 )
  483. {
  484. DWORD dwThreadId;
  485. MTVERIFY( g_hRequestDataThread[g_nGreeIndex] =
  486. CreateThread(
  487. NULL,
  488. 0,
  489. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  490. (char *)(LPCTSTR)g_strPort[g_nGreeIndex],
  491. 0,
  492. &dwThreadId) );
  493. g_nGreeIndex++;
  494. }
  495. }
  496. }
  497. else if ( strcmp(chDllName, "carel.dll") == 0 )
  498. {
  499. if (!InitCarelComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  500. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  501. {
  502. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  503. pDev->m_bCommStatus = FALSE;
  504. }
  505. else
  506. {
  507. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  508. pDev->m_bCommStatus = TRUE;
  509. }
  510. CString strPort;
  511. strPort.Format("%d", pDev->m_iPort);
  512. if( FALSE == FindCarelCommPort(strPort) )
  513. {
  514. g_strPort[g_nCarelIndex].Format("%d", pDev->m_iPort);
  515. if( pDev->m_Vars.GetSize() >= 1 )
  516. {
  517. DWORD dwThreadId;
  518. MTVERIFY( g_hRequestDataThread[g_nCarelIndex] =
  519. CreateThread(
  520. NULL,
  521. 0,
  522. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  523. (char *)(LPCTSTR)g_strPort[g_nCarelIndex],
  524. 0,
  525. &dwThreadId) );
  526. g_nCarelIndex++;
  527. }
  528. }
  529. }
  530. else if( strcmp( chDllName, "scantak_3c.dll" ) == 0 )
  531. {
  532. if (!InitSeparateComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  533. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  534. {
  535. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  536. pDev->m_bCommStatus = FALSE;
  537. }
  538. else
  539. {
  540. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  541. pDev->m_bCommStatus = TRUE;
  542. }
  543. CString strPort;
  544. strPort.Format("%d", pDev->m_iPort);
  545. if( FALSE == FindScanTakCommPort(strPort) )
  546. {
  547. g_strPort[g_nScantakIndex].Format("%d", pDev->m_iPort);
  548. if( pDev->m_Vars.GetSize() >= 1 )
  549. {
  550. DWORD dwThreadId;
  551. MTVERIFY( g_hRequestDataThread[g_nScantakIndex] =
  552. CreateThread(
  553. NULL,
  554. 0,
  555. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  556. (char *)(LPCTSTR)g_strPort[g_nScantakIndex],
  557. 0,
  558. &dwThreadId) );
  559. g_nScantakIndex++;
  560. }
  561. }
  562. }
  563. else if( strcmp( chDllName, "delta.dll" ) == 0 )
  564. {
  565. if (!InitSeparateComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  566. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  567. {
  568. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  569. pDev->m_bCommStatus = FALSE;
  570. }
  571. else
  572. {
  573. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  574. pDev->m_bCommStatus = TRUE;
  575. }
  576. CString strPort;
  577. strPort.Format("%d", pDev->m_iPort);
  578. if( FALSE == FindDeltaCommPort(strPort) )
  579. {
  580. g_strPort[g_nDeltaIndex].Format("%d", pDev->m_iPort);
  581. if( pDev->m_Vars.GetSize() >= 1 )
  582. {
  583. DWORD dwThreadId;
  584. MTVERIFY( g_hRequestDataThread[g_nDeltaIndex] =
  585. CreateThread(
  586. NULL,
  587. 0,
  588. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  589. (char *)(LPCTSTR)g_strPort[g_nDeltaIndex],
  590. 0,
  591. &dwThreadId) );
  592. g_nDeltaIndex++;
  593. }
  594. }
  595. }
  596. else if ( strcmp(szIniName, "upsuh31.ini") == 0 ||
  597. strcmp(szIniName, "emerson_cm.ini") == 0 ||
  598. strcmp(szIniName, "dme3000.ini") == 0 ||
  599. strcmp(szIniName, "paradigm200a_rectifier.ini") == 0 ||
  600. strcmp(szIniName, "paradigm200a_inverter.ini") == 0 ||
  601. strcmp(szIniName, "paradigm200b.ini") == 0 ||
  602. strcmp(szIniName, "hipulseigmnet.ini") == 0 ||
  603. strcmp(szIniName, "hipulsecdt.ini") == 0 ||
  604. strcmp(szIniName, "prostar.ini") == 0 ||
  605. strcmp(szIniName, "joton.ini") == 0 ||
  606. strcmp(szIniName, "hiross.ini") == 0 ||
  607. strcmp(szIniName, "upslink.ini") == 0 ||
  608. strcmp(szIniName, "upsapcsilcon.ini") == 0 ||
  609. strcmp(szIniName, "ul33.ini") == 0 ||
  610. strcmp(szIniName, "hipulse_u.ini") == 0 ||
  611. strcmp(szIniName, "kehua.ini") == 0 ||
  612. strcmp(szIniName, "secups.ini") == 0 ||
  613. strcmp(szIniName, "upsuh11.ini") == 0 ||
  614. strcmp(szIniName, "weibo.ini") == 0 ||
  615. strcmp(szIniName, "sda_ii.ini") == 0 ||
  616. strcmp(szIniName, "daikin.ini") == 0 ||
  617. strcmp(szIniName, "scantak_3c.ini") == 0 ||
  618. strcmp(szIniName, "delta.ini") == 0 ||
  619. strcmp(szIniName, "m84c5z.ini") == 0 ||
  620. strcmp(szIniName, "upsnxr.ini") == 0 ||
  621. strcmp(szIniName, "batteryadu.ini") == 0 ||
  622. strcmp(szIniName, "upsmonitorlan.ini") == 0 ||
  623. strcmp(szIniName, "sk6000.ini") == 0 ||
  624. strcmp(szIniName, "batteryadu2000.ini") == 0 ||
  625. strcmp(szIniName, "santak_shiwei.ini") == 0 ||
  626. strcmp(szIniName, "santak_q4.ini") == 0 ||
  627. strcmp(szIniName, "ups_nxl.ini") == 0 ||
  628. strcmp(szIniName, "twoul33.ini") == 0 ||
  629. strcmp(szIniName, "oumai.ini") == 0 ||
  630. strcmp(szIniName, "santak_3a3120ks.ini") == 0 ||
  631. strcmp(szIniName, "tmaster.ini") == 0 ||
  632. strcmp(szIniName, "st_6c20ks.ini") == 0 ||
  633. strcmp(szIniName, "ea800.ini") == 0 ||
  634. strcmp(szIniName, "jb1501a64p.ini") == 0||
  635. strcmp(szIniName, "st_3c340ks.ini") == 0||
  636. strcmp(szIniName, "delta_v1.54.ini") == 0||
  637. strcmp(szIniName, "icp_i7017.ini") == 0 ||
  638. strcmp(szIniName, "fad.ini") == 0 ||
  639. strcmp(szIniName, "itrustadapt.ini" ) == 0 ||
  640. strcmp(szIniName, "uha3r_0200l.ini") == 0)
  641. {
  642. //LOG4C((LOG_NOTICE, "Enter InitMostDeviceComm szIniName = %s", szIniName));
  643. if (!InitMostDeviceComm(g_strDirectory, szIniName, pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  644. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  645. {
  646. LOG4C((LOG_NOTICE, "device name:%s open com%d lost!", pDev->m_strDeviceName,pDev->m_iPort));
  647. pDev->m_bCommStatus = FALSE;
  648. }
  649. else
  650. {
  651. LOG4C((LOG_NOTICE, "device name:%s open com%d success!", pDev->m_strDeviceName,pDev->m_iPort));
  652. pDev->m_bCommStatus = TRUE;
  653. }
  654. //LOG4C((LOG_NOTICE, "Leave InitMostDeviceComm szIniName = %s", szIniName));
  655. CString strPort;
  656. strPort.Format("%d", pDev->m_iPort);
  657. if( FALSE == FindMostDeviceCommPort(strPort) )
  658. {
  659. g_strPort[g_nMostDeviceIndex].Format("%d", pDev->m_iPort);
  660. if( pDev->m_Vars.GetSize() >= 1 )
  661. {
  662. DWORD dwThreadId;
  663. MTVERIFY( g_hRequestDataThread[g_nMostDeviceIndex] =
  664. CreateThread(
  665. NULL,
  666. 0,
  667. (LPTHREAD_START_ROUTINE)RequestDataThreadProc,
  668. (char *)(LPCTSTR)g_strPort[g_nMostDeviceIndex],
  669. 0,
  670. &dwThreadId) );
  671. g_nMostDeviceIndex++;
  672. }
  673. }
  674. }
  675. else if ( 0 == strcmp(szIniName, "stu2000.dll") )
  676. {
  677. }
  678. }
  679. }
  680. LOG4C((LOG_NOTICE, "iVarCount = %d", iVarCount));
  681. // 每MAX_SINGLE_TCP_VAR_NUM个变量,建立一个监听端口
  682. #if 0
  683. int nSocketIndex = 1;
  684. if( iVarCount > MAX_SINGLE_TCP_VAR_NUM )
  685. {
  686. while( iVarCount / (nSocketIndex * MAX_SINGLE_TCP_VAR_NUM) != 0 )
  687. {
  688. if( NULL == g_pServerSocket[nSocketIndex] )
  689. {
  690. g_pServerSocket[nSocketIndex] = new CServerSocket();
  691. CString strPort;
  692. strPort.Format("%d", SOCKET_SERVER_PORT + nSocketIndex);
  693. LOG4C((LOG_NOTICE, "Create Port %s success", strPort));
  694. g_pServerSocket[nSocketIndex]->SvrStart(strPort);
  695. nSocketIndex++;
  696. }
  697. }
  698. }
  699. #else
  700. int nSocketIndex = 1;
  701. if( NULL == g_pServerSocket[nSocketIndex] )
  702. {
  703. g_pServerSocket[nSocketIndex] = new CServerSocket();
  704. CString strPort;
  705. strPort.Format("%d", SOCKET_SERVER_PORT + nSocketIndex);
  706. LOG4C((LOG_NOTICE, "Create Port %s success", strPort));
  707. g_pServerSocket[nSocketIndex]->SvrStart(strPort);
  708. }
  709. #endif
  710. // 实时监控设备是否在线;
  711. if( g_hDevOnlineThread == NULL )
  712. {
  713. DWORD dwThreadId;
  714. MTVERIFY( g_hDevOnlineThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)DevOnlineThreadProc, 0, 0, &dwThreadId) );
  715. }
  716. // 实时监控,超时值外设备断开进行重连;
  717. if( g_hReConnectCommThread == NULL )
  718. {
  719. DWORD dwThreadId;
  720. MTVERIFY( g_hReConnectCommThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReConnectCommThreadProc, NULL, 0, &dwThreadId) );
  721. }
  722. // 定时发送系统正常运行短信;
  723. if( g_hTimingSendSmsThread == NULL )
  724. {
  725. DWORD dwThreadId;
  726. if( g_nSendSms==1 || g_nSendNotice==1 )
  727. {
  728. MTVERIFY( g_hTimingSendSmsThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TimingSendSmsThread, NULL, 0, &dwThreadId) );
  729. }
  730. }
  731. // 实时监控报警,发送报警信息;
  732. if( g_hVoiceNoticeThread == NULL )
  733. {
  734. DWORD dwThreadId;
  735. MTVERIFY( g_hVoiceNoticeThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)VoiceNoticeThreadProc, NULL, 0, &dwThreadId) );
  736. }
  737. // 对外输出,供第三方使用;
  738. if( g_hOutputThread == NULL )
  739. {
  740. if( 1 == g_nOutputEnable )
  741. {
  742. DWORD dwThreadId;
  743. MTVERIFY( g_hOutputThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)OutputThreadProc, NULL, 0, &dwThreadId) );
  744. }
  745. }
  746. // 时间计划;
  747. if( g_hGetDatePlanThread == NULL )
  748. {
  749. DWORD dwThreadId;
  750. MTVERIFY( g_hGetDatePlanThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GetDatePlanThreadProc, NULL, 0, &dwThreadId) );
  751. }
  752. return 0;
  753. }
  754. void EndMain()
  755. {
  756. #if IS_USER_MUL_THREAD_REQUEST
  757. for( int i = 0; i < MAX_DLL_TYPE; i++ )
  758. {
  759. if (NULL != g_hRequestDataThread[i])
  760. {
  761. MTVERIFY( WaitForSingleObject( g_hRequestDataThread[i], INFINITE ) != WAIT_FAILED );
  762. MTVERIFY( CloseHandle( g_hRequestDataThread[i] ) );
  763. g_hRequestDataThread[i] = NULL;
  764. }
  765. }
  766. #else
  767. if (NULL != g_hRequestDataThread)
  768. {
  769. MTVERIFY( WaitForSingleObject( g_hRequestDataThread, INFINITE ) != WAIT_FAILED );
  770. MTVERIFY( CloseHandle( g_hRequestDataThread ) );
  771. g_hRequestDataThread = NULL;
  772. }
  773. #endif
  774. if( NULL != g_hDevOnlineThread )
  775. {
  776. MTVERIFY( WaitForSingleObject( g_hDevOnlineThread, INFINITE ) != WAIT_FAILED );
  777. MTVERIFY( CloseHandle( g_hDevOnlineThread ) );
  778. g_hDevOnlineThread = NULL;
  779. }
  780. if( NULL != g_hReConnectCommThread )
  781. {
  782. MTVERIFY( WaitForSingleObject( g_hReConnectCommThread, INFINITE ) != WAIT_FAILED );
  783. MTVERIFY( CloseHandle( g_hReConnectCommThread ) );
  784. g_hReConnectCommThread = NULL;
  785. }
  786. if( g_hTimingSendSmsThread != NULL )
  787. {
  788. MTVERIFY( WaitForSingleObject( g_hTimingSendSmsThread, INFINITE ) != WAIT_FAILED );
  789. MTVERIFY( CloseHandle( g_hTimingSendSmsThread ) );
  790. g_hTimingSendSmsThread = NULL;
  791. }
  792. if( g_hVoiceNoticeThread != NULL )
  793. {
  794. MTVERIFY( WaitForSingleObject( g_hVoiceNoticeThread, INFINITE ) != WAIT_FAILED );
  795. MTVERIFY( CloseHandle( g_hVoiceNoticeThread ) );
  796. g_hVoiceNoticeThread = NULL;
  797. }
  798. if( 1 == g_nOutputEnable )
  799. {
  800. if( NULL != g_hOutputThread )
  801. {
  802. MTVERIFY( WaitForSingleObject( g_hOutputThread, INFINITE ) != WAIT_FAILED );
  803. MTVERIFY( CloseHandle( g_hOutputThread ) );
  804. g_hOutputThread = NULL;
  805. }
  806. }
  807. if( NULL != g_hGetDatePlanThread )
  808. {
  809. MTVERIFY( WaitForSingleObject( g_hGetDatePlanThread, INFINITE ) != WAIT_FAILED );
  810. MTVERIFY( CloseHandle( g_hGetDatePlanThread ) );
  811. g_hGetDatePlanThread = NULL;
  812. }
  813. DeleteCriticalSection( &g_csDevOnlineReadOneData );
  814. }
  815. //
  816. // 以下12个动态库重连函数,均由线程ReConnectCommThreadProc调用;
  817. // 参数是单个设备 ,当设备状态 = 断开时,以下重连函数执行;;
  818. // 该函数是按变量进行采集.
  819. BOOL ModbusRtuReConnectProc(CDevice *pDev)
  820. {
  821. BOOL bResult = FALSE; // 返回结果;
  822. char chMsg[80] = {0};
  823. SETBASEPARAM SetBasePara = {0};
  824. SetBasePara.nAddr = pDev->m_iDevideaddr;
  825. // 循环所有变量
  826. int nVarSize = pDev->m_Vars.GetSize();
  827. nVarSize = nVarSize<10 ? nVarSize : 10;
  828. for( int j = 0; j < nVarSize; j++ )
  829. {
  830. //if( j > 0 ) break;
  831. CBaseVar *pBaseVar = pDev->m_Vars[j];
  832. if( pBaseVar->m_bReadOnly == 1 ) continue; // 变量 == 1 只写;
  833. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  834. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  835. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  836. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  837. if( pBaseVar->m_nVarTypeID >= SPECIAL_VAR_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= SPECIAL_VAR_TYPE_MAX_ID ) // 特殊变量
  838. {
  839. // RtuRequestSpecData不属于动态接口函数,是本服务的函数处理特殊变量;
  840. if (RtuRequestSpecData(SetBasePara, pBaseVar->m_iDataLen, pDev->m_iPort, pDev->m_iDevideaddr,
  841. (char *)(LPCTSTR)pBaseVar->m_strVarUID, pBaseVar->m_nVarID, pBaseVar->m_nVarTypeID) == 0)
  842. {
  843. bResult = TRUE;
  844. break;
  845. }
  846. }
  847. else
  848. {
  849. // 动态库接口函数,处理一般变量;
  850. if (pRTU_DLLInit(pDev->m_iPort, pDev->m_iDevideaddr, SetBasePara, pBaseVar->m_iDataLen, chMsg)==0)//初始化设备DLL
  851. {
  852. bResult = TRUE;
  853. break;
  854. }
  855. }
  856. }
  857. return bResult;
  858. }
  859. BOOL ModbusAscReConnectProc(CDevice *pDev)
  860. {
  861. BOOL bResult = FALSE;
  862. char chMsg[80] = {0};
  863. SETBASEPARAM SetBasePara = {0};
  864. SetBasePara.nAddr = pDev->m_iDevideaddr;
  865. // 循环所有变量
  866. int nVarSize = pDev->m_Vars.GetSize();
  867. nVarSize = nVarSize<10 ? nVarSize : 10;
  868. for( int j = 0; j < nVarSize; j++ )
  869. {
  870. //if( j > 0 ) break;
  871. CBaseVar *pBaseVar = pDev->m_Vars[j];
  872. if( pBaseVar->m_bReadOnly == 1 ) continue;
  873. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  874. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  875. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  876. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  877. if( pBaseVar->m_nVarTypeID >= SPECIAL_VAR_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= SPECIAL_VAR_TYPE_MAX_ID ) // 特殊变量
  878. {
  879. if (AscRequestSpecData((char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, pBaseVar->m_nVarTypeID) == 0)
  880. {
  881. bResult = TRUE;
  882. break;
  883. }
  884. }
  885. else
  886. {
  887. if(pASCDLLInit(pDev->m_iPort, pDev->m_iDevideaddr, SetBasePara, pBaseVar->m_iDataLen, chMsg)==0)//初始化设备DLL
  888. {
  889. bResult = TRUE;
  890. break;
  891. }
  892. }
  893. }
  894. return bResult;
  895. }
  896. BOOL SnmpReConnectProc(CDevice *pDev)
  897. {
  898. BOOL bResult = FALSE;
  899. char chMsg[80] = {0};
  900. // 循环所有变量
  901. int nVarSize = pDev->m_Vars.GetSize();
  902. nVarSize = nVarSize<10 ? nVarSize : 10;
  903. for( int j = 0; j < nVarSize; j++ )
  904. {
  905. //if( j > 0 ) break;
  906. CBaseVar *pBaseVar = pDev->m_Vars[j];
  907. if( pBaseVar->m_bReadOnly == 1 ) continue;
  908. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  909. if( pSnmpDllGetValue )
  910. {
  911. //TRACE("变量值id=%d\r\n", pBaseVar->m_nVarID);
  912. int nRet = pSnmpDllGetValue((char *)(LPCTSTR)pDev->m_strIp,
  913. (char *)(LPCTSTR)pBaseVar->m_strFields,
  914. (char *)(LPCTSTR)pBaseVar->m_strSnmpoi,
  915. chMsg);
  916. if( nRet == 0 )
  917. {
  918. bResult = TRUE;
  919. break;
  920. }
  921. }
  922. }
  923. return bResult;
  924. }
  925. BOOL IcpdasReConnectProc(CDevice *pDev)
  926. {
  927. BOOL bResult = FALSE;
  928. char chMsg[80] = {0};
  929. SETBASEPARAM SetBasePara = {0};
  930. SetBasePara.nAddr = pDev->m_iDevideaddr;
  931. // 循环所有变量
  932. int nVarSize = pDev->m_Vars.GetSize();
  933. nVarSize = nVarSize<10 ? nVarSize : 10;
  934. for( int j = 0; j < nVarSize; j++ )
  935. {
  936. //if( j > 0 ) break;
  937. CBaseVar *pBaseVar = pDev->m_Vars[j];
  938. if( pBaseVar->m_bReadOnly == 1 ) continue;
  939. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  940. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  941. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  942. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  943. if (IcpdasRequestData(j, SetBasePara, pBaseVar->m_iDataLen, pDev->m_iPort, pDev->m_iDevideaddr,
  944. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char *)(LPCTSTR)pBaseVar->m_strRs232cmd) == 0)
  945. {
  946. bResult = TRUE;
  947. break;
  948. }
  949. }
  950. return bResult;
  951. }
  952. BOOL ParadigmReConnectProc(CDevice *pDev)
  953. {
  954. BOOL bResult = FALSE;
  955. char chMsg[80] = {0};
  956. // 循环所有变量
  957. int nVarSize = pDev->m_Vars.GetSize();
  958. nVarSize = nVarSize<10 ? nVarSize : 10;
  959. for( int j = 0; j < nVarSize; j++ )
  960. {
  961. //if( j > 0 ) break;
  962. CBaseVar *pBaseVar = pDev->m_Vars[j];
  963. if( pBaseVar->m_bReadOnly == 1 ) continue;
  964. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  965. if (ParadigmRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  966. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char*)(LPCTSTR)pBaseVar->m_strRs232cmd) == 0)
  967. {
  968. bResult = TRUE;
  969. break;
  970. }
  971. }
  972. return bResult;
  973. }
  974. BOOL StulzReConnectProc(CDevice *pDev)
  975. {
  976. BOOL bResult = FALSE;
  977. char chMsg[80] = {0};
  978. SETBASEPARAM SetBasePara = {0};
  979. SetBasePara.nAddr = pDev->m_iDevideaddr;
  980. // 循环所有变量
  981. int nVarSize = pDev->m_Vars.GetSize();
  982. nVarSize = nVarSize<10 ? nVarSize : 10;
  983. for( int j = 0; j < nVarSize; j++ )
  984. {
  985. //if( j > 0 ) break;
  986. CBaseVar *pBaseVar = pDev->m_Vars[j];
  987. if( pBaseVar->m_bReadOnly == 1 ) continue;
  988. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  989. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  990. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  991. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  992. if (StulzRequestData(SetBasePara, pBaseVar->m_iDataLen, pDev->m_iPort, pDev->m_iDevideaddr,
  993. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char*)(LPCTSTR)pBaseVar->m_strRs232cmd) == 0)
  994. {
  995. bResult = TRUE;
  996. break;
  997. }
  998. }
  999. return bResult;
  1000. }
  1001. BOOL DaiKinReConnectProc(CDevice *pDev)
  1002. {
  1003. BOOL bResult = FALSE;
  1004. char chMsg[80] = {0};
  1005. // 循环所有变量
  1006. int nVarSize = pDev->m_Vars.GetSize();
  1007. nVarSize = nVarSize<10 ? nVarSize : 10;
  1008. for( int j = 0; j < nVarSize; j++ )
  1009. {
  1010. //if( j > 0 ) break;
  1011. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1012. if( pBaseVar->m_bReadOnly == 1 ) continue;
  1013. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  1014. if (DaiKinRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  1015. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char*)(LPCTSTR)pBaseVar->m_strRs232cmd) == 0)
  1016. {
  1017. bResult = TRUE;
  1018. break;
  1019. }
  1020. }
  1021. return bResult;
  1022. }
  1023. BOOL PowerwareReConnectProc(CDevice *pDev)
  1024. {
  1025. BOOL bResult = FALSE;
  1026. char chMsg[80] = {0};
  1027. // 循环所有变量
  1028. int nVarSize = pDev->m_Vars.GetSize();
  1029. nVarSize = nVarSize<10 ? nVarSize : 10;
  1030. for( int j = 0; j < nVarSize; j++ )
  1031. {
  1032. //if( j > 0 ) break;
  1033. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1034. if( pBaseVar->m_bReadOnly == 1 ) continue;
  1035. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  1036. if (PowerwareRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  1037. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char*)(LPCTSTR)pBaseVar->m_strRs232cmd) == 0)
  1038. {
  1039. bResult = TRUE;
  1040. break;
  1041. }
  1042. }
  1043. return bResult;
  1044. }
  1045. BOOL GreeReConnectProc(CDevice *pDev)
  1046. {
  1047. BOOL bResult = FALSE;
  1048. char chMsg[80] = {0};
  1049. SETBASEPARAM SetBasePara = {0};
  1050. SetBasePara.nAddr = pDev->m_iDevideaddr;
  1051. // 循环所有变量
  1052. int nVarSize = pDev->m_Vars.GetSize();
  1053. nVarSize = nVarSize<10 ? nVarSize : 10;
  1054. for( int j = 0; j < nVarSize; j++ )
  1055. {
  1056. //if( j > 0 ) break;
  1057. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1058. if( pBaseVar->m_bReadOnly == 1 ) continue;
  1059. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  1060. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  1061. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  1062. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  1063. if (GreeRequestData(SetBasePara, pBaseVar->m_iDataLen, pDev->m_iPort, pDev->m_iDevideaddr,
  1064. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char*)(LPCTSTR)pBaseVar->m_strRs232cmd) == 0)
  1065. {
  1066. bResult = TRUE;
  1067. break;
  1068. }
  1069. }
  1070. return bResult;
  1071. }
  1072. BOOL CarelReConnectProc(CDevice *pDev)
  1073. {
  1074. BOOL bResult = FALSE;
  1075. char chMsg[80] = {0};
  1076. // 循环所有变量
  1077. int nVarSize = pDev->m_Vars.GetSize();
  1078. nVarSize = nVarSize<10 ? nVarSize : 10;
  1079. for( int j = 0; j < nVarSize; j++ )
  1080. {
  1081. //if( j > 0 ) break;
  1082. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1083. if( pBaseVar->m_bReadOnly == 1 ) continue;
  1084. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  1085. if (CarelRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  1086. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID,
  1087. (char*)(LPCTSTR)pBaseVar->m_strRs232cmd) == 0)
  1088. {
  1089. bResult = TRUE;
  1090. break;
  1091. }
  1092. }
  1093. return bResult;
  1094. }
  1095. BOOL SeparateReConnectProc(CDevice *pDev)
  1096. {
  1097. BOOL bResult = FALSE;
  1098. char chMsg[80] = {0};
  1099. char szIniName[MAX_PATH] = {0};
  1100. strcpy(szIniName, (char *)(LPCTSTR)pDev->m_strIniName);
  1101. StrLower(szIniName, szIniName);
  1102. // 循环所有变量
  1103. int nVarSize = pDev->m_Vars.GetSize();
  1104. nVarSize = nVarSize<10 ? nVarSize : 10;
  1105. for( int j = 0; j < nVarSize; j++ )
  1106. {
  1107. //if( j > 0 ) break;
  1108. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1109. if( pBaseVar->m_bReadOnly == 1 ) continue;
  1110. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  1111. if (SeparateRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  1112. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID,
  1113. (char*)(LPCTSTR)pBaseVar->m_strRs232cmd,
  1114. szIniName) == 0)
  1115. {
  1116. bResult = TRUE;
  1117. break;
  1118. }
  1119. }
  1120. return bResult;
  1121. }
  1122. BOOL MostDeviceReConnectProc(CDevice *pDev)
  1123. {
  1124. BOOL bResult = FALSE;
  1125. char chMsg[80] = {0};
  1126. char szIniName[MAX_PATH] = {0};
  1127. strcpy(szIniName, (char *)(LPCTSTR)pDev->m_strIniName);
  1128. StrLower(szIniName, szIniName);
  1129. // 循环所有变量
  1130. int nVarSize = pDev->m_Vars.GetSize();
  1131. nVarSize = nVarSize<10 ? nVarSize : 10;
  1132. for( int j = 0; j < nVarSize; j++ )
  1133. {
  1134. //if( j > 1 ) break;
  1135. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1136. if( pBaseVar->m_bReadOnly == 1 ) continue;
  1137. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  1138. if (MostDeviceRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  1139. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID,
  1140. (char*)(LPCTSTR)pBaseVar->m_strRs232cmd,
  1141. szIniName) == 0)
  1142. {
  1143. bResult = TRUE;
  1144. break;
  1145. }
  1146. }
  1147. return bResult;
  1148. }
  1149. //
  1150. int GetTcpThreadStructIndex(CString strUid)
  1151. {
  1152. int nRet = -1;
  1153. for( int i = 0; i < MAX_TCP_THREAD; i++ )
  1154. {
  1155. if( g_tcpThreadStruct[i].strUid.Compare(strUid) == 0 )
  1156. {
  1157. nRet = i;
  1158. break;
  1159. }
  1160. }
  1161. return nRet;
  1162. }
  1163. //
  1164. //以上13个函数,均由线程重连串口ReConnectCommThreadProc调用 ;
  1165. // 功能如下:
  1166. // 0.判断是否设备是否掉线,掉线则重连串口并采集;
  1167. // 1.每个类型设备,首先初始化.判断是否打开了该串口;
  1168. // 2.串口打开成功,进行数据采集,存储到变量和设备里;
  1169. DWORD WINAPI ReConnectCommThreadProc(void *Para)
  1170. {
  1171. do
  1172. {
  1173. int iSize = (int)g_pDevicesManager->m_Devices.GetSize();
  1174. int nBaudRate, nDataBit;
  1175. for( int i = 0; i < iSize; i++ )
  1176. {
  1177. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  1178. char chDllName[MAX_PATH] = {0};
  1179. char szIniName[MAX_PATH] = {0};
  1180. strcpy(chDllName, (char *)(LPCTSTR)pDev->m_strProtocoldllname);
  1181. strcpy(szIniName, (char *)(LPCTSTR)pDev->m_strIniName);
  1182. StrLower(chDllName,chDllName);
  1183. StrLower(szIniName, szIniName);
  1184. nBaudRate = GetCommRate( pDev->m_iBaudrate );
  1185. nDataBit = GetCommDataBit( pDev->m_iDatabit );
  1186. if( pDev->m_bySpecialdevice == 0 )
  1187. {
  1188. // 设备连接状态 = FALSE 断开,进行重连串口;
  1189. if( pDev->m_bCommStatus == FALSE )
  1190. {
  1191. // InitRtuComm初始串口函数 ,调用动态库接口函数进行初始化;
  1192. if (!InitRtuComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate, nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  1193. {
  1194. // 初始化失败,继续标识为断线;
  1195. pDev->m_bCommStatus = FALSE;
  1196. }
  1197. else
  1198. {
  1199. // 成功初始化串口,进行数据采集操作;
  1200. EnterCriticalSection( &g_csRtuReadOneData );
  1201. // 进行数据的采集;抽取动态函数;
  1202. pDev->m_bCommStatus = ModbusRtuReConnectProc(pDev);
  1203. if( pDev->m_bCommStatus == TRUE )
  1204. {
  1205. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1206. pDev->m_dwOnlineTick = GetTickCount();
  1207. }
  1208. LeaveCriticalSection( &g_csRtuReadOneData );
  1209. }
  1210. }
  1211. }
  1212. else if( pDev->m_bySpecialdevice == 1 )
  1213. {
  1214. if( pDev->m_bCommStatus == FALSE )
  1215. {
  1216. if (!InitAscComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1217. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  1218. {
  1219. pDev->m_bCommStatus = FALSE;
  1220. }
  1221. else
  1222. {
  1223. EnterCriticalSection( &g_csAscReadOneData );
  1224. pDev->m_bCommStatus = ModbusAscReConnectProc(pDev);
  1225. if( pDev->m_bCommStatus == TRUE )
  1226. {
  1227. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1228. pDev->m_dwOnlineTick = GetTickCount();
  1229. }
  1230. LeaveCriticalSection( &g_csAscReadOneData );
  1231. }
  1232. }
  1233. }
  1234. else if( pDev->m_bySpecialdevice == 2 )
  1235. {
  1236. if( pDev->m_bCommStatus == FALSE )
  1237. {
  1238. int iPingResult = 0;
  1239. // ping 10 次,如果都是timeout, 认为设备已断线
  1240. for( int iPingIndex = 0; iPingIndex < 10; iPingIndex++ )
  1241. {
  1242. if( Ping(2000, pDev->m_strIp) == 0 )
  1243. {
  1244. iPingResult = 0;
  1245. break;
  1246. }
  1247. else
  1248. {
  1249. iPingResult = 1;
  1250. }
  1251. }
  1252. if( 1 == iPingResult ) // 设备已断线
  1253. {
  1254. }
  1255. else
  1256. {
  1257. if (!InitTcpNet(pDev->m_iDevideaddr, pDev->m_iIpport, (char *)(LPCTSTR)pDev->m_strIp))
  1258. {
  1259. EnterCriticalSection( &g_csTcpReadOneData );
  1260. pDev->m_bCommStatus = FALSE;
  1261. LeaveCriticalSection( &g_csTcpReadOneData );
  1262. }
  1263. else
  1264. {
  1265. EnterCriticalSection( &g_csTcpReadOneData );
  1266. pDev->m_dwOnlineTick = GetTickCount();
  1267. pDev->m_bCommStatus = TRUE;
  1268. LeaveCriticalSection( &g_csTcpReadOneData );
  1269. int nIndex = GetTcpThreadStructIndex( pDev->m_strUid );
  1270. if( nIndex != -1 )
  1271. {
  1272. if( g_hRequestDataThread[ g_tcpThreadStruct[nIndex].nTcpTheadIndex ] != NULL )
  1273. {
  1274. ResumeThread( g_hRequestDataThread[ g_tcpThreadStruct[nIndex].nTcpTheadIndex ] );
  1275. }
  1276. }
  1277. }
  1278. }
  1279. }
  1280. }
  1281. else if( pDev->m_bySpecialdevice == 3 )
  1282. {
  1283. // 循环所有变量
  1284. if( pDev->m_bCommStatus == FALSE )
  1285. {
  1286. int iPingResult = 0;
  1287. // ping 10 次,如果都是timeout, 认为设备已断线
  1288. for( int iPingIndex = 0; iPingIndex < 10; iPingIndex++ )
  1289. {
  1290. if( Ping(2000, pDev->m_strIp) == 0 )
  1291. {
  1292. iPingResult = 0;
  1293. break;
  1294. }
  1295. else
  1296. {
  1297. iPingResult = 1;
  1298. }
  1299. }
  1300. if( 1 == iPingResult ) // 设备已断线
  1301. {
  1302. }
  1303. else
  1304. {
  1305. EnterCriticalSection( &g_csIcpdasReadOneData );
  1306. pDev->m_dwOnlineTick = GetTickCount();
  1307. pDev->m_bCommStatus = TRUE;
  1308. LeaveCriticalSection( &g_csIcpdasReadOneData );
  1309. }
  1310. }
  1311. }
  1312. else if( pDev->m_bySpecialdevice == 4 )
  1313. {
  1314. if ( strcmp(chDllName, "icpdas.dll") == 0 )
  1315. {
  1316. if( pDev->m_bCommStatus == FALSE )
  1317. {
  1318. if (!InitIcpdasComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1319. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  1320. {
  1321. pDev->m_bCommStatus = FALSE;
  1322. }
  1323. else
  1324. {
  1325. EnterCriticalSection( &g_csIcpdasReadOneData );
  1326. pDev->m_bCommStatus = IcpdasReConnectProc(pDev);
  1327. if( pDev->m_bCommStatus == TRUE )
  1328. {
  1329. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1330. pDev->m_dwOnlineTick = GetTickCount();
  1331. }
  1332. LeaveCriticalSection( &g_csIcpdasReadOneData );
  1333. }
  1334. }
  1335. }
  1336. else if ( strcmp(chDllName, "upsparadigm.dll") == 0 )
  1337. {
  1338. if( pDev->m_bCommStatus == FALSE )
  1339. {
  1340. if (!InitParadigmComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1341. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  1342. {
  1343. pDev->m_bCommStatus = FALSE;
  1344. }
  1345. else
  1346. {
  1347. EnterCriticalSection( &g_csParadigmReadOneData );
  1348. pDev->m_bCommStatus = ParadigmReConnectProc(pDev);
  1349. if( pDev->m_bCommStatus == TRUE )
  1350. {
  1351. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1352. pDev->m_dwOnlineTick = GetTickCount();
  1353. }
  1354. LeaveCriticalSection( &g_csParadigmReadOneData );
  1355. }
  1356. }
  1357. }
  1358. else if ( strcmp(chDllName, "stulz.dll") == 0 )
  1359. {
  1360. if( pDev->m_bCommStatus == FALSE )
  1361. {
  1362. if (!InitStulzComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1363. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  1364. {
  1365. pDev->m_bCommStatus = FALSE;
  1366. }
  1367. else
  1368. {
  1369. EnterCriticalSection( &g_csStulzReadOneData );
  1370. pDev->m_bCommStatus = StulzReConnectProc(pDev);
  1371. if( pDev->m_bCommStatus == TRUE )
  1372. {
  1373. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1374. pDev->m_dwOnlineTick = GetTickCount();
  1375. }
  1376. LeaveCriticalSection( &g_csStulzReadOneData );
  1377. }
  1378. }
  1379. }
  1380. else if ( strcmp(chDllName, "daikin.dll") == 0 )
  1381. {
  1382. if( pDev->m_bCommStatus == FALSE )
  1383. {
  1384. if( !InitDaiKinComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1385. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000) )
  1386. {
  1387. pDev->m_bCommStatus = FALSE;
  1388. }
  1389. else
  1390. {
  1391. EnterCriticalSection( &g_csDaiKinReadOneData );
  1392. pDev->m_bCommStatus = DaiKinReConnectProc(pDev);
  1393. if( pDev->m_bCommStatus == TRUE )
  1394. {
  1395. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1396. pDev->m_dwOnlineTick = GetTickCount();
  1397. }
  1398. LeaveCriticalSection( &g_csDaiKinReadOneData );
  1399. }
  1400. }
  1401. }
  1402. else if ( strcmp(chDllName, "powerware.dll") == 0 )
  1403. {
  1404. if( pDev->m_bCommStatus == FALSE )
  1405. {
  1406. if( !InitPowerwareComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1407. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000) )
  1408. {
  1409. pDev->m_bCommStatus = FALSE;
  1410. }
  1411. else
  1412. {
  1413. EnterCriticalSection( &g_csPowerwareReadOneData );
  1414. pDev->m_bCommStatus = PowerwareReConnectProc(pDev);
  1415. if( pDev->m_bCommStatus == TRUE )
  1416. {
  1417. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1418. pDev->m_dwOnlineTick = GetTickCount();
  1419. }
  1420. LeaveCriticalSection( &g_csPowerwareReadOneData );
  1421. }
  1422. }
  1423. }
  1424. else if ( strcmp(chDllName, "gree.dll") == 0 )
  1425. {
  1426. if (pDev->m_bCommStatus == FALSE)
  1427. {
  1428. if (!InitGreeComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1429. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  1430. {
  1431. pDev->m_bCommStatus = FALSE;
  1432. }
  1433. else
  1434. {
  1435. EnterCriticalSection(&g_csGreeReadOneData);
  1436. pDev->m_bCommStatus = GreeReConnectProc(pDev);
  1437. if( pDev->m_bCommStatus == TRUE )
  1438. {
  1439. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1440. pDev->m_dwOnlineTick = GetTickCount();
  1441. }
  1442. LeaveCriticalSection(&g_csGreeReadOneData);
  1443. }
  1444. }
  1445. }
  1446. else if ( strcmp(chDllName, "carel.dll") == 0 )
  1447. {
  1448. if (pDev->m_bCommStatus == FALSE)
  1449. {
  1450. if (!InitCarelComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1451. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  1452. {
  1453. pDev->m_bCommStatus = FALSE;
  1454. }
  1455. else
  1456. {
  1457. EnterCriticalSection(&g_csCarelReadOneData);
  1458. pDev->m_bCommStatus = CarelReConnectProc(pDev);
  1459. if( pDev->m_bCommStatus == TRUE )
  1460. {
  1461. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1462. pDev->m_dwOnlineTick = GetTickCount();
  1463. }
  1464. LeaveCriticalSection(&g_csCarelReadOneData);
  1465. }
  1466. }
  1467. }
  1468. else if ( strcmp(chDllName, "scantak_3c.dll") == 0 )
  1469. {
  1470. if (pDev->m_bCommStatus == FALSE)
  1471. {
  1472. if (!InitSeparateComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1473. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  1474. {
  1475. pDev->m_bCommStatus = FALSE;
  1476. }
  1477. else
  1478. {
  1479. EnterCriticalSection(&g_csSeparateReadOneData);
  1480. pDev->m_bCommStatus = SeparateReConnectProc(pDev);
  1481. if( pDev->m_bCommStatus == TRUE )
  1482. {
  1483. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1484. pDev->m_dwOnlineTick = GetTickCount();
  1485. }
  1486. LeaveCriticalSection(&g_csSeparateReadOneData);
  1487. }
  1488. }
  1489. }
  1490. else if ( strcmp(chDllName, "delta.dll") == 0 )
  1491. {
  1492. if (pDev->m_bCommStatus == FALSE)
  1493. {
  1494. if (!InitSeparateComm(pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1495. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  1496. {
  1497. pDev->m_bCommStatus = FALSE;
  1498. }
  1499. else
  1500. {
  1501. EnterCriticalSection(&g_csSeparateReadOneData);
  1502. pDev->m_bCommStatus = SeparateReConnectProc(pDev);
  1503. if( pDev->m_bCommStatus == TRUE )
  1504. {
  1505. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1506. pDev->m_dwOnlineTick = GetTickCount();
  1507. }
  1508. LeaveCriticalSection(&g_csSeparateReadOneData);
  1509. }
  1510. }
  1511. }
  1512. else if ( strcmp(szIniName, "upsuh31.ini") == 0 ||
  1513. strcmp(szIniName, "emerson_cm.ini") == 0 ||
  1514. strcmp(szIniName, "dme3000.ini") == 0 ||
  1515. strcmp(szIniName, "paradigm200a_rectifier.ini") == 0 ||
  1516. strcmp(szIniName, "paradigm200a_inverter.ini") == 0 ||
  1517. strcmp(szIniName, "paradigm200b.ini") == 0 ||
  1518. strcmp(szIniName, "hipulseigmnet.ini") == 0 ||
  1519. strcmp(szIniName, "hipulsecdt.ini") == 0 ||
  1520. strcmp(szIniName, "prostar.ini") == 0 ||
  1521. strcmp(szIniName, "joton.ini") == 0 ||
  1522. strcmp(szIniName, "hiross.ini") == 0 ||
  1523. strcmp(szIniName, "upslink.ini") == 0 ||
  1524. strcmp(szIniName, "upsapcsilcon.ini") == 0 ||
  1525. strcmp(szIniName, "ul33.ini") == 0 ||
  1526. strcmp(szIniName, "hipulse_u.ini") == 0 ||
  1527. strcmp(szIniName, "kehua.ini") == 0 ||
  1528. strcmp(szIniName, "secups.ini") == 0 ||
  1529. strcmp(szIniName, "upsuh11.ini") == 0 ||
  1530. strcmp(szIniName, "weibo.ini") == 0 ||
  1531. strcmp(szIniName, "sda_ii.ini") == 0 ||
  1532. strcmp(szIniName, "daikin.ini") == 0 ||
  1533. strcmp(szIniName, "scantak_3c.ini") == 0 ||
  1534. strcmp(szIniName, "delta.ini") == 0 ||
  1535. strcmp(szIniName, "m84c5z.ini") == 0 ||
  1536. strcmp(szIniName, "upsnxr.ini") == 0 ||
  1537. strcmp(szIniName, "batteryadu.ini") == 0 ||
  1538. strcmp(szIniName, "upsmonitorlan.ini") == 0 ||
  1539. strcmp(szIniName, "sk6000.ini") == 0 ||
  1540. strcmp(szIniName, "batteryadu2000.ini") == 0 ||
  1541. strcmp(szIniName, "santak_shiwei.ini") == 0 ||
  1542. strcmp(szIniName, "santak_q4.ini") == 0 ||
  1543. strcmp(szIniName, "ups_nxl.ini") == 0 ||
  1544. strcmp(szIniName, "twoul33.ini") == 0 ||
  1545. strcmp(szIniName, "oumai.ini") == 0 ||
  1546. strcmp(szIniName, "santak_3a3120ks.ini") == 0 ||
  1547. strcmp(szIniName, "tmaster.ini") == 0 ||
  1548. strcmp(szIniName, "st_6c20ks.ini") == 0||
  1549. strcmp(szIniName, "ea800.ini") == 0 ||
  1550. strcmp(szIniName, "jb1501a64p.ini") == 0||
  1551. strcmp(szIniName, "st_3c340ks.ini") == 0||
  1552. strcmp(szIniName, "delta_v1.54.ini") == 0||
  1553. strcmp(szIniName, "icp_i7017.ini") == 0 ||
  1554. strcmp(szIniName, "fad.ini") == 0 ||
  1555. strcmp(szIniName, "itrustadapt.ini" ) == 0 ||
  1556. strcmp(szIniName, "uha3r_0200l.ini") == 0)
  1557. {
  1558. if (pDev->m_bCommStatus == FALSE)
  1559. {
  1560. if (!InitMostDeviceComm(g_strDirectory, szIniName, pDev->m_iDevideaddr, pDev->m_iPort, nBaudRate,
  1561. nDataBit, pDev->m_iStopbit, pDev->m_iParitybit, 1000))
  1562. {
  1563. pDev->m_bCommStatus = FALSE;
  1564. }
  1565. else
  1566. {
  1567. EnterCriticalSection(&g_csMostDeviceReadOneData);
  1568. pDev->m_bCommStatus = MostDeviceReConnectProc(pDev);
  1569. if( pDev->m_bCommStatus == TRUE )
  1570. {
  1571. LOG4C((LOG_NOTICE, "device name:%s open com success!", pDev->m_strDeviceName));
  1572. pDev->m_dwOnlineTick = GetTickCount();
  1573. }
  1574. LeaveCriticalSection(&g_csMostDeviceReadOneData);
  1575. }
  1576. }
  1577. }
  1578. }
  1579. }
  1580. Sleep(100); //chn add
  1581. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  1582. return 0;
  1583. }
  1584. //
  1585. // 设备是否在线判断线程;
  1586. // 1.网络设备通过ping函数来判断是否在线;
  1587. // 2.串口设备通过采集数据的超时值来判断是否在线(每个设备都一个超时变量);
  1588. DWORD WINAPI DevOnlineThreadProc(void *pPara)
  1589. {
  1590. do
  1591. {
  1592. // 1.循环所有设备
  1593. int nDeviceSize = g_pDevicesManager->m_Devices.GetSize();
  1594. for( int i = 0; i < nDeviceSize; i++ )
  1595. {
  1596. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  1597. // 2.循环该设备变量
  1598. int nVarSize = pDev->m_Vars.GetSize();
  1599. for( int j = 0; j < nVarSize; j++ )
  1600. {
  1601. // 3.只判断设备变量中的内存变量(即设备通讯状态变量);
  1602. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1603. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID ) // 内存变量,判断设备是否在线
  1604. {
  1605. // 4.分别对网络和串口设备进行处理,设定条件成立则设备定义为通讯断线;
  1606. if( pDev->m_bySpecialdevice == 2 || pDev->m_bySpecialdevice == 3 ) // 网络设备
  1607. {
  1608. int iPingResult = 0;
  1609. // 4.1 网络设备ping 10 次,如果都是timeout, 认为设备已断线
  1610. for( int iPingIndex = 0; iPingIndex < 10; iPingIndex++ )
  1611. {
  1612. if( Ping(2000, pDev->m_strIp) == 0 )
  1613. {
  1614. iPingResult = 0;
  1615. break;
  1616. }
  1617. else
  1618. {
  1619. iPingResult = 1;
  1620. }
  1621. }
  1622. // 4.2 对网络设备断线和在线处理;
  1623. if( 1 == iPingResult ) // 设备已断线
  1624. {
  1625. if( pDev->m_bySpecialdevice == 2 )
  1626. {
  1627. // 通过uid获取网络设备线程ID索引;
  1628. int nIndex = GetTcpThreadStructIndex( pDev->m_strUid );
  1629. if( nIndex != -1 )
  1630. {
  1631. // 通过已知线程ID索引,挂起线程;
  1632. if( g_hRequestDataThread[ g_tcpThreadStruct[nIndex].nTcpTheadIndex ] != NULL )
  1633. {
  1634. //LOG4C((LOG_NOTICE, "Enter SuspendThread."));
  1635. SuspendThread( g_hRequestDataThread[ g_tcpThreadStruct[nIndex].nTcpTheadIndex ] );
  1636. //LOG4C((LOG_NOTICE, "Leave SuspendThread."));
  1637. }
  1638. }
  1639. // 关闭该线程所使用的网络资源(如:套接字);
  1640. //LOG4C((LOG_NOTICE, "Enter CloseTcpNet."));
  1641. CloseTcpNet(pDev->m_iDevideaddr, pDev->m_iIpport, (char *)(LPCTSTR)pDev->m_strIp);
  1642. //LOG4C((LOG_NOTICE, "Leave CloseTcpNet."));
  1643. }
  1644. // 设备该设备在线变量;
  1645. //if( pDev->m_bCommStatus == TRUE )
  1646. {
  1647. //LOG4C((LOG_NOTICE, "devname = %s ping timeout", pDev->m_strDeviceName));
  1648. EnterCriticalSection( &g_csDevOnlineReadOneData );
  1649. pBaseVar->m_dbData = 1;
  1650. pBaseVar->m_nStatus = 1;
  1651. pDev->m_bCommStatus = FALSE;
  1652. LeaveCriticalSection( &g_csDevOnlineReadOneData );
  1653. }
  1654. }
  1655. else
  1656. {
  1657. EnterCriticalSection( &g_csDevOnlineReadOneData );
  1658. pBaseVar->m_dbData = 0;
  1659. pBaseVar->m_nStatus = 0;
  1660. pDev->m_bCommStatus = TRUE;
  1661. LeaveCriticalSection( &g_csDevOnlineReadOneData );//g_csRtuReadOneData
  1662. }
  1663. }
  1664. else // 串口设备
  1665. {
  1666. if( pDev->m_dwOnlineTick <= 0 )
  1667. {
  1668. pDev->m_dwOnlineTick = GetTickCount();
  1669. }
  1670. // 1.判断变量超时值是否超过设定值,超过则设备判定为掉线;
  1671. if( GetTickCount() - pDev->m_dwOnlineTick > 3 * 60 * 1000 && pDev->m_dwOnlineTick > 0 )
  1672. {
  1673. //LOG4C((LOG_NOTICE, "%s %d, 串口通信故障!", pDev->m_strDeviceName,pDev->m_bySpecialdevice));
  1674. //if( pDev->m_bCommStatus == TRUE )
  1675. {
  1676. EnterCriticalSection( &g_csDevOnlineReadOneData );
  1677. pBaseVar->m_dbData = 1;
  1678. pBaseVar->m_nStatus = 1;
  1679. pDev->m_bCommStatus = FALSE;
  1680. LeaveCriticalSection( &g_csDevOnlineReadOneData );
  1681. }
  1682. }
  1683. else if( GetTickCount() - pDev->m_dwOnlineTick < 3 * 60 * 1000 && pDev->m_dwOnlineTick > 0 )
  1684. {
  1685. //LOG4C((LOG_NOTICE, "%s, 串口通信正常!", pDev->m_strDeviceName));
  1686. EnterCriticalSection( &g_csDevOnlineReadOneData );
  1687. pBaseVar->m_dbData = 0;
  1688. pBaseVar->m_nStatus = 0;
  1689. pDev->m_bCommStatus = TRUE;
  1690. LeaveCriticalSection( &g_csDevOnlineReadOneData );
  1691. }
  1692. }
  1693. break;
  1694. }
  1695. }
  1696. Sleep(10); // 每个设备之间,延时10ms
  1697. }
  1698. }while( WaitForSingleObject( g_hRunObject, 100L ) == WAIT_TIMEOUT );
  1699. return 0;
  1700. }
  1701. //
  1702. // 以下13个动态库采集函数,均由采集线程RequestDataThreadProc调用;
  1703. int ModbusRtuProcessProc(CDevice *pDev)
  1704. {
  1705. char chMsg[80] = {0};
  1706. SETBASEPARAM SetBasePara = {0};
  1707. SetBasePara.nAddr = pDev->m_iDevideaddr;
  1708. // 循环所有变量
  1709. int nVarSize = pDev->m_Vars.GetSize();
  1710. for( int j = 0; j < nVarSize; j++ )
  1711. {
  1712. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1713. if( pBaseVar->m_bReadOnly == 1 ) continue;
  1714. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  1715. // 记住上一次读到的数值
  1716. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  1717. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  1718. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  1719. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  1720. if( pBaseVar->m_nVarTypeID >= SPECIAL_VAR_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= SPECIAL_VAR_TYPE_MAX_ID ) // 特殊变量
  1721. {
  1722. //TRACE("变量值id=%d\r\n", pBaseVar->m_nVarID);
  1723. int nRet = RtuRequestSpecData(SetBasePara, pBaseVar->m_iDataLen, pDev->m_iPort, pDev->m_iDevideaddr,
  1724. (char *)(LPCTSTR)pBaseVar->m_strVarUID, pBaseVar->m_nVarID, pBaseVar->m_nVarTypeID);
  1725. if (nRet == 0)
  1726. {
  1727. }
  1728. else if (nRet == 30005)//Modbus rtu设备lrc校验失败
  1729. {
  1730. // 22:41 2012-6-6 tryto:当采集不到数据时,读取变量的初始值为当前值;
  1731. pBaseVar->m_dbData = pBaseVar->m_nReserved1;
  1732. pDev->m_dwOnlineTick = GetTickCount();
  1733. }
  1734. else
  1735. {
  1736. //Sleep(200);
  1737. // 22:41 2012-6-6 tryto:当采集不到数据时,读取变量的初始值为当前值;
  1738. pBaseVar->m_dbData = pBaseVar->m_nReserved1;
  1739. continue;
  1740. }
  1741. }
  1742. else
  1743. {
  1744. int nRet = pRTU_DLLInit(pDev->m_iPort, pDev->m_iDevideaddr, SetBasePara, pBaseVar->m_iDataLen, chMsg);
  1745. if (nRet == 0)//初始化设备DLL
  1746. {
  1747. RtuSingleResponseData((char *)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, chMsg );
  1748. }
  1749. else if (nRet == 30005)//Modbus rtu设备lrc校验失败
  1750. {
  1751. pDev->m_dwOnlineTick = GetTickCount();
  1752. }
  1753. else
  1754. {
  1755. //LOG4C((LOG_NOTICE, "变量(%s),请求失败,错误代码=%d\n", pBaseVar->m_strDesc, nRet));
  1756. }
  1757. }
  1758. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  1759. {
  1760. }
  1761. else
  1762. {
  1763. pBaseVar->m_bIsChange = TRUE;
  1764. }
  1765. Sleep(1);
  1766. }
  1767. return 0;
  1768. }
  1769. int ModbusAscProcessProc(CDevice *pDev)
  1770. {
  1771. char chMsg[80] = {0};
  1772. SETBASEPARAM SetBasePara = {0};
  1773. SetBasePara.nAddr = pDev->m_iDevideaddr;
  1774. // 循环所有变量
  1775. int nVarSize = pDev->m_Vars.GetSize();
  1776. for( int j = 0; j < nVarSize; j++ )
  1777. {
  1778. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1779. if( pBaseVar->m_bReadOnly == 1 ) continue;
  1780. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  1781. // 记住上一次读到的数值
  1782. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  1783. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  1784. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  1785. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  1786. if( pBaseVar->m_nVarTypeID >= SPECIAL_VAR_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= SPECIAL_VAR_TYPE_MAX_ID ) // 特殊变量
  1787. {
  1788. //TRACE("变量值id=%d\r\n", pBaseVar->m_nVarID);
  1789. int nRet = AscRequestSpecData((char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, pBaseVar->m_nVarTypeID);
  1790. if( nRet==0 )
  1791. {
  1792. }
  1793. else if (nRet == 20005)//Modbus asc设备lrc校验失败
  1794. {
  1795. pDev->m_dwOnlineTick = GetTickCount();
  1796. }
  1797. else
  1798. {
  1799. continue;
  1800. }
  1801. }
  1802. else
  1803. {
  1804. int nRet = pASCDLLInit(pDev->m_iPort, pDev->m_iDevideaddr, SetBasePara, pBaseVar->m_iDataLen, chMsg);//初始化设备DLL
  1805. if( nRet==0 )
  1806. {
  1807. //TRACE("变量值id=%d\r\n", pBaseVar->m_nVarID);
  1808. AscSingleResponseData((char *)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, chMsg);
  1809. }
  1810. else if (nRet == 20005)//Modbus asc设备lrc校验失败
  1811. {
  1812. pDev->m_dwOnlineTick = GetTickCount();
  1813. }
  1814. else if (nRet == 30005)//Modbus rtu设备lrc校验失败
  1815. {
  1816. pDev->m_dwOnlineTick = GetTickCount();
  1817. }
  1818. }
  1819. //if( pBaseVar->m_strName=="Leak2_Alarm" )
  1820. //{
  1821. // CString s;
  1822. // s.Format( "Leak2_Alarm=%d",pBaseVar->m_dbData );
  1823. // LOG4C((LOG_NOTICE, s));
  1824. //}
  1825. //if( pBaseVar->m_strName=="Leak2_Pos" )
  1826. //{
  1827. // CString s;
  1828. // s.Format( "Leak2_Pos=%d",pBaseVar->m_dbData );
  1829. // LOG4C((LOG_NOTICE, s));
  1830. //}
  1831. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  1832. {
  1833. }
  1834. else
  1835. {
  1836. pBaseVar->m_bIsChange = TRUE;
  1837. }
  1838. Sleep(1);
  1839. }
  1840. return 0;
  1841. }
  1842. int ModbusTcpProcessProc(CDevice *pDev)
  1843. {
  1844. char chMsg[80] = {0};
  1845. // 循环所有变量
  1846. int nVarSize = pDev->m_Vars.GetSize();
  1847. for( int j = 0; j < nVarSize; j++ )
  1848. {
  1849. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1850. if( pBaseVar->m_bReadOnly == 1 ) continue;
  1851. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  1852. // 记住上一次读到的数值
  1853. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  1854. if( WaitForSingleObject( g_hRunObject, 5L ) == WAIT_OBJECT_0 )
  1855. {
  1856. MTVERIFY( SetEvent(pDev->m_hSemNet) );
  1857. }
  1858. #if 0
  1859. if( WaitForSingleObject(g_hSemNet, 0) == WAIT_OBJECT_0 ) // 有信号才发送数据
  1860. {
  1861. char chRegNum[4] = {0};
  1862. char chStartAddr[4] = {0};
  1863. char chFuncCode[4] = {0};
  1864. if (TcpRequestData(pDev->m_iIpport,
  1865. pDev->m_iDevideaddr,
  1866. (char *)(LPCTSTR)pDev->m_strIp,
  1867. pBaseVar->m_iRegisterNum,
  1868. pBaseVar->m_nRegStartAddr,
  1869. pBaseVar->m_iFuncID) == 0)
  1870. {
  1871. //TRACE("变量值id=%d\r\n", pBaseVar->m_nVarID);
  1872. int nRet = TcpSingleResponseData((char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID);
  1873. if( nRet == 0 )
  1874. {
  1875. g_nIndex--;
  1876. if( g_nIndex != 0 )
  1877. {
  1878. //TRACE("ModbusTcp数据不同步!\r\n");
  1879. }
  1880. //TRACE(">>>>>>>>>>结束请求一次数据\r\n");
  1881. SetEvent( g_hSemNet );
  1882. //TRACE("变量ID=%d, 请求寄存器地址:%d, 变量值=%.1f\r\n", pReadSnVar->iVarid, atoi(chStartAddr), g_TcpReadOneData.dbData);
  1883. //
  1884. }
  1885. else
  1886. {
  1887. g_nIndex--;
  1888. //TRACE("ModbusTcp响应数据错误!\r\n");
  1889. SetEvent( g_hSemNet );
  1890. }
  1891. }
  1892. else
  1893. {
  1894. //TRACE("没有读到数据\r\n");
  1895. }
  1896. }
  1897. #else
  1898. #if 0
  1899. char chRegNum[4] = {0};
  1900. char chStartAddr[4] = {0};
  1901. char chFuncCode[4] = {0};
  1902. if( WaitForSingleObject(pDev->m_hSemNet, 0) == WAIT_OBJECT_0 ) // 有信号才发送数据
  1903. {
  1904. if (TcpRequestData(pDev->m_iIpport,
  1905. pDev->m_iDevideaddr,
  1906. (char *)(LPCTSTR)pDev->m_strIp,
  1907. pBaseVar->m_iRegisterNum,
  1908. pBaseVar->m_nRegStartAddr,
  1909. pBaseVar->m_iFuncID) == 0)
  1910. {
  1911. int nRet = TcpSingleResponseData((char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID);
  1912. }
  1913. }
  1914. #else
  1915. char chRegNum[4] = {0};
  1916. char chStartAddr[4] = {0};
  1917. char chFuncCode[4] = {0};
  1918. if( WaitForSingleObject(pDev->m_hSemNet, 0) == WAIT_OBJECT_0 ) // 有信号才发送数据
  1919. {
  1920. g_nIndex++;
  1921. ResetEvent( pDev->m_hSemNet );
  1922. if (TcpRequestData(pDev->m_iIpport,
  1923. pDev->m_iDevideaddr,
  1924. (char *)(LPCTSTR)pDev->m_strIp,
  1925. pBaseVar->m_iRegisterNum,
  1926. pBaseVar->m_nRegStartAddr,
  1927. pBaseVar->m_iFuncID) == 0)
  1928. {
  1929. int nRet = TcpSingleResponseData((char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID);
  1930. if( nRet == 0 )
  1931. {
  1932. g_nIndex--;
  1933. if( g_nIndex != 0 )
  1934. {
  1935. //TRACE("ModbusTcp数据不同步!\r\n");
  1936. }
  1937. //TRACE(">>>>>>>>>>结束请求一次数据\r\n");
  1938. SetEvent( pDev->m_hSemNet );
  1939. //TRACE("变量ID=%d, 请求寄存器地址:%d, 变量值=%.1f\r\n", pReadSnVar->iVarid, atoi(chStartAddr), g_TcpReadOneData.dbData);
  1940. //
  1941. }
  1942. else if( nRet == 20405 )
  1943. {
  1944. pDev->m_dwOnlineTick = GetTickCount();
  1945. }
  1946. else
  1947. {
  1948. g_nIndex--;
  1949. //TRACE("ModbusTcp响应数据错误!\r\n");
  1950. SetEvent( pDev->m_hSemNet );
  1951. }
  1952. }
  1953. else
  1954. {
  1955. SetEvent( pDev->m_hSemNet );
  1956. }
  1957. }
  1958. #endif
  1959. #endif
  1960. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  1961. {
  1962. //LOG4C((LOG_NOTICE, "var desc = %s IsChange = false", pBaseVar->m_strDesc));
  1963. }
  1964. else
  1965. {
  1966. //LOG4C((LOG_NOTICE, "var desc = %s IsChange = true", pBaseVar->m_strDesc));
  1967. pBaseVar->m_bIsChange = TRUE;
  1968. }
  1969. Sleep(1);
  1970. }
  1971. return 0;
  1972. }
  1973. int SnmpProcessProc(CDevice *pDev)
  1974. {
  1975. char chMsg[1024] = {0};
  1976. // 循环所有变量
  1977. int nVarSize = pDev->m_Vars.GetSize();
  1978. for( int j = 0; j < nVarSize; j++ )
  1979. {
  1980. CBaseVar *pBaseVar = pDev->m_Vars[j];
  1981. if( pBaseVar->m_bReadOnly == 1 ) continue;
  1982. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  1983. // 记住上一次读到的数值
  1984. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  1985. if( pSnmpDllGetValue )
  1986. {
  1987. //TRACE("变量值id=%d\r\n", pBaseVar->m_nVarID);
  1988. int nRet = pSnmpDllGetValue((char *)(LPCTSTR)pDev->m_strIp,
  1989. (char *)(LPCTSTR)pBaseVar->m_strFields,
  1990. (char *)(LPCTSTR)pBaseVar->m_strSnmpoi,
  1991. chMsg);
  1992. if( nRet == 0 )
  1993. {
  1994. ParseSnmpData(
  1995. (char *)(LPCTSTR)pDev->m_strUid,
  1996. pBaseVar->m_nVarID,
  1997. chMsg);
  1998. }
  1999. }
  2000. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  2001. {
  2002. }
  2003. else
  2004. {
  2005. pBaseVar->m_bIsChange = TRUE;
  2006. }
  2007. Sleep(1);
  2008. }
  2009. return 0;
  2010. }
  2011. int IcpdasProcessProc(CDevice *pDev)
  2012. {
  2013. char chMsg[80] = {0};
  2014. SETBASEPARAM SetBasePara = {0};
  2015. SetBasePara.nAddr = pDev->m_iDevideaddr;
  2016. // 循环所有变量
  2017. int nVarSize = pDev->m_Vars.GetSize();
  2018. for( int j = 0; j < nVarSize; j++ )
  2019. {
  2020. CBaseVar *pBaseVar = pDev->m_Vars[j];
  2021. if( pBaseVar->m_bReadOnly == 1 ) continue;
  2022. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  2023. // 记住上一次读到的数值
  2024. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  2025. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  2026. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  2027. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  2028. int nRet = IcpdasRequestData(j, SetBasePara, pBaseVar->m_iDataLen, pDev->m_iPort, pDev->m_iDevideaddr,
  2029. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char *)(LPCTSTR)pBaseVar->m_strRs232cmd);
  2030. if( nRet==0 )
  2031. {
  2032. }
  2033. else if( nRet == 20805 )
  2034. {
  2035. pDev->m_dwOnlineTick = GetTickCount();
  2036. }
  2037. else
  2038. {
  2039. continue;
  2040. }
  2041. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  2042. {
  2043. }
  2044. else
  2045. {
  2046. pBaseVar->m_bIsChange = TRUE;
  2047. }
  2048. Sleep(1);
  2049. }
  2050. return 0;
  2051. }
  2052. int ParadigmProcessProc(CDevice *pDev)
  2053. {
  2054. char chMsg[80] = {0};
  2055. // 循环所有变量
  2056. int nVarSize = pDev->m_Vars.GetSize();
  2057. for( int j = 0; j < nVarSize; j++ )
  2058. {
  2059. CBaseVar *pBaseVar = pDev->m_Vars[j];
  2060. if( pBaseVar->m_bReadOnly == 1 ) continue;
  2061. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  2062. // 记住上一次读到的数值
  2063. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  2064. int nRet = ParadigmRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  2065. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char*)(LPCTSTR)pBaseVar->m_strRs232cmd);
  2066. if( nRet == 0)
  2067. {
  2068. }
  2069. else
  2070. {
  2071. continue;
  2072. }
  2073. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  2074. {
  2075. }
  2076. else
  2077. {
  2078. pBaseVar->m_bIsChange = TRUE;
  2079. }
  2080. Sleep(1);
  2081. }
  2082. return 0;
  2083. }
  2084. int StulzProcessProc(CDevice *pDev)
  2085. {
  2086. char chMsg[80] = {0};
  2087. SETBASEPARAM SetBasePara = {0};
  2088. SetBasePara.nAddr = pDev->m_iDevideaddr;
  2089. // 循环所有变量
  2090. int nVarSize = pDev->m_Vars.GetSize();
  2091. for( int j = 0; j < nVarSize; j++ )
  2092. {
  2093. CBaseVar *pBaseVar = pDev->m_Vars[j];
  2094. if( pBaseVar->m_bReadOnly == 1 ) continue;
  2095. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  2096. // 记住上一次读到的数值
  2097. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  2098. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  2099. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  2100. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  2101. int nRet = StulzRequestData(SetBasePara, pBaseVar->m_iDataLen, pDev->m_iPort, pDev->m_iDevideaddr,
  2102. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char*)(LPCTSTR)pBaseVar->m_strRs232cmd);
  2103. if( nRet==0 )
  2104. {
  2105. }
  2106. else if( nRet == 20805 )
  2107. {
  2108. pDev->m_dwOnlineTick = GetTickCount();
  2109. }
  2110. else
  2111. {
  2112. continue;
  2113. }
  2114. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  2115. {
  2116. }
  2117. else
  2118. {
  2119. pBaseVar->m_bIsChange = TRUE;
  2120. }
  2121. Sleep(1);
  2122. }
  2123. return 0;
  2124. }
  2125. int DaiKinProcessProc(CDevice *pDev)
  2126. {
  2127. char chMsg[80] = {0};
  2128. // 循环所有变量
  2129. int nVarSize = pDev->m_Vars.GetSize();
  2130. for( int j = 0; j < nVarSize; j++ )
  2131. {
  2132. CBaseVar *pBaseVar = pDev->m_Vars[j];
  2133. if( pBaseVar->m_bReadOnly == 1 ) continue;
  2134. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  2135. // 记住上一次读到的数值
  2136. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  2137. int nRet = DaiKinRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  2138. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char*)(LPCTSTR)pBaseVar->m_strRs232cmd);
  2139. if( nRet == 0)
  2140. {
  2141. }
  2142. else if( nRet == 21405 )
  2143. {
  2144. pDev->m_dwOnlineTick = GetTickCount();
  2145. }
  2146. else
  2147. {
  2148. continue;
  2149. }
  2150. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  2151. {
  2152. }
  2153. else
  2154. {
  2155. pBaseVar->m_bIsChange = TRUE;
  2156. }
  2157. Sleep(1);
  2158. }
  2159. return 0;
  2160. }
  2161. int PowerwareProcessProc(CDevice *pDev)
  2162. {
  2163. char chMsg[80] = {0};
  2164. // 循环所有变量
  2165. int nVarSize = pDev->m_Vars.GetSize();
  2166. for( int j = 0; j < nVarSize; j++ )
  2167. {
  2168. CBaseVar *pBaseVar = pDev->m_Vars[j];
  2169. if( pBaseVar->m_bReadOnly == 1 ) continue;
  2170. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  2171. // 记住上一次读到的数值
  2172. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  2173. int nRet = PowerwareRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  2174. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char*)(LPCTSTR)pBaseVar->m_strRs232cmd);
  2175. if( nRet == 0)
  2176. {
  2177. }
  2178. else if( nRet == 21605 )
  2179. {
  2180. pDev->m_dwOnlineTick = GetTickCount();
  2181. }
  2182. else
  2183. {
  2184. continue;
  2185. }
  2186. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  2187. {
  2188. }
  2189. else
  2190. {
  2191. pBaseVar->m_bIsChange = TRUE;
  2192. }
  2193. Sleep(1);
  2194. }
  2195. return 0;
  2196. }
  2197. int GreeProcessProc(CDevice *pDev)
  2198. {
  2199. char chMsg[80] = {0};
  2200. SETBASEPARAM SetBasePara = {0};
  2201. SetBasePara.nAddr = pDev->m_iDevideaddr;
  2202. // 循环所有变量
  2203. int nVarSize = pDev->m_Vars.GetSize();
  2204. for( int j = 0; j < nVarSize; j++ )
  2205. {
  2206. if( WaitForSingleObject( g_hRunObject, 5L ) == WAIT_OBJECT_0 ) return 0;
  2207. CBaseVar *pBaseVar = pDev->m_Vars[j];
  2208. if( pBaseVar->m_bReadOnly == 1 ) continue;
  2209. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  2210. // 记住上一次读到的数值
  2211. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  2212. SetBasePara.nRegNum = pBaseVar->m_iRegisterNum;
  2213. SetBasePara.nStartAddr = pBaseVar->m_nRegStartAddr;
  2214. SetBasePara.FuncCode = pBaseVar->m_iFuncID;
  2215. int nRet = GreeRequestData(SetBasePara, pBaseVar->m_iDataLen, pDev->m_iPort, pDev->m_iDevideaddr,
  2216. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID, (char*)(LPCTSTR)pBaseVar->m_strRs232cmd);
  2217. if( nRet == 0)
  2218. {
  2219. }
  2220. else if( nRet == 21805 )
  2221. {
  2222. pDev->m_dwOnlineTick = GetTickCount();
  2223. }
  2224. else
  2225. {
  2226. continue;
  2227. }
  2228. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  2229. {
  2230. }
  2231. else
  2232. {
  2233. pBaseVar->m_bIsChange = TRUE;
  2234. }
  2235. Sleep(1);
  2236. }
  2237. return 0;
  2238. }
  2239. int CarelProcessProc(CDevice *pDev)
  2240. {
  2241. char chMsg[80] = {0};
  2242. // 循环所有变量
  2243. int nVarSize = pDev->m_Vars.GetSize();
  2244. for( int j = 0; j < nVarSize; j++ )
  2245. {
  2246. if( WaitForSingleObject( g_hRunObject, 5L ) == WAIT_OBJECT_0 ) return 0;
  2247. CBaseVar *pBaseVar = pDev->m_Vars[j];
  2248. if( pBaseVar->m_bReadOnly == 1 ) continue;
  2249. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  2250. // 记住上一次读到的数值
  2251. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  2252. int nRet = CarelRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  2253. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID,
  2254. (char*)(LPCTSTR)pBaseVar->m_strRs232cmd);
  2255. if( nRet == 0)
  2256. {
  2257. }
  2258. else if( nRet == 22205 )
  2259. {
  2260. pDev->m_dwOnlineTick = GetTickCount();
  2261. }
  2262. else
  2263. {
  2264. continue;
  2265. }
  2266. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  2267. {
  2268. }
  2269. else
  2270. {
  2271. pBaseVar->m_bIsChange = TRUE;
  2272. }
  2273. Sleep(1);
  2274. }
  2275. return 0;
  2276. }
  2277. int SeparateProcessProc(CDevice *pDev)
  2278. {
  2279. char chMsg[80] = {0};
  2280. char szIniName[MAX_PATH] = {0};
  2281. strcpy(szIniName, (char *)(LPCTSTR)pDev->m_strIniName);
  2282. StrLower(szIniName, szIniName);
  2283. // 循环所有变量
  2284. int nVarSize = pDev->m_Vars.GetSize();
  2285. for( int j = 0; j < nVarSize; j++ )
  2286. {
  2287. if( WaitForSingleObject( g_hRunObject, 5L ) == WAIT_OBJECT_0 ) return 0;
  2288. CBaseVar *pBaseVar = pDev->m_Vars[j];
  2289. if( pBaseVar->m_bReadOnly == 1 ) continue;
  2290. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  2291. // 记住上一次读到的数值
  2292. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  2293. int nRet = SeparateRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  2294. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID,
  2295. (char*)(LPCTSTR)pBaseVar->m_strRs232cmd,
  2296. szIniName);
  2297. if( nRet == 0)
  2298. {
  2299. }
  2300. else if( nRet == 50205 )
  2301. {
  2302. pDev->m_dwOnlineTick = GetTickCount();
  2303. }
  2304. else
  2305. {
  2306. continue;
  2307. }
  2308. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  2309. {
  2310. }
  2311. else
  2312. {
  2313. pBaseVar->m_bIsChange = TRUE;
  2314. }
  2315. Sleep(1);
  2316. }
  2317. return 0;
  2318. }
  2319. int MostDeviceProcessProc(CDevice *pDev)
  2320. {
  2321. char chMsg[80] = {0};
  2322. char szIniName[MAX_PATH] = {0};
  2323. strcpy(szIniName, (char *)(LPCTSTR)pDev->m_strIniName);
  2324. StrLower(szIniName, szIniName);
  2325. // 循环所有变量
  2326. int nVarSize = pDev->m_Vars.GetSize();
  2327. for( int j = 0; j < nVarSize; j++ )
  2328. {
  2329. if( WaitForSingleObject( g_hRunObject, 0 ) == WAIT_OBJECT_0 ) return 0;
  2330. CBaseVar *pBaseVar = pDev->m_Vars[j];
  2331. // m_bReadOnly == 0只读; == 1只写 ; == :读写;
  2332. if( pBaseVar->m_bReadOnly == 1 ) continue;
  2333. if( pBaseVar->m_nVarTypeID == ONLINE_VAR_ID || HIGH_PRIORITY_ONLINE_ID == pBaseVar->m_nVarTypeID ) continue; // 内存变量,在DevOnlineThread处理
  2334. // 记住上一次读到的数值
  2335. pBaseVar->m_dbLastData = pBaseVar->m_dbData;
  2336. int nRet = MostDeviceRequestData(pDev->m_iPort, pDev->m_iDevideaddr,
  2337. (char*)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID,
  2338. (char*)(LPCTSTR)pBaseVar->m_strRs232cmd,
  2339. szIniName);
  2340. if( nRet == 0)
  2341. {
  2342. }
  2343. else if( nRet == 50205 )
  2344. {
  2345. pDev->m_dwOnlineTick = GetTickCount();
  2346. }
  2347. else
  2348. {
  2349. Sleep(1);
  2350. continue;
  2351. }
  2352. // abs(int ) :将参数转为绝对值.
  2353. if( abs(pBaseVar->m_dbData - pBaseVar->m_dbLastData) < DATA_CHANGE_FLAG )
  2354. {
  2355. }
  2356. else
  2357. {
  2358. pBaseVar->m_bIsChange = TRUE;
  2359. }
  2360. Sleep(1);
  2361. }
  2362. return 0;
  2363. }
  2364. //
  2365. // 以上13个采集函数均由采集线程RequestDataThreadProc调用;
  2366. // 该线程的参数是外部指定的串口号;
  2367. DWORD WINAPI RequestDataThreadProc(void *pPara)
  2368. {
  2369. #if IS_USER_MUL_THREAD_REQUEST
  2370. CString strName;
  2371. strName = CString((char *)pPara);
  2372. strName.MakeLower();
  2373. #endif
  2374. do
  2375. {
  2376. // 循环所有设备
  2377. int nDeviceSize = g_pDevicesManager->m_Devices.GetSize();
  2378. for( int i = 0; i < nDeviceSize; i++ )
  2379. {
  2380. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  2381. char chDllName[MAX_PATH] = {0};
  2382. strcpy(chDllName, (char *)(LPCTSTR)pDev->m_strProtocoldllname);
  2383. StrLower(chDllName,chDllName);
  2384. char szIniName[MAX_PATH] = {0};
  2385. strcpy(szIniName, (char *)(LPCTSTR)pDev->m_strIniName);
  2386. StrLower(szIniName, szIniName);
  2387. char szUid[MAX_PATH] = {0};
  2388. strcpy(szUid, (char *)(LPCTSTR)pDev->m_strUid);
  2389. StrLower(szUid, szUid);
  2390. char szPort[MAX_PATH] = {0};
  2391. CString strPort;
  2392. strPort.Format("%d", pDev->m_iPort);
  2393. strcpy(szPort, (char *)(LPCTSTR)strPort);
  2394. StrLower(szPort, szPort);
  2395. #if 0
  2396. if( pDev->m_bCommStatus == FALSE )
  2397. {
  2398. Sleep(1);
  2399. continue;
  2400. }
  2401. #endif
  2402. if( pDev->m_bySpecialdevice == 0 )
  2403. {
  2404. #if IS_USER_MUL_THREAD_REQUEST
  2405. if( strName.Compare(szPort) != 0 ) continue;
  2406. #endif
  2407. ModbusRtuProcessProc(pDev);
  2408. }
  2409. else if( pDev->m_bySpecialdevice == 1 )
  2410. {
  2411. #if IS_USER_MUL_THREAD_REQUEST
  2412. if( strName.Compare(szPort) != 0 ) continue;
  2413. #endif
  2414. ModbusAscProcessProc(pDev);
  2415. }
  2416. else if( pDev->m_bySpecialdevice == 2 )
  2417. {
  2418. #if IS_USER_MUL_THREAD_REQUEST
  2419. if( strName.Compare(szUid) ) continue;
  2420. #endif
  2421. ModbusTcpProcessProc(pDev);
  2422. }
  2423. else if( pDev->m_bySpecialdevice == 3 )
  2424. {
  2425. #if IS_USER_MUL_THREAD_REQUEST
  2426. if( strName.Compare(szUid) ) continue;
  2427. #endif
  2428. SnmpProcessProc(pDev);
  2429. }
  2430. else if( pDev->m_bySpecialdevice == 4 )
  2431. {
  2432. if ( strcmp(chDllName, "icpdas.dll") == 0 )
  2433. {
  2434. #if IS_USER_MUL_THREAD_REQUEST
  2435. if( strName.Compare(szPort) != 0 ) continue;
  2436. #endif
  2437. IcpdasProcessProc(pDev);
  2438. }
  2439. else if ( strcmp(chDllName, "upsparadigm.dll") == 0 )
  2440. {
  2441. #if IS_USER_MUL_THREAD_REQUEST
  2442. if( strName.Compare(szPort) != 0 ) continue;
  2443. #endif
  2444. ParadigmProcessProc(pDev);
  2445. }
  2446. else if ( strcmp(chDllName, "stulz.dll") == 0 )
  2447. {
  2448. #if IS_USER_MUL_THREAD_REQUEST
  2449. if( strName.Compare(szPort) != 0 ) continue;
  2450. #endif
  2451. StulzProcessProc(pDev);
  2452. }
  2453. else if ( strcmp(chDllName, "daikin.dll") == 0 )
  2454. {
  2455. #if IS_USER_MUL_THREAD_REQUEST
  2456. if( strName.Compare(szPort) != 0 ) continue;
  2457. #endif
  2458. DaiKinProcessProc(pDev);
  2459. }
  2460. else if ( strcmp(chDllName, "powerware.dll") == 0 )
  2461. {
  2462. #if IS_USER_MUL_THREAD_REQUEST
  2463. if( strName.Compare(szPort) != 0 ) continue;
  2464. #endif
  2465. PowerwareProcessProc(pDev);
  2466. }
  2467. else if ( strcmp(chDllName, "gree.dll") == 0 )
  2468. {
  2469. #if IS_USER_MUL_THREAD_REQUEST
  2470. if( strName.Compare(szPort) != 0 ) continue;
  2471. #endif
  2472. GreeProcessProc(pDev);
  2473. }
  2474. else if ( strcmp(chDllName, "carel.dll") == 0 )
  2475. {
  2476. #if IS_USER_MUL_THREAD_REQUEST
  2477. if( strName.Compare(szPort) != 0 ) continue;
  2478. #endif
  2479. CarelProcessProc(pDev);
  2480. }
  2481. else if ( strcmp(chDllName, "scantak_3c.dll") == 0 )
  2482. {
  2483. #if IS_USER_MUL_THREAD_REQUEST
  2484. if( strName.Compare(szPort) != 0 ) continue;
  2485. #endif
  2486. SeparateProcessProc(pDev);
  2487. }
  2488. else if ( strcmp(chDllName, "delta.dll") == 0 )
  2489. {
  2490. #if IS_USER_MUL_THREAD_REQUEST
  2491. if( strName.Compare(szPort) != 0 ) continue;
  2492. #endif
  2493. //TRACE("变量值id=%d\r\n", pBaseVar->m_nVarID);
  2494. SeparateProcessProc(pDev);
  2495. }
  2496. else if ( strcmp(szIniName, "upsuh31.ini") == 0 ||
  2497. strcmp(szIniName, "emerson_cm.ini") == 0 ||
  2498. strcmp(szIniName, "dme3000.ini") == 0 ||
  2499. strcmp(szIniName, "paradigm200a_rectifier.ini") == 0 ||
  2500. strcmp(szIniName, "paradigm200a_inverter.ini") == 0 ||
  2501. strcmp(szIniName, "paradigm200b.ini") == 0 ||
  2502. strcmp(szIniName, "hipulseigmnet.ini") == 0 ||
  2503. strcmp(szIniName, "hipulsecdt.ini") == 0 ||
  2504. strcmp(szIniName, "prostar.ini") == 0 ||
  2505. strcmp(szIniName, "joton.ini") == 0 ||
  2506. strcmp(szIniName, "hiross.ini") == 0 ||
  2507. strcmp(szIniName, "upslink.ini") == 0 ||
  2508. strcmp(szIniName, "upsapcsilcon.ini") == 0 ||
  2509. strcmp(szIniName, "ul33.ini") == 0 ||
  2510. strcmp(szIniName, "hipulse_u.ini") == 0 ||
  2511. strcmp(szIniName, "kehua.ini") == 0 ||
  2512. strcmp(szIniName, "secups.ini") == 0 ||
  2513. strcmp(szIniName, "upsuh11.ini") == 0 ||
  2514. strcmp(szIniName, "weibo.ini") == 0 ||
  2515. strcmp(szIniName, "sda_ii.ini") == 0 ||
  2516. strcmp(szIniName, "daikin.ini") == 0 ||
  2517. strcmp(szIniName, "scantak_3c.ini") == 0 ||
  2518. strcmp(szIniName, "delta.ini") == 0 ||
  2519. strcmp(szIniName, "m84c5z.ini") == 0 ||
  2520. strcmp(szIniName, "upsnxr.ini") == 0 ||
  2521. strcmp(szIniName, "batteryadu.ini") == 0 ||
  2522. strcmp(szIniName, "upsmonitorlan.ini") == 0 ||
  2523. strcmp(szIniName, "sk6000.ini") == 0 ||
  2524. strcmp(szIniName, "batteryadu2000.ini") == 0 ||
  2525. strcmp(szIniName, "santak_shiwei.ini") == 0 ||
  2526. strcmp(szIniName, "santak_q4.ini") == 0 ||
  2527. strcmp(szIniName, "ups_nxl.ini") == 0 ||
  2528. strcmp(szIniName, "twoul33.ini") == 0 ||
  2529. strcmp(szIniName, "oumai.ini") == 0 ||
  2530. strcmp(szIniName, "santak_3a3120ks.ini") == 0 ||
  2531. strcmp(szIniName, "tmaster.ini") == 0 ||
  2532. strcmp(szIniName, "st_6c20ks.ini") == 0 ||
  2533. strcmp(szIniName, "ea800.ini") == 0 ||
  2534. strcmp(szIniName, "jb1501a64p.ini") == 0||
  2535. strcmp(szIniName, "st_3c340ks.ini") == 0||
  2536. strcmp(szIniName, "delta_v1.54.ini") == 0 ||
  2537. strcmp(szIniName, "icp_i7017.ini") == 0 ||
  2538. strcmp(szIniName, "fad.ini") == 0 ||
  2539. strcmp(szIniName, "itrustadapt.ini" ) == 0 ||
  2540. strcmp(szIniName, "uha3r_0200l.ini") == 0)
  2541. {
  2542. #if IS_USER_MUL_THREAD_REQUEST
  2543. if( strName.Compare(szPort) != 0 ) continue;
  2544. #endif
  2545. //LOG4C((LOG_NOTICE, "Enter MostDeviceProcessProc szIniName = %s", szIniName));
  2546. MostDeviceProcessProc(pDev);
  2547. //LOG4C((LOG_NOTICE, "Leave MostDeviceProcessProc szIniName = %s", szIniName));
  2548. }
  2549. }
  2550. }
  2551. }while( WaitForSingleObject( g_hRunObject, 5L ) == WAIT_TIMEOUT );
  2552. return 0;
  2553. }
  2554. //
  2555. // 定时发送系统运行通知 短信/电话/邮件/传真 线程;
  2556. // 应添加如下功能:
  2557. // 1.选择按天发送系统信息;
  2558. // 2.选择按星期发送系统信息;
  2559. // 3.选择按月发送系统信息;
  2560. DWORD WINAPI TimingSendSmsThread(void *pPara)
  2561. {
  2562. char chTel[MAX_TEL_LENGTH] = {0}; // 电话;
  2563. char chMobile[MAX_TEL_LENGTH] = {0}; // 手机;
  2564. char chFax[MAX_TEL_LENGTH] = {0}; // 传真;
  2565. char chEmail[MAX_EMAIL_LENGTH] = {0}; // 邮件;
  2566. // 创建容器(最后使用完要释放内存空间);
  2567. list<string> list_tel, list_mobile, list_fax, list_email;
  2568. list<string>::iterator it_tel, it_mobile, it_fax, it_email;
  2569. // 从数据库中获取用户信息;
  2570. int nUserCount = CDBInterface::GetInstancePtr()->GetUserInfoList(list_tel, list_mobile, list_fax, list_email);
  2571. // 无用户,返回;
  2572. if( 0 == nUserCount )
  2573. return 0;
  2574. // 有用户,但用户无电话等信息,返回;
  2575. if( 0 == list_tel.size() )
  2576. return 0;
  2577. do
  2578. {
  2579. // 时刻获取当前时间,对比已设定好的时间;
  2580. CTime ctNow = CTime::GetCurrentTime();
  2581. int nWeek, nHour, nMinute;
  2582. nWeek = ctNow.GetDayOfWeek();
  2583. nHour = ctNow.GetHour();
  2584. nMinute = ctNow.GetMinute();
  2585. #if 0 //每天发一次
  2586. if( nHour == g_nTimingHours && nMinute == g_nTimingMinutes && g_nTimingEnable == 1 && g_nTimingSendFinished == 0 )
  2587. {
  2588. g_nTimingSendFinished = 1;
  2589. char szResultMsg[MAX_EQUIP_DESC + 1] = {0};
  2590. int nResult = -1;
  2591. memset(szResultMsg, 0, sizeof(szResultMsg));
  2592. for( it_tel = list_tel.begin(), it_mobile = list_mobile.begin(), it_fax = list_fax.begin(), it_email = list_email.begin();
  2593. it_tel != list_tel.end(), it_mobile != list_mobile.end(), it_fax != list_fax.end(), it_email != list_email.end(); )
  2594. {
  2595. strcpy(chTel, (*it_tel++).c_str());
  2596. strcpy(chMobile, (*it_mobile++).c_str());
  2597. strcpy(chFax, (*it_fax++).c_str());
  2598. strcpy(chEmail, (*it_email++).c_str());
  2599. // 是否启用短信模块功能;
  2600. if( g_nSendSms==1 ) // g_nSendSms;
  2601. {
  2602. if( strcmp(chMobile, "") != 0 )
  2603. {
  2604. CString strMobileTel;
  2605. strMobileTel.Format("%s",chMobile );
  2606. if( strMobileTel.GetLength()!=11 ) continue; //判断是不是手机号码 只根据长度
  2607. if (pSmsDllSendSms && g_bSuccesOpen)
  2608. {
  2609. nResult = pSmsDllSendSms(chMobile, (char *)(LPCTSTR)g_strHintSysRun, szResultMsg);
  2610. }
  2611. if( nResult != 0 ) // 发送失败
  2612. {
  2613. }
  2614. }
  2615. }
  2616. // 是否启用语音模块功能;
  2617. if( g_nSendNotice == 1 )
  2618. {
  2619. //LOG4C((LOG_NOTICE, "nWeek = %d, nHour = %d, nMinute = %d", nWeek, nHour, nMinute));
  2620. if( strcmp(chTel, "") != 0 )
  2621. {
  2622. LOG4C((LOG_NOTICE, "nWeek = %d, nHour = %d, nMinute = %d, g_strHintSysRun = %s", nWeek, nHour, nMinute, g_strHintSysRun));
  2623. // 继续加入语音发送list
  2624. EnterCriticalSection( &g_csVarNotice );
  2625. InsertSendNoticeList("0.0.0.0", 1, chTel, (char *)(LPCTSTR)g_strHintSysRun);
  2626. LeaveCriticalSection( &g_csVarNotice );
  2627. }
  2628. #if 0 //移动电话 不打电话
  2629. if( strcmp(chMobile, "") != 0 )
  2630. {
  2631. //LOG4C((LOG_NOTICE, "nWeek = %d, nHour = %d, nMinute = %d, g_strHintSysRun = %s", nWeek, nHour, nMinute, g_strHintSysRun));
  2632. // 继续加入语音发送list
  2633. EnterCriticalSection( &g_csVarNotice );
  2634. InsertSendNoticeList("0.0.0.0", 1, chMobile, (char *)(LPCTSTR)g_strHintSysRun);
  2635. LeaveCriticalSection( &g_csVarNotice );
  2636. }
  2637. #endif
  2638. }
  2639. }
  2640. }
  2641. else
  2642. {
  2643. if( !( nHour == g_nTimingHours && nMinute == g_nTimingMinutes ) )
  2644. {
  2645. g_nTimingSendFinished = 0;
  2646. }
  2647. }
  2648. #else //每星期发一次
  2649. if( nWeek == g_nTimingWeeks && nHour == g_nTimingHours && nMinute == g_nTimingMinutes && g_nTimingEnable == 1 && g_nTimingSendFinished == 0 )
  2650. {
  2651. g_nTimingSendFinished = 1;
  2652. char szResultMsg[MAX_EQUIP_DESC + 1] = {0};
  2653. int nResult = -1;
  2654. memset(szResultMsg, 0, sizeof(szResultMsg));
  2655. for( it_tel = list_tel.begin(), it_mobile = list_mobile.begin(), it_fax = list_fax.begin(), it_email = list_email.begin();
  2656. it_tel != list_tel.end(), it_mobile != list_mobile.end(), it_fax != list_fax.end(), it_email != list_email.end(); )
  2657. {
  2658. strcpy(chTel, (*it_tel++).c_str());
  2659. strcpy(chMobile, (*it_mobile++).c_str());
  2660. strcpy(chFax, (*it_fax++).c_str());
  2661. strcpy(chEmail, (*it_email++).c_str());
  2662. // 发送短信;
  2663. if( g_nSendSms==1 )
  2664. {
  2665. if( strcmp(chMobile, "") != 0 )
  2666. {
  2667. CString strMobileTel;
  2668. strMobileTel.Format("%s",chMobile );
  2669. if( strMobileTel.GetLength()!=11 ) continue; //判断是不是手机号码 只根据长度
  2670. if (pSmsDllSendSms && g_bSuccesOpen)
  2671. {
  2672. nResult = pSmsDllSendSms(chMobile, (char *)(LPCTSTR)g_strHintSysRun, szResultMsg);
  2673. }
  2674. if( nResult != 0 ) // 发送失败
  2675. {
  2676. }
  2677. }
  2678. }
  2679. // 发送电话语音;
  2680. if( g_nSendNotice == 1 )
  2681. {
  2682. //LOG4C((LOG_NOTICE, "nWeek = %d, nHour = %d, nMinute = %d", nWeek, nHour, nMinute));
  2683. if( strcmp(chTel, "") != 0 )
  2684. {
  2685. LOG4C((LOG_NOTICE, "nWeek = %d, nHour = %d, nMinute = %d, g_strHintSysRun = %s", nWeek, nHour, nMinute, g_strHintSysRun));
  2686. // 继续加入语音发送list
  2687. EnterCriticalSection( &g_csVarNotice );
  2688. InsertSendNoticeList("0.0.0.0", 1, chTel, (char *)(LPCTSTR)g_strHintSysRun,0);
  2689. LeaveCriticalSection( &g_csVarNotice );
  2690. }
  2691. #if 0 //移动电话 不打电话
  2692. if( strcmp(chMobile, "") != 0 )
  2693. {
  2694. //LOG4C((LOG_NOTICE, "nWeek = %d, nHour = %d, nMinute = %d, g_strHintSysRun = %s", nWeek, nHour, nMinute, g_strHintSysRun));
  2695. // 继续加入语音发送list
  2696. EnterCriticalSection( &g_csVarNotice );
  2697. InsertSendNoticeList("0.0.0.0", 1, chMobile, (char *)(LPCTSTR)g_strHintSysRun);
  2698. LeaveCriticalSection( &g_csVarNotice );
  2699. }
  2700. #endif
  2701. }
  2702. }
  2703. }
  2704. else
  2705. {
  2706. if( !( nWeek == g_nTimingWeeks && nHour == g_nTimingHours && nMinute == g_nTimingMinutes ) )
  2707. {
  2708. g_nTimingSendFinished = 0;
  2709. }
  2710. }
  2711. #endif
  2712. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  2713. // 作内存释放;
  2714. for( it_tel = list_tel.begin(), it_mobile = list_mobile.begin(), it_fax = list_fax.begin(), it_email = list_email.begin();
  2715. it_tel != list_tel.end(), it_mobile != list_mobile.end(), it_fax != list_fax.end(), it_email != list_email.end(); )
  2716. {
  2717. list_tel.erase(it_tel++);
  2718. list_mobile.erase(it_mobile++);
  2719. list_fax.erase(it_fax++);
  2720. list_email.erase(it_email++);
  2721. }
  2722. return 0;
  2723. }
  2724. //
  2725. // 语音报警线程;
  2726. // 该线程功能如下:
  2727. // 1.判断变量值是否改变,改变则将变量发送到所有客户端,并写入数据库sumlog表里,以及写入txt日志文件中;
  2728. // 2.判断变量采集是否超时,如果超时,写入变量值到数据库sumlog表里,并写入txt日志文件中;
  2729. // 3.判断变量是否有报警选项,有投递到函数VarAlarmNotice进行报警处理过滤;
  2730. DWORD WINAPI VoiceNoticeThreadProc(void *pPara)
  2731. {
  2732. do
  2733. {
  2734. // 循环所有设备
  2735. int nDeviceSize = g_pDevicesManager->m_Devices.GetSize();
  2736. for( int i = 0; i < nDeviceSize; i++ )
  2737. {
  2738. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  2739. char chDllName[MAX_PATH] = {0};
  2740. strcpy(chDllName, (char *)(LPCTSTR)pDev->m_strProtocoldllname);
  2741. StrLower(chDllName,chDllName);
  2742. char szIniName[MAX_PATH] = {0};
  2743. strcpy(szIniName, (char *)(LPCTSTR)pDev->m_strIniName);
  2744. StrLower(szIniName, szIniName);
  2745. char szUid[MAX_PATH] = {0};
  2746. strcpy(szUid, (char *)(LPCTSTR)pDev->m_strUid);
  2747. StrLower(szUid, szUid);
  2748. char szPort[MAX_PATH] = {0};
  2749. CString strPort;
  2750. strPort.Format("%d", pDev->m_iPort);
  2751. strcpy(szPort, (char *)(LPCTSTR)strPort);
  2752. StrLower(szPort, szPort);
  2753. // 循环所有变量
  2754. int nVarSize = pDev->m_Vars.GetSize();
  2755. for( int j = 0; j < nVarSize; j++ )
  2756. {
  2757. CBaseVar *pBaseVar = pDev->m_Vars[j];
  2758. CString strTime;
  2759. CTime tm;
  2760. tm = CTime::GetCurrentTime();
  2761. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  2762. int iIndex = 0;
  2763. // 变量的值是否在正常范围
  2764. if( pBaseVar->m_nVarTypeID != SNMP_NPM_TYPE && pBaseVar->m_nVarTypeID != SNMP_STRING_ID )
  2765. {
  2766. if( (int)pBaseVar->m_dbData < pBaseVar->m_nReserved2 || (int)pBaseVar->m_dbData > pBaseVar->m_nReserved3 )
  2767. {
  2768. Sleep(1);
  2769. continue;
  2770. }
  2771. }
  2772. #if 0
  2773. if( 1 == g_nSnmpEnable )
  2774. {
  2775. if( 0 == pBaseVar->m_dwCollectTick )
  2776. {
  2777. CString strSetValue;
  2778. strSetValue.Format("%.1f", pBaseVar->m_dbData);
  2779. CString strObjID;
  2780. strObjID.Format("%s.%d", g_strSnmpObjOid, pBaseVar->m_nVarID);
  2781. pSnmpDllSetValue(
  2782. g_strSnmpIP,
  2783. g_strSnmpField,
  2784. (char *)(LPCTSTR)strObjID,
  2785. "str",
  2786. 1,
  2787. 300,
  2788. (char *)(LPCTSTR)strSetValue
  2789. );
  2790. }
  2791. }
  2792. #endif
  2793. DWORD iTickCount = GetTickCount();
  2794. // 变量值改变;
  2795. if( TRUE == pBaseVar->m_bIsChange )
  2796. {
  2797. // 发送值改变了的变量到所有客户端里?
  2798. g_pServerSocket[1]->AutoSendDataToAllClient( pBaseVar );
  2799. // 变量实时采集,则写入数据库summlog里;
  2800. if( TRUE == pBaseVar->m_bRealTimeflag )
  2801. {
  2802. // 写数据库数据日志表;
  2803. CDBInterface::GetInstancePtr()->EditSummaryRecord(
  2804. (char *)(LPCTSTR)pDev->m_strDeviceName, // 设备名称
  2805. (char *)(LPCTSTR)strTime, // 发生时间
  2806. "", // 备注
  2807. (char *)(LPCTSTR)pBaseVar->m_strDesc, // 变量描述
  2808. pBaseVar->m_dbData, // 变量值
  2809. (char *)(LPCTSTR)pDev->m_strUid, // 设备Uid
  2810. (char *)(LPCTSTR)pBaseVar->m_strName); // 变量名称
  2811. EnterCriticalSection( &g_csRtuReadOneData );
  2812. pBaseVar->m_bIsChange = FALSE;
  2813. LeaveCriticalSection( &g_csRtuReadOneData );
  2814. }
  2815. // 变量实时保存,则保存日志到log.txt文件中;
  2816. if( TRUE == pBaseVar->m_bIsSave )
  2817. {
  2818. WriteSummaryLog(
  2819. (char *)(LPCTSTR)pDev->m_strDeviceName, // 设备名称
  2820. (char *)(LPCTSTR)strTime, // 发生时间
  2821. "", // 备注
  2822. 0, // 现没有用到
  2823. (char *)(LPCTSTR)pBaseVar->m_strDesc, // 变量描述
  2824. pBaseVar->m_dbData, // 变量值
  2825. (char *)(LPCTSTR)pDev->m_strUid, // 设备Uid
  2826. (char *)(LPCTSTR)pBaseVar->m_strName); // 变量名称
  2827. }
  2828. }
  2829. // 变量值没有改变,假如超过了采集频率,且变量有保存选项,同样上一步操作;
  2830. else if ((( iTickCount - pBaseVar->m_dwCollectTick) > pBaseVar->m_nCollectFreq * 1000) && pBaseVar->m_bIsSave )
  2831. {
  2832. // 是否实时采集;
  2833. if( TRUE == pBaseVar->m_bRealTimeflag )
  2834. {
  2835. CDBInterface::GetInstancePtr()->EditSummaryRecord(
  2836. (char *)(LPCTSTR)pDev->m_strDeviceName, // 设备名称
  2837. (char *)(LPCTSTR)strTime, // 发生时间
  2838. "", // 备注
  2839. (char *)(LPCTSTR)pBaseVar->m_strDesc, // 变量描述
  2840. pBaseVar->m_dbData, // 变量值
  2841. (char *)(LPCTSTR)pDev->m_strUid, // 设备Uid
  2842. (char *)(LPCTSTR)pBaseVar->m_strName); // 变量名称
  2843. }
  2844. // 是否保存;
  2845. if( TRUE == pBaseVar->m_bIsSave )
  2846. {
  2847. WriteSummaryLog(
  2848. (char *)(LPCTSTR)pDev->m_strDeviceName, // 设备名称
  2849. (char *)(LPCTSTR)strTime, // 发生时间
  2850. "", // 备注
  2851. 0, // 现没有用到
  2852. (char *)(LPCTSTR)pBaseVar->m_strDesc, // 变量描述
  2853. pBaseVar->m_dbData, // 变量值
  2854. (char *)(LPCTSTR)pDev->m_strUid, // 设备Uid
  2855. (char *)(LPCTSTR)pBaseVar->m_strName); // 变量名称
  2856. EnterCriticalSection(&g_csRtuReadOneData);
  2857. pBaseVar->m_dwCollectTick = GetTickCount();
  2858. LeaveCriticalSection(&g_csRtuReadOneData);
  2859. }
  2860. }
  2861. // m_bReserved2 预留2 是否触发远程报警
  2862. // 是否触发远程报警(在函数VarAlarmNotice进行报警内容组合);
  2863. if( pBaseVar->m_bReserved2 == TRUE )
  2864. {
  2865. VarAlarmNotice(pDev, pBaseVar);
  2866. }
  2867. }
  2868. }
  2869. }while( WaitForSingleObject( g_hRunObject, 5L ) == WAIT_TIMEOUT );
  2870. return 0;
  2871. }
  2872. //
  2873. // 变量输出 == 专为上海烟草局设计 ????
  2874. DWORD WINAPI OutputThreadProc(void *pPara)
  2875. {
  2876. CString strClock, strTh;
  2877. strClock.Format("%s", g_strOutputClock);
  2878. strTh.Format("%s", g_strOutputFileName);
  2879. //LOG4C((LOG_NOTICE, "%s", strClock));
  2880. //LOG4C((LOG_NOTICE, "%s", strTh));
  2881. CStdioFile fClock;
  2882. CStdioFile fTh;
  2883. bool bfClockResult = false;
  2884. bool bfThResult = false;
  2885. bfClockResult = fClock.Open(strClock, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite);
  2886. bfThResult = fTh.Open(strTh, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite);
  2887. do
  2888. {
  2889. CString strTime;
  2890. CTime tm;
  2891. tm = CTime::GetCurrentTime();
  2892. strTime = tm.Format("%H:%M");
  2893. strTime += "\n";
  2894. if( bfClockResult )
  2895. {
  2896. //fClock.SetLength(0);
  2897. fClock.SeekToBegin();
  2898. fClock.Write(strTime.GetBuffer(), strTime.GetLength());
  2899. }
  2900. if( false == bfThResult )
  2901. {
  2902. Sleep(10);
  2903. continue;
  2904. }
  2905. int iDeviceIndex = -1, iVarIndex = -1;
  2906. char szVarName[MAX_UID + 1] = {0};
  2907. int iSeekPos = 0;
  2908. fTh.SeekToBegin();
  2909. for( int iLineIndex1 = 0; iLineIndex1 < g_nOutputLineCount1; iLineIndex1++ )
  2910. {
  2911. // 获取输出行里的变量名;
  2912. SplitStr(g_strOutputLine1, ',', iLineIndex1, szVarName);
  2913. // 根据变量名,uid查找变量;
  2914. BOOL bFind = FindVar( g_strOutputLineUid1, szVarName, iDeviceIndex, iVarIndex );
  2915. if( bFind )
  2916. {
  2917. CDevice *pDev = g_pDevicesManager->m_Devices[iDeviceIndex];
  2918. CBaseVar *pBaseVar = pDev->m_Vars[iVarIndex];
  2919. CString strVarData;
  2920. if( 0 == iLineIndex1 )
  2921. {
  2922. strVarData.Format("%.1f ", pBaseVar->m_dbData);
  2923. fTh.Write(strVarData, strVarData.GetLength());
  2924. }
  2925. else if( 1 == iLineIndex1 )
  2926. {
  2927. strVarData.Format("%.1f%s \r\n", pBaseVar->m_dbData, pBaseVar->m_strUnit);
  2928. fTh.Write(strVarData, strVarData.GetLength());
  2929. }
  2930. iSeekPos += strVarData.GetLength();
  2931. }
  2932. }
  2933. for( int iLineIndex2 = 0; iLineIndex2 < g_nOutputLineCount2; iLineIndex2++ )
  2934. {
  2935. SplitStr(g_strOutputLine2, ',', iLineIndex2, szVarName);
  2936. BOOL bFind = FindVar( g_strOutputLineUid2, szVarName, iDeviceIndex, iVarIndex );
  2937. if( bFind )
  2938. {
  2939. CDevice *pDev = g_pDevicesManager->m_Devices[iDeviceIndex];
  2940. CBaseVar *pBaseVar = pDev->m_Vars[iVarIndex];
  2941. CString strVarData;
  2942. if( 0 == iLineIndex2 )
  2943. {
  2944. strVarData.Format("%.1f ", pBaseVar->m_dbData);
  2945. fTh.Write(strVarData, strVarData.GetLength());
  2946. }
  2947. else if( 1 == iLineIndex2 )
  2948. {
  2949. strVarData.Format("%.1f%s \r\n", pBaseVar->m_dbData, pBaseVar->m_strUnit);
  2950. fTh.Write(strVarData, strVarData.GetLength());
  2951. }
  2952. iSeekPos += strVarData.GetLength();
  2953. }
  2954. }
  2955. for( int iLineIndex3 = 0; iLineIndex3 < g_nOutputLineCount3; iLineIndex3++ )
  2956. {
  2957. SplitStr(g_strOutputLine3, ',', iLineIndex3, szVarName);
  2958. BOOL bFind = FindVar( g_strOutputLineUid3, szVarName, iDeviceIndex, iVarIndex );
  2959. if( bFind )
  2960. {
  2961. CDevice *pDev = g_pDevicesManager->m_Devices[iDeviceIndex];
  2962. CBaseVar *pBaseVar = pDev->m_Vars[iVarIndex];
  2963. CString strVarData;
  2964. if( 0 == iLineIndex3 )
  2965. {
  2966. strVarData.Format("%.1f ", pBaseVar->m_dbData);
  2967. fTh.Write(strVarData, strVarData.GetLength());
  2968. }
  2969. else if( 1 == iLineIndex3 )
  2970. {
  2971. strVarData.Format("%.1f%s \r\n", pBaseVar->m_dbData, pBaseVar->m_strUnit);
  2972. fTh.Write(strVarData, strVarData.GetLength());
  2973. }
  2974. iSeekPos += strVarData.GetLength();
  2975. }
  2976. }
  2977. }while( WaitForSingleObject( g_hRunObject, 100L ) == WAIT_TIMEOUT );
  2978. // 内存释放;
  2979. if( bfClockResult )
  2980. {
  2981. fClock.Close();
  2982. }
  2983. // 内存释放;
  2984. if( bfThResult )
  2985. {
  2986. fTh.Close();
  2987. }
  2988. return 0;
  2989. }
  2990. //
  2991. // 变量时间计划线程;
  2992. DWORD WINAPI GetDatePlanThreadProc(void *pPara)
  2993. {
  2994. do
  2995. {
  2996. static int nStaticHours = -1;
  2997. CTime time = CTime::GetCurrentTime();
  2998. int nHours = time.GetHour();
  2999. if( nStaticHours!=nHours )
  3000. {
  3001. LOG4C((LOG_NOTICE, _T("GetDatePlanThreadProc")));
  3002. nStaticHours = nHours;
  3003. // 循环所有设备
  3004. int nDeviceSize = g_pDevicesManager->m_Devices.GetSize();
  3005. for( int i = 0; i < nDeviceSize; i++ )
  3006. {
  3007. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  3008. // 循环所有变量
  3009. int nVarSize = pDev->m_Vars.GetSize();
  3010. for( int j = 0; j < nVarSize; j++ )
  3011. {
  3012. CBaseVar *pBaseVar = pDev->m_Vars[j];
  3013. EnterCriticalSection(&g_csRtuReadOneData);
  3014. // 从数据库中获取时间计划???
  3015. pBaseVar->m_bExistDatePlan = CDBInterface::GetInstancePtr()->IsExistDatePlan((char *)(LPCTSTR)pDev->m_strUid, pBaseVar->m_nVarID);
  3016. LeaveCriticalSection(&g_csRtuReadOneData);
  3017. }
  3018. }
  3019. }
  3020. }while( WaitForSingleObject( g_hRunObject, 10 * 1000L ) == WAIT_TIMEOUT );
  3021. return 0;
  3022. }
  3023. //
  3024. // 以下13个查找 串口是否使用 函数,均由主函数StartMain调用;
  3025. BOOL FindRtuCommPort(CString strPort)
  3026. {
  3027. BOOL bResult = FALSE;
  3028. for( int i = RTU_THREAD_START; i < g_nRtuThreadIndex; i++ )
  3029. {
  3030. if( g_strPort[i].Compare(strPort) == 0 )
  3031. {
  3032. bResult = TRUE;
  3033. break;
  3034. }
  3035. }
  3036. return bResult;
  3037. }
  3038. BOOL FindAscCommPort(CString strPort)
  3039. {
  3040. BOOL bResult = FALSE;
  3041. for( int i = ASC_THREAD_START; i < g_nAscThreadIndex; i++ )
  3042. {
  3043. if( g_strPort[i].Compare(strPort) == 0 )
  3044. {
  3045. bResult = TRUE;
  3046. break;
  3047. }
  3048. }
  3049. return bResult;
  3050. }
  3051. BOOL FindTcpCommPort(CString strPort)
  3052. {
  3053. BOOL bResult = FALSE;
  3054. for( int i = TCP_THREAD_START; i < g_nTcpThreadIndex; i++ )
  3055. {
  3056. if( g_strPort[i].Compare(strPort) == 0 )
  3057. {
  3058. bResult = TRUE;
  3059. break;
  3060. }
  3061. }
  3062. return bResult;
  3063. }
  3064. BOOL FindMostDeviceCommPort(CString strPort)
  3065. {
  3066. BOOL bResult = FALSE;
  3067. for( int i = MOSTDEVICE_THREAD_START; i < g_nMostDeviceIndex; i++ )
  3068. {
  3069. if( g_strPort[i].Compare(strPort) == 0 )
  3070. {
  3071. bResult = TRUE;
  3072. break;
  3073. }
  3074. }
  3075. return bResult;
  3076. }
  3077. BOOL FindIcpdasCommPort(CString strPort)
  3078. {
  3079. BOOL bResult = FALSE;
  3080. for( int i = ICPDAS_THREAD_START; i < g_nIcpdasIndex; i++ )
  3081. {
  3082. if( g_strPort[i].Compare(strPort) == 0 )
  3083. {
  3084. bResult = TRUE;
  3085. break;
  3086. }
  3087. }
  3088. return bResult;
  3089. }
  3090. BOOL FindParadigmCommPort(CString strPort)
  3091. {
  3092. BOOL bResult = FALSE;
  3093. for( int i = PARADIGM_THREAD_START; i < g_nParadigmIndex; i++ )
  3094. {
  3095. if( g_strPort[i].Compare(strPort) == 0 )
  3096. {
  3097. bResult = TRUE;
  3098. break;
  3099. }
  3100. }
  3101. return bResult;
  3102. }
  3103. BOOL FindStulzCommPort(CString strPort)
  3104. {
  3105. BOOL bResult = FALSE;
  3106. for( int i = STULZ_THREAD_START; i < g_nStulzIndex; i++ )
  3107. {
  3108. if( g_strPort[i].Compare(strPort) == 0 )
  3109. {
  3110. bResult = TRUE;
  3111. break;
  3112. }
  3113. }
  3114. return bResult;
  3115. }
  3116. BOOL FindDaiKinCommPort(CString strPort)
  3117. {
  3118. BOOL bResult = FALSE;
  3119. for( int i = DAIKIN_THREAD_START; i < g_nDaiKinIndex; i++ )
  3120. {
  3121. if( g_strPort[i].Compare(strPort) == 0 )
  3122. {
  3123. bResult = TRUE;
  3124. break;
  3125. }
  3126. }
  3127. return bResult;
  3128. }
  3129. BOOL FindPowerwareCommPort(CString strPort)
  3130. {
  3131. BOOL bResult = FALSE;
  3132. for( int i = POWERWARE_THREAD_START; i < g_nPowerwareIndex; i++ )
  3133. {
  3134. if( g_strPort[i].Compare(strPort) == 0 )
  3135. {
  3136. bResult = TRUE;
  3137. break;
  3138. }
  3139. }
  3140. return bResult;
  3141. }
  3142. BOOL FindGreeCommPort(CString strPort)
  3143. {
  3144. BOOL bResult = FALSE;
  3145. for( int i = GREE_THREAD_START; i < g_nGreeIndex; i++ )
  3146. {
  3147. if( g_strPort[i].Compare(strPort) == 0 )
  3148. {
  3149. bResult = TRUE;
  3150. break;
  3151. }
  3152. }
  3153. return bResult;
  3154. }
  3155. BOOL FindCarelCommPort(CString strPort)
  3156. {
  3157. BOOL bResult = FALSE;
  3158. for( int i = CAREL_THREAD_START; i < g_nCarelIndex; i++ )
  3159. {
  3160. if( g_strPort[i].Compare(strPort) == 0 )
  3161. {
  3162. bResult = TRUE;
  3163. break;
  3164. }
  3165. }
  3166. return bResult;
  3167. }
  3168. BOOL FindScanTakCommPort(CString strPort)
  3169. {
  3170. BOOL bResult = FALSE;
  3171. for( int i = SCANTAK_THREAD_START; i < g_nScantakIndex; i++ )
  3172. {
  3173. if( g_strPort[i].Compare(strPort) == 0 )
  3174. {
  3175. bResult = TRUE;
  3176. break;
  3177. }
  3178. }
  3179. return bResult;
  3180. }
  3181. BOOL FindDeltaCommPort(CString strPort)
  3182. {
  3183. BOOL bResult = FALSE;
  3184. for( int i = DELTA_THREAD_START; i < g_nDeltaIndex; i++ )
  3185. {
  3186. if( g_strPort[i].Compare(strPort) == 0 )
  3187. {
  3188. bResult = TRUE;
  3189. break;
  3190. }
  3191. }
  3192. return bResult;
  3193. }
  3194. // 获取实际波特率
  3195. INT GetCommRate(int nRate)
  3196. {
  3197. int nBaudRate = 9600;
  3198. switch( nRate )
  3199. {
  3200. case 0:
  3201. nBaudRate = 1200;
  3202. break;
  3203. case 1:
  3204. nBaudRate = 2400;
  3205. break;
  3206. case 2:
  3207. nBaudRate = 4800;
  3208. break;
  3209. case 3:
  3210. nBaudRate = 9600;
  3211. break;
  3212. case 4:
  3213. nBaudRate = 14400;
  3214. break;
  3215. case 5:
  3216. nBaudRate = 19200;
  3217. break;
  3218. case 6:
  3219. nBaudRate = 38400;
  3220. break;
  3221. case 7:
  3222. nBaudRate = 57600;
  3223. break;
  3224. case 8:
  3225. nBaudRate = 115200;
  3226. break;
  3227. case 9:
  3228. nBaudRate = 128000;
  3229. break;
  3230. case 10:
  3231. nBaudRate = 256000;
  3232. break;
  3233. }
  3234. return nBaudRate;
  3235. }
  3236. // 获取实际数据位
  3237. INT GetCommDataBit(int nDataBitIndex)
  3238. {
  3239. int nDataBit = 8;
  3240. switch( nDataBitIndex )
  3241. {
  3242. case 0:
  3243. nDataBit = 7;
  3244. break;
  3245. case 1:
  3246. nDataBit = 8;
  3247. break;
  3248. }
  3249. return nDataBit;
  3250. }