GpioClassImpl_DistrBusUSB.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. // GpioClassImpl_DistrBusUSB.cpp :
  2. //
  3. #include <cstdio>
  4. #include "GpioClassImpl_DistrBusUSB.h"
  5. CINIAccess g_iniAccess;
  6. CHAR g_sIniPath[MAX_PATH] = {0};
  7. LOG_EXTERN()
  8. int getFileVer(char* sFile, short &ch1, short &ch2)
  9. {
  10. ch1 = 0;
  11. ch2 = 0;
  12. char* pFind = strstr(sFile, ".so");
  13. char* pTemp = pFind;
  14. while(pTemp)
  15. {
  16. pFind = pTemp;
  17. pTemp = strstr(pFind + 3, ".so");
  18. }
  19. if(pFind == nullptr) return 0;
  20. pTemp = pFind - 1;
  21. while(isdigit(*pTemp) && pTemp > sFile) pTemp--;
  22. if(*pTemp == '.')
  23. ch2 = atoi(pTemp + 1);
  24. pTemp--;
  25. while(isdigit(*pTemp) && pTemp > sFile) pTemp--;
  26. if(*pTemp == '.')
  27. ch1 = atoi(pTemp + 1);
  28. return 1;
  29. }
  30. //int GetDllPathLocal(std::string& dllPath)
  31. //{
  32. // Dl_info path_info;
  33. // dladdr("load_so",&path_info);
  34. // dladdr("LoadSDKDll",&path_info);
  35. // char str_FilePath[256]{0};
  36. // memcpy(str_FilePath,path_info.dli_fname,strlen(path_info.dli_fname));
  37. // printf("first_str_FilePath=%s\n",str_FilePath);
  38. // char *pLastSlath=strrchr(str_FilePath,'/');
  39. // *(pLastSlath + 1)=0;
  40. // std::string a(str_FilePath);
  41. // dllPath = a;
  42. // return 0;
  43. //}
  44. GpioClassImpl::GpioClassImpl():
  45. m_bDevOpen(false),
  46. m_bIsUSBOn(false),
  47. m_nUSBDelay(0),
  48. m_hDistrBusUSB(NULL),
  49. PDD_DIO_CATEGORY(40000),
  50. PDD_DIO_QUERY(CtlCode( PDD_DIO_CATEGORY, (0x900 + 6), Buffered, Any )),
  51. PDD_DIO_READ(CtlCode( PDD_DIO_CATEGORY, (0x900 + 7), Buffered, Any )),
  52. PDD_DIO_WRITE_INT(CtlCode( PDD_DIO_CATEGORY, (0x900 + 10), Buffered, Any ))
  53. {
  54. KEBA_DevCtrl=new CKEBA_DevCtrl();
  55. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  56. }
  57. GpioClassImpl::~GpioClassImpl()
  58. {
  59. LOG_FUNCTION();
  60. // DevClose();
  61. if (KEBA_DevCtrl != NULL)
  62. {
  63. delete KEBA_DevCtrl;
  64. KEBA_DevCtrl = NULL;
  65. }
  66. }
  67. //DeviceBaseClass
  68. ErrorCodeEnum GpioClassImpl::GetDevCategory(DevCategoryInfo &devCategory)
  69. {
  70. LOG_FUNCTION();
  71. // LogM("GpioClassImpl::Entry->Gpio GetDevCategory()!");
  72. memset(devCategory.szType, 0, MAX_DEV_TYPE_LEN);
  73. memset(devCategory.szModel, 0, MAX_DEV_MODEL_LEN);
  74. memset(devCategory.szVendor, 0, MAX_DEV_VENDOR_LEN);
  75. char sPath[256], sFile[128] = {0};
  76. GetCurModulePath(sPath, sFile);
  77. short v1,v2;
  78. getFileVer(sFile, v1, v2);
  79. devCategory.version.wMajor = v1;
  80. devCategory.version.wMinor = v2;
  81. devCategory.version.wRevision = 0xFFFF;
  82. devCategory.version.wBuild = 1;
  83. // memcpy(devCategory.szType, "CMB.GPIO", strlen("CMB.GPIO"));
  84. memcpy(devCategory.szType, "PVER=KEBA#MID=DistrBusUSB", strlen("PVER=KEBA#MID=DistrBusUSB"));
  85. // memcpy(devCategory.szModel, "Distribution_Bus_USB", strlen("Distribution_Bus_USB"));
  86. memcpy(devCategory.szModel, "FWID=V1.0", strlen("FWID=V1.0"));
  87. memcpy(devCategory.szVendor, "KEBA", strlen("KEBA"));
  88. devCategory.eState = (DevStateEnum)m_wDevStatus;
  89. // devCategory.version.wMajor = (WORD)portio_read_version();
  90. LogM("DevCategoryInfo() outputparameter: szType:%s, szModel:%s,szVendor:%s, eState:%d", devCategory.szType, devCategory.szModel, devCategory.szVendor, devCategory.eState);
  91. LogM("DevCategoryInfo() outputparameter: wMajor:%d, wMinor:%d,wRevision:%d, wBuild:%d", devCategory.version.wMajor, devCategory.version.wMinor, devCategory.version.wRevision, devCategory.version.wBuild);
  92. // LogM("DevCategoryInfo() OK!");
  93. return Error_Succeed;
  94. }
  95. ErrorCodeEnum GpioClassImpl::Reset()
  96. {
  97. LOG_FUNCTION();
  98. // LogM("GpioClassImpl::Entry->Gpio Reset()!");
  99. BYTE inBuffer[8]={0};
  100. BYTE outBuffer[8]={0};
  101. ULONG bytesReturned = 0;
  102. char szErrorMsg[256] = {0};
  103. if(!m_bDevOpen)
  104. {
  105. GpioInitParam initParam;
  106. initParam.dwPortNum = m_dwPortNum;
  107. for(int i=0; i<MIN(MAX_PORT_NUM, m_dwPortNum); i++)
  108. {
  109. initParam.dir[i] = m_dir[i];
  110. }
  111. if(DevOpen(initParam))
  112. {
  113. sprintf(szErrorMsg, "打开设备失败");
  114. SaveErrorInfo(szErrorMsg, DEP_DEV_NOT_OPENED, __FUNCTION__, __LINE__);
  115. LogM("Reset() return Error_DevNotAvailable");
  116. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  117. return Error_DevNotAvailable;
  118. }
  119. }
  120. if(KEBA_DevCtrl->Reset() != FALSE)
  121. {
  122. // LogM("Reset() OK!");
  123. sprintf(szErrorMsg, "复位成功");
  124. SaveErrorInfo(szErrorMsg, DEP_SUCCESS, __FUNCTION__, __LINE__);
  125. m_wDevStatus = DEVICE_STATUS_NORMAL;
  126. return Error_Succeed;
  127. }
  128. else
  129. {
  130. LogM("Reset() return Error_DevNotAvailable");
  131. sprintf(szErrorMsg, "复位失败");
  132. SaveErrorInfo(szErrorMsg, DEP_NO_E_GPIO_IOCONTROL, __FUNCTION__, __LINE__);
  133. m_wDevStatus = DEVICE_STATUS_FAULT;
  134. return Error_DevNotAvailable;
  135. }
  136. }
  137. ErrorCodeEnum GpioClassImpl::DevClose()
  138. {
  139. LOG_FUNCTION();
  140. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  141. // LogM("DevClose() ");
  142. if(!m_bDevOpen)
  143. {
  144. // LogM("DevClose(): Dev is not open.");
  145. // LogM("DevClose() OK!");
  146. return Error_Succeed;
  147. }
  148. KEBA_DevCtrl->CloseDevice();
  149. m_bDevOpen = false;
  150. // LogM("DevClose() OK!");
  151. return Error_Succeed;
  152. }
  153. ErrorCodeEnum GpioClassImpl::GetLastErr(DevErrorInfo &devErrInfo)
  154. {
  155. LOG_FUNCTION();
  156. // LogM("GetLastErr() ");
  157. devErrInfo.dwErrMsgLen = strlen(m_szErrMsg);
  158. memset(devErrInfo.szErrMsg, 0, MAX_DEV_ERROR_MSG_LEN);
  159. memcpy(devErrInfo.szErrMsg, m_szErrMsg, MIN(devErrInfo.dwErrMsgLen, MAX_DEV_ERROR_MSG_LEN));
  160. LogM("GetLastErr() return: devErrInfo.dwErrMsgLen=%d, devErrInfo.szErrMsg:%s", devErrInfo.dwErrMsgLen, devErrInfo.szErrMsg);
  161. // LogM("GetLastErr() OK!");
  162. return Error_Succeed;
  163. }
  164. //GpioClass
  165. //
  166. // Device initialization.
  167. // Configure port input/output direction.
  168. //
  169. ErrorCodeEnum GpioClassImpl::DevOpen(GpioInitParam initParam)
  170. {
  171. LOG_FUNCTION();
  172. // CHAR sIniPath[MAX_PATH];
  173. // memset(sIniPath, 0, sizeof(sIniPath));
  174. // char sPath[256] = {0}, sFile[128] = {0};
  175. // GetCurModulePath(sPath, sFile);
  176. // GetCurModulePath(sPath, nullptr);
  177. // memcpy(g_sIniPath,sPath,sizeof (sPath));
  178. // sprintf(m_sIniPath, "%s/%s", m_sIniPath, "keba/Adapter_L.keba.INI");
  179. // strcat(g_sIniPath, "/keba/Adapter_L.keba.INI");
  180. // LogM("GPIO的配置文件=%s",m_sIniPath);
  181. // std::string dllPath = "";
  182. // GetDllPathLocal(dllPath);
  183. // dllPath += "keba/Adapter_L.keba.INI";
  184. // memcpy(g_sIniPath, dllPath.c_str(), dllPath.length());
  185. char szErrorMsg[256] = {0};
  186. CFileDir::GetFilePathName(
  187. getpid(), "keba", "Adapter_L.keba.INI",
  188. g_sIniPath, sizeof(g_sIniPath));
  189. // strcpy(g_sIniPath, "/opt/Run/version/0.0.3.22/dep/keba/Adapter_L.keba.INI");
  190. // m_nUSBDelay = g_iniAccess.GetPrivateProfileInt("Gpio", "USBDELAY", 3000, g_sIniPath);
  191. m_nUSBDelay = 3000;
  192. //DevOpen():dwPortNum:3, dir:1 1 0 204
  193. LogM("DevOpen() input parameter:dwPortNum:%d, dir:%d %d %d %d", initParam.dwPortNum, initParam.dir[0], initParam.dir[1],initParam.dir[2],initParam.dir[3] );
  194. m_dwPortNum = initParam.dwPortNum;
  195. m_outputPortTotal = 0;
  196. for(int i = 0; i < MAX_PORT_NUM; i++)
  197. {
  198. m_dir[i] = initParam.dir[i];
  199. if(m_dir[i]==1)
  200. {
  201. m_outputPortTotal ++;
  202. LogM("DevOpen() m_dir[%d] = %d", i,m_dir[i]);
  203. }
  204. }
  205. if(m_bDevOpen)
  206. {
  207. m_wDevStatus = DEVICE_STATUS_NORMAL;
  208. sprintf(szErrorMsg, "已经打开设备成功");
  209. SaveErrorInfo(szErrorMsg, DEP_SUCCESS, __FUNCTION__, __LINE__);
  210. // LogM("DevOpen() Dev is open already. Return Error_Succeed;");
  211. return Error_Succeed;
  212. }
  213. DevClose();
  214. BOOL bRet = KEBA_DevCtrl->InitDevice();
  215. if (bRet == FALSE)
  216. {
  217. LogM("DevOpen()->GetFileType() failed. return Error_DevNotAvailable;");
  218. DevClose();
  219. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  220. sprintf(szErrorMsg, "打开设备失败");
  221. SaveErrorInfo(szErrorMsg, DEP_DEV_NOT_OPENED, __FUNCTION__, __LINE__);
  222. return Error_DevNotAvailable;
  223. }
  224. m_bDevOpen = true;
  225. m_wDevStatus = DEVICE_STATUS_NORMAL;
  226. sprintf(szErrorMsg, "打开设备成功");
  227. SaveErrorInfo(szErrorMsg, DEP_SUCCESS, __FUNCTION__, __LINE__);
  228. return Error_Succeed;
  229. /*
  230. if(Error_Succeed == LightTurnOn())
  231. {
  232. //m_pLog->Print("DevOpen(): OK!" );
  233. m_wDevStatus = DEVICE_STATUS_NORMAL;
  234. return Error_Succeed;
  235. }
  236. else
  237. {
  238. //m_pLog->Print("DevOpen() return Error_DevNotAvailable;");
  239. m_wDevStatus = DEVICE_STATUS_FAULT;
  240. return Error_DevNotAvailable;
  241. }
  242. */
  243. }
  244. //
  245. // Set ports output.
  246. // Arguments:
  247. // - dwPort:port serial number,0~MAX_PORT_NUM-1
  248. // - btData:output value
  249. // bit value 1/0 means voltage high or low
  250. // ex.dwPort=0 btData=10001010 means set port 0's the 2nd,4th,8th pin output high
  251. //
  252. ErrorCodeEnum GpioClassImpl::WritePort(DWORD dwPort, BYTE btData)
  253. {
  254. LOG_FUNCTION();
  255. //zouchuang rang jia 50ms
  256. usleep(50*1000);
  257. // LogM("WritePort()input parameter:dwPort:%d, btData:0x%02x", dwPort, btData);
  258. if(dwPort == 0)
  259. {
  260. if((btData & 0x02)== 0x02)
  261. m_bIsUSBOn = true;
  262. else if (m_bIsUSBOn)
  263. {
  264. usleep(m_nUSBDelay*1000);
  265. m_bIsUSBOn = false;
  266. }
  267. }
  268. char szErrorMsg[256] = {0};
  269. if(!m_bDevOpen)
  270. {
  271. LogM("WritePort() return Error_DevNotAvailable");
  272. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  273. sprintf(szErrorMsg, "没有打开设备");
  274. SaveErrorInfo(szErrorMsg, DEP_DEV_NOT_OPENED, __FUNCTION__, __LINE__);
  275. return Error_DevNotAvailable;
  276. }
  277. if(!m_dir[dwPort])
  278. {
  279. LogM("WritePort() input parameter is invalied:Port%d is not output port. return Error_Param",dwPort);
  280. return Error_Param;
  281. }
  282. // CHAR sIniPath[MAX_PATH];
  283. // memset(sIniPath, 0, sizeof(sIniPath));
  284. // char sPath[256] = {0}, sFile[128] = {0};
  285. // GetCurModulePath(sPath, sFile);
  286. // memcpy(sIniPath,sPath,sizeof (sPath));
  287. // strcat(sIniPath, "/keba/Adapter_L.keba.INI");
  288. // strcpy(sIniPath, "/opt/Run/version/0.0.3.22/dep/keba/Adapter_L.keba.INI");
  289. BYTE inBuffer[8]={0};
  290. BYTE outBuffer[8]={0};
  291. ULONG bytesReturned = 0;
  292. char outputPin[20] = {0};
  293. int iOutputsNo = 0, iOutputs = 0, iCnt = 0;
  294. iOutputsNo = 0; //PinNo need change status, will put into buffer[0]~[3]
  295. iOutputs = 0; //PinStatus need change,will put into buffer[4]~[7]
  296. BOOL bRet = FALSE;
  297. bool bIsOn[8] = {FALSE};
  298. WORD wOutputNobuf[8] = {0};
  299. int iPin, iOutputNo;
  300. memset(inBuffer, 0x00, 8);
  301. for(int i = 1; i<=8; i++)
  302. {
  303. iPin = i + dwPort*8;
  304. sprintf(outputPin, "OUTPUT_PIN%d", iPin);
  305. iOutputNo = g_iniAccess.GetPrivateProfileInt("Gpio", outputPin, 0, g_sIniPath);
  306. if (iPin == 1)
  307. {
  308. iOutputNo = 11;
  309. }
  310. else if (iPin == 2)
  311. {
  312. iOutputNo = 28;
  313. }
  314. else if (iPin == 3)
  315. {
  316. iOutputNo = 26;
  317. }
  318. else if (iPin == 4)
  319. {
  320. iOutputNo = 15;
  321. }
  322. else if (iPin == 5)
  323. {
  324. iOutputNo = 16;
  325. }
  326. else if (iPin == 6)
  327. {
  328. iOutputNo = 17;
  329. }
  330. else if (iPin == 7)
  331. {
  332. iOutputNo = 21;
  333. }
  334. else if (iPin == 8)
  335. {
  336. iOutputNo = 29;
  337. }
  338. else if (iPin == 9)
  339. {
  340. iOutputNo = 25;
  341. }
  342. else if (iPin == 10)
  343. {
  344. iOutputNo = 24;
  345. }
  346. else if (iPin == 11)
  347. {
  348. iOutputNo = 27;
  349. }
  350. else if (iPin == 12)
  351. {
  352. iOutputNo = 13;
  353. }
  354. else if (iPin == 13)
  355. {
  356. iOutputNo = 19;
  357. }
  358. else if (iPin == 14)
  359. {
  360. iOutputNo = 18;
  361. }
  362. else if (iPin == 15)
  363. {
  364. iOutputNo = 12;
  365. }
  366. else if (iPin == 16)
  367. {
  368. iOutputNo = 22;
  369. }
  370. else {
  371. }
  372. if ( !iOutputNo ) continue;
  373. wOutputNobuf[iCnt] = (WORD)iOutputNo;
  374. if(btData & (1<<(i-1)))
  375. {
  376. bIsOn[iCnt] = TRUE;
  377. }
  378. else
  379. {
  380. bIsOn[iCnt] = FALSE;
  381. }
  382. iCnt ++;
  383. }
  384. bRet = KEBA_DevCtrl->SetGuidLight(wOutputNobuf, bIsOn, iCnt);
  385. if (bRet == FALSE)
  386. {
  387. sprintf(szErrorMsg, "SetGuidLight() 执行失败,点灯失败");
  388. SaveErrorInfo(szErrorMsg, DEP_NO_E_GPIO_IOCONTROL, __FUNCTION__, __LINE__);
  389. LogM("WritePort()input parameter:dwPort:%d, btData:0x%02x", dwPort, btData);
  390. LogM("WritePort()->SetGuidLight(): %s",szErrorMsg);
  391. m_wDevStatus = DEVICE_STATUS_FAULT;
  392. return Error_DevNotAvailable;
  393. }
  394. // LogM("WritePort() OK!");
  395. m_wDevStatus = DEVICE_STATUS_NORMAL;
  396. return Error_Succeed;
  397. }
  398. //
  399. // Get port input.
  400. // Arguments:
  401. // - dwPort:port serial number,0~MAX_PORT_NUM-1
  402. // - btStatus:input value
  403. // ex.dwPort=0 btStatus=10001010 means port 0's the 2nd,4th,8th pin with high level
  404. //
  405. ErrorCodeEnum GpioClassImpl::ReadPort(DWORD dwPort,BYTE &btStatus)
  406. {
  407. LOG_FUNCTION();
  408. char szErrorMsg[256] = {0};
  409. // LogM("ReadPort()input parameter:dwPort:%d", dwPort);
  410. if(!m_bDevOpen)
  411. {
  412. sprintf(szErrorMsg, "没有打开设备");
  413. SaveErrorInfo(szErrorMsg, DEP_DEV_NOT_OPENED, __FUNCTION__, __LINE__);
  414. LogM("ReadPort() return Error_DevNotAvailable");
  415. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  416. return Error_DevNotAvailable;
  417. }
  418. // CHAR sIniPath[MAX_PATH];
  419. // memset(sIniPath, 0, sizeof(sIniPath));
  420. // strcpy(sIniPath, "/opt/Run/version/0.0.3.22/dep/keba/Adapter_L.keba.INI");
  421. // char sPath[256] = {0}, sFile[128] = {0};
  422. // GetCurModulePath(sPath, sFile);
  423. // memcpy(sIniPath,sPath,sizeof (sPath));
  424. // strcat(sIniPath, "/keba/Adapter_L.keba.INI");
  425. BYTE inBuffer[8]={0};
  426. BYTE outBuffer[8]={0};
  427. ULONG bytesReturned = 0;
  428. int iPin = 0, iInputNo = 0;
  429. int portno = 0;
  430. BOOL bRet = FALSE, bFailFlag = FALSE;
  431. WORD wStatus = 0;
  432. memset(inBuffer, 0x00, 8);
  433. btStatus = 0x00;
  434. for(int i = 0; i < m_dwPortNum; i ++)
  435. {
  436. if(!m_dir[i])
  437. {
  438. portno ++;
  439. if(i == dwPort)
  440. {
  441. portno --;
  442. break;
  443. }
  444. }
  445. }
  446. char inputPin[20] = {0};
  447. for(int i = 1; i<=8; i++ )
  448. {
  449. iPin = i + portno*8;
  450. if(m_dir[dwPort])
  451. {
  452. sprintf(inputPin, "OUTPUT_PIN%d", iPin%32);
  453. iInputNo = 32 + g_iniAccess.GetPrivateProfileInt("Gpio", inputPin, 0, g_sIniPath);
  454. if (g_iniAccess.GetPrivateProfileInt("Gpio", inputPin, 0, g_sIniPath) == 0)
  455. {
  456. if (iPin%32 == 1)
  457. {
  458. iInputNo = 32 + 11;
  459. }
  460. else if (iPin%32 == 2)
  461. {
  462. iInputNo = 32 + 28;
  463. }
  464. else if (iPin%32 == 3)
  465. {
  466. iInputNo = 32 + 26;
  467. }
  468. else if (iPin%32 == 4)
  469. {
  470. iInputNo = 32 + 15;
  471. }
  472. else if (iPin%32 == 5)
  473. {
  474. iInputNo = 32 + 16;
  475. }
  476. else if (iPin%32 == 6)
  477. {
  478. iInputNo = 32 + 17;
  479. }
  480. else if (iPin%32 == 7)
  481. {
  482. iInputNo = 32 + 21;
  483. }
  484. else if (iPin%32 == 8)
  485. {
  486. iInputNo = 32 + 29;
  487. }
  488. else if (iPin%32 == 9)
  489. {
  490. iInputNo = 32 + 25;
  491. }
  492. else if (iPin%32 == 10)
  493. {
  494. iInputNo = 32 + 24;
  495. }
  496. else if (iPin%32 == 11)
  497. {
  498. iInputNo = 32 + 27;
  499. }
  500. else if (iPin%32 == 12)
  501. {
  502. iInputNo = 32 + 13;
  503. }
  504. else if (iPin%32 == 13)
  505. {
  506. iInputNo = 32 + 19;
  507. }
  508. else if (iPin%32 == 14)
  509. {
  510. iInputNo = 32 + 18;
  511. }
  512. else if (iPin%32 == 15)
  513. {
  514. iInputNo = 32 + 12;
  515. }
  516. else if (iPin%32 == 16)
  517. {
  518. iInputNo = 32 + 22;
  519. }
  520. else
  521. {
  522. }
  523. }
  524. bRet = KEBA_DevCtrl->getChannelStatus(TRUE, iInputNo, wStatus);
  525. }
  526. else
  527. {
  528. sprintf(inputPin, "INPUT_PIN%d", iPin%32);
  529. iInputNo = g_iniAccess.GetPrivateProfileInt("Gpio", inputPin, 0, g_sIniPath);
  530. if (iInputNo == 0)
  531. {
  532. if (iPin%32 == 1)
  533. {
  534. iInputNo = 7;
  535. }
  536. else if (iPin%32 == 2)
  537. {
  538. iInputNo = 2;
  539. }
  540. else if (iPin%32 == 3)
  541. {
  542. iInputNo = 6;
  543. }
  544. else if (iPin%32 == 4)
  545. {
  546. iInputNo = 5;
  547. }
  548. else if (iPin%32 == 5)
  549. {
  550. iInputNo = 4;
  551. }
  552. else if (iPin%32 == 6)
  553. {
  554. iInputNo = 8;
  555. }
  556. else if (iPin%32 == 7)
  557. {
  558. iInputNo = 3;
  559. }
  560. else if (iPin%32 == 8)
  561. {
  562. iInputNo = 4;
  563. }
  564. else
  565. {
  566. }
  567. }
  568. bRet = KEBA_DevCtrl->getChannelStatus(FALSE, iInputNo, wStatus);
  569. }
  570. if (bRet == FALSE)
  571. {
  572. sprintf(szErrorMsg, "getChannelStatus(channel=%d) 执行失败,获取状态失败", iInputNo);
  573. SaveErrorInfo(szErrorMsg, DEP_NO_E_GPIO_IOCONTROL, __FUNCTION__, __LINE__);
  574. LogM("ReadPort()input parameter:dwPort:%d", dwPort);
  575. LogM("WritePort()->SetGuidLight(): %s",szErrorMsg);
  576. bFailFlag = TRUE;
  577. }
  578. if(wStatus)
  579. {
  580. btStatus |= 1<<(i-1);
  581. }
  582. }
  583. if (bFailFlag != FALSE)
  584. {
  585. m_wDevStatus = DEVICE_STATUS_FAULT;
  586. return Error_DevNotAvailable;
  587. }
  588. // LogM("ReadPort()output parameter:btStatus:0x%02x", btStatus);
  589. // LogM("ReadPort() OK!");
  590. m_wDevStatus = DEVICE_STATUS_NORMAL;
  591. return Error_Succeed;
  592. }
  593. //Private
  594. /*
  595. void GpioClassImpl::SaveErrorInfo(CHAR* errMsg, int errCode)
  596. {
  597. LOG_FUNCTION();
  598. //memset(m_szErrMsg, 0, MAX_DEV_ERR_MSG);
  599. LogM("SaveErrorInfo()input parameter:errMsg:%s, errCode:%d", errMsg, errCode);
  600. memset(m_szErrMsg, 0, MAX_DEV_ERROR_MSG_LEN);
  601. sprintf(m_szErrMsg, "%s[%d]", errMsg, errCode);
  602. // LogM("SaveErrorInfo() OK!");
  603. }
  604. */
  605. void GpioClassImpl::SaveErrorInfo(CHAR* errMsg, int errCode, const CHAR* strFunc, int nLine)
  606. {
  607. CHAR chJson[MAX_DEV_ERROR_MSG_LEN] = {0};
  608. sprintf(chJson, "Func:%s,Line:%d,Msg:%s", strFunc, nLine, errMsg);
  609. memset(m_szErrMsg, 0, MAX_DEV_ERROR_MSG_LEN);
  610. sprintf(m_szErrMsg, "{\"Errcode\":%d,\"Description\":%s}", errCode,chJson);
  611. LogM(m_szErrMsg);
  612. /*
  613. CHAR chJson[MAX_DEV_ERROR_MSG_LEN] = {0};
  614. QJsonObject root_Obj;
  615. QJsonDocument root_Doc;
  616. sprintf(chJson, "Func:%s,Line:%d,Msg:%s", strFunc, nLine, errMsg);
  617. root_Obj.insert("ErrCode", errCode);
  618. root_Obj.insert("Description", chJson);
  619. root_Doc.setObject(root_Obj);
  620. QByteArray root_str = root_Doc.toJson(QJsonDocument::Indented); //标准JSON格式 QString strJson(root_str);
  621. QString strJson(root_str);
  622. memset(m_szErrMsg, 0, MAX_DEV_ERROR_MSG_LEN);
  623. sprintf(m_szErrMsg, strJson.toUtf8().data());
  624. LogM(m_szErrMsg);
  625. */
  626. }
  627. ErrorCodeEnum GpioClassImpl::LightTurnOn()
  628. {
  629. LOG_FUNCTION();
  630. int iOutputNo = 0;
  631. char outputPin[20] = {0};
  632. CHAR szErrorMsg[256] = {0};
  633. if(!m_bDevOpen)
  634. {
  635. LogM("LightTurnOn() return Error_DevNotAvailable");
  636. sprintf(szErrorMsg, "没有打开设备");
  637. SaveErrorInfo(szErrorMsg, DEP_DEV_NOT_OPENED, __FUNCTION__, __LINE__);
  638. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  639. return Error_DevNotAvailable;
  640. }
  641. BYTE inBuffer[8]={0};
  642. BYTE outBuffer[8]={0};
  643. ULONG bytesReturned = 0;
  644. BOOL bRet = FALSE;
  645. int iCnt = 0;
  646. WORD wOutputNoBuf[16] = {0};
  647. bool bOnStat[16] = {1};
  648. // CHAR sIniPath[MAX_PATH];
  649. // memset(sIniPath, 0, sizeof(sIniPath));
  650. // strcpy(sIniPath, "/opt/Run/version/0.0.3.22/dep/keba/Adapter_L.keba.INI");
  651. // char sPath[256] = {0}, sFile[128] = {0};
  652. // GetCurModulePath(sPath, sFile);
  653. // memcpy(sIniPath,sPath,sizeof (sPath));
  654. // strcat(sIniPath, "/keba/Adapter_L.keba.INI");
  655. for (int i = 1; i < 17; i++)
  656. {
  657. sprintf(outputPin, "OUTPUT_PIN%d", i);
  658. iOutputNo = g_iniAccess.GetPrivateProfileInt("Gpio", outputPin, 0, g_sIniPath);
  659. if (i == 1)
  660. {
  661. iOutputNo = 11;
  662. }
  663. else if (i == 2)
  664. {
  665. iOutputNo = 28;
  666. }
  667. else if (i == 3)
  668. {
  669. iOutputNo = 26;
  670. }
  671. else if (i == 4)
  672. {
  673. iOutputNo = 15;
  674. }
  675. else if (i == 5)
  676. {
  677. iOutputNo = 16;
  678. }
  679. else if (i == 6)
  680. {
  681. iOutputNo = 17;
  682. }
  683. else if (i == 7)
  684. {
  685. iOutputNo = 21;
  686. }
  687. else if (i == 8)
  688. {
  689. iOutputNo = 29;
  690. }
  691. else if (i == 9)
  692. {
  693. iOutputNo = 25;
  694. }
  695. else if (i == 10)
  696. {
  697. iOutputNo = 24;
  698. }
  699. else if (i == 11)
  700. {
  701. iOutputNo = 27;
  702. }
  703. else if (i == 12)
  704. {
  705. iOutputNo = 13;
  706. }
  707. else if (i == 13)
  708. {
  709. iOutputNo = 19;
  710. }
  711. else if (i == 14)
  712. {
  713. iOutputNo = 18;
  714. }
  715. else if (i == 15)
  716. {
  717. iOutputNo = 12;
  718. }
  719. else if (i == 16)
  720. {
  721. iOutputNo = 22;
  722. }
  723. else {
  724. }
  725. if ( !iOutputNo ) continue;
  726. wOutputNoBuf[iCnt] = iOutputNo;
  727. iCnt ++;
  728. }
  729. bRet = KEBA_DevCtrl->SetGuidLight(wOutputNoBuf, bOnStat, 16);
  730. if (bRet == FALSE)
  731. {
  732. sprintf(szErrorMsg, "SetGuidLight() 执行失败,点灯失败");
  733. SaveErrorInfo(szErrorMsg, DEP_NO_E_GPIO_IOCONTROL, __FUNCTION__, __LINE__);
  734. LogM("WritePort()->SetGuidLight(): %s",szErrorMsg);
  735. m_wDevStatus = DEVICE_STATUS_FAULT;
  736. return Error_DevNotAvailable;
  737. }
  738. // LogM("LightTurnOn() OK!");
  739. m_wDevStatus = DEVICE_STATUS_NORMAL;
  740. return Error_Succeed;
  741. }
  742. void GpioClassImpl::intToByte(int i, BYTE* buf, UINT uOffset)
  743. {
  744. buf[0 + uOffset] = (BYTE) (0xff & i);
  745. buf[1 + uOffset] = (BYTE) ((0xff00 & i) >> 8);
  746. buf[2 + uOffset] = (BYTE) ((0xff0000 & i) >> 16);
  747. buf[3 + uOffset] = (BYTE) ((0xff000000 & i) >> 24);
  748. }
  749. int GpioClassImpl::bytesToInt(BYTE* bytes, UINT uOffset)
  750. {
  751. int num = bytes[0 + uOffset] & 0xFF;
  752. num |= ((bytes[1 + uOffset] << 8) & 0xFF00);
  753. num |= ((bytes[2 + uOffset] << 16) & 0xFF0000);
  754. num |= ((bytes[3 + uOffset] << 24) & 0xFF000000);
  755. return num;
  756. }