gpio_impl.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef LIBFRAMEWORK_GPIO_IMPL_H
  2. #define LIBFRAMEWORK_GPIO_IMPL_H
  3. #include "GpioClass.h"
  4. #include "IOSignalCtrBoardDev.h"
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <cstring>
  8. #include <cassert>
  9. #include <string>
  10. #include <iostream>
  11. //bhj
  12. #include "ExactTick.h"
  13. #if (defined _WIN32 || defined _WIN64)
  14. #include "windows.h"
  15. #ifndef AFX_ODEVBASE_H__8BBC195A_112D_11D4_B865_5254AB1A7D30__INCLUDED_COM_
  16. #define AFX_ODEVBASE_H__8BBC195A_112D_11D4_B865_5254AB1A7D30__INCLUDED_COM_
  17. #pragma pack(push, 1)
  18. // typedef struct tDevReturn
  19. // {
  20. // int iLogicCode;
  21. // int iPhyCode;
  22. // int iHandle;
  23. // int iType;
  24. // char acDevReturn[128];
  25. // char acReserve[128];
  26. // } tDevReturn;
  27. #pragma pack(pop)
  28. #define SUCCESS 0
  29. #define FAIL 1
  30. #define ERRORFLAG_CLEAR 0
  31. #endif // AFX_ODEVBASE_H__8BBC195A_112D_11D4_B865_5254AB1A7D30__INCLUDED_COM_
  32. #define snprintf _snprintf
  33. #else //
  34. #include <unistd.h>
  35. #include <dlfcn.h>
  36. #include <cwchar>
  37. typedef void* HMODULE;
  38. //typedef void* HANDLE;
  39. typedef unsigned int UINT;
  40. HMODULE LoadLibraryA(const char* lpLibFileName)
  41. {
  42. std::string l_szLibFileName = lpLibFileName;
  43. l_szLibFileName = "lib"+l_szLibFileName.substr(0, l_szLibFileName.rfind(".dll", l_szLibFileName.length()))+".so";
  44. void* h = dlopen(l_szLibFileName.c_str(), RTLD_NOW);
  45. printf("LoadLibrary %s = %p \r\n", l_szLibFileName.c_str(), h);
  46. return (HMODULE)h;
  47. }
  48. void* GetProcAddress(HMODULE hModule, const char* lpProcName)
  49. {
  50. return (void*)dlsym(hModule, lpProcName);
  51. }
  52. int FreeLibrary(HMODULE hLibModule)
  53. {
  54. if (NULL==hLibModule)
  55. {
  56. return 0;
  57. }
  58. int l_bRet = 0==dlclose(hLibModule);
  59. printf("FreeLibrary %p \r\n", hLibModule);
  60. return l_bRet;
  61. }
  62. #define NOERROR 0
  63. #define UNREFERENCED_PARAMETER(p) if ((p)!=NULL)assert(p)
  64. #define WINAPI
  65. #define LoadLibrary LoadLibraryA
  66. typedef unsigned int WPARAM;
  67. typedef unsigned char BYTE;
  68. #ifndef GRGDEVBASE_H
  69. #define GRGDEVBASE_H
  70. #pragma pack(push, 1)
  71. typedef struct tDevReturn
  72. {
  73. int iLogicCode;
  74. int iPhyCode;
  75. int iHandle;
  76. int iType;
  77. char acDevReturn[128];
  78. char acReserve[128];
  79. } tDevReturn;
  80. #pragma pack(pop)
  81. #define SUCCESS 0
  82. #define FAIL 1
  83. #define ERRORFLAG_CLEAR 0
  84. #endif // GRGDEVBASE_H
  85. #endif // _WIN32
  86. #pragma pack(push, 1)
  87. #include "IOExtendDev.h"
  88. #pragma pack(pop)
  89. #define DEVCLASSNAME IOExtendDev
  90. #define DEVCLASSPARA "grgIOExtendDev.dll"
  91. #define SAVETRACE(sz) printf("%s,%s,%d:%s\n", __FILE__, __FUNCTION__, __LINE__, (sz));
  92. DEVCLASSNAME* g_pDev = NULL;
  93. //////////////////////////////////////////////////////////////////////////
  94. class GPIOClassImpl : public GpioClass
  95. {
  96. public:
  97. GPIOClassImpl();
  98. ~GPIOClassImpl();
  99. //DeviceBaseClass
  100. ErrorCodeEnum GetDevCategory(DevCategoryInfo &devCategory);
  101. ErrorCodeEnum Reset();
  102. ErrorCodeEnum DevClose();
  103. ErrorCodeEnum GetLastErr(DevErrorInfo &devErrInfo);
  104. //
  105. // Device initialization.
  106. // Configure port input/output direction.
  107. //
  108. virtual ErrorCodeEnum DevOpen(GpioInitParam initParam);
  109. //
  110. // Set ports output.
  111. // Arguments:
  112. // - dwPort:port serial number,0~MAX_PORT_NUM-1
  113. // - btData:output value
  114. // bit value 1/0 means voltage high or low
  115. // ex.dwPort=0 btData=10001010 means set port 0's the 2nd,4th,8th pin output high
  116. //
  117. virtual ErrorCodeEnum WritePort(DWORD dwPort, BYTE btData);
  118. //
  119. // Get port input.
  120. // Arguments:
  121. // - dwPort:port serial number,0~MAX_PORT_NUM-1
  122. // - btStatus:input value
  123. // ex.dwPort=0 btStatus=10001010 means port 0's the 2nd,4th,8th pin with high level
  124. //
  125. virtual ErrorCodeEnum ReadPort(DWORD dwPort, BYTE& btStatus);
  126. private:
  127. int m_mode;
  128. void* m_pHandle;
  129. DevErrorInfo m_DevErrorInfo;
  130. GpioInitParam m_GpioParm;
  131. void ZeroDevErrorInfo();
  132. void FormatDevErrorInfo(const char* funcname, int line, int errCode,const char* errStr);
  133. //light
  134. bool vChangeTable(int &p_iGrg, int &p_iZS, bool p_bToZS = true);
  135. void vGetLightStatus(BYTE *p_pbtStatus, tDevReturn *p_ptDevReturn);
  136. int iGetLightStatus(int p_piLightNum, tDevReturn *p_psStatus);
  137. //IO in
  138. int iGetIOInStatus(int p_piLightNum, tDevReturn *p_psStatus);
  139. void vGetIOInStatus(BYTE *p_pbtStatus, tDevReturn *p_ptDevReturn);
  140. bool vChangeTableIOIn(int &p_iGrg, int &p_iZS, bool p_bToZS = true);
  141. //IO扩展板接口
  142. int ExiSetCommPara(const char* funcname, int line);
  143. int ExiCloseComm(const char* funcname, int line);
  144. int ExiInit(const char* funcname, int line);
  145. int ExiGetVersion(const char* funcname, int line);
  146. int ExiGetDevVersion(const char* funcname, int line);
  147. int ExiGetSerialNumber(const char* funcname, int line);
  148. int ExiGetLightStatus(const char* funcname, int line);
  149. int ExiSetLightStatus(int p_piLightNum, int p_piStatus,const char* funcname, int line);
  150. };
  151. #endif //LIBFRAMEWORK_GPIO_IMPL_H