gpiodevice.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "gpiodevice.h"
  2. #include "log4vendor.h"
  3. GPIODevice::GPIODevice()
  4. {
  5. }
  6. GPIODevice::~GPIODevice()
  7. {
  8. }
  9. int GPIODevice::SetLight(int iIndex, char* sCmd, int iState)
  10. {
  11. char sSend[256]={0};
  12. strcpy(sSend, sCmd);
  13. int iLen = strlen(sCmd);
  14. sSend[iLen++] = iIndex;
  15. sSend[iLen++] = iState;
  16. int iRead = 255;
  17. int hRt = ExecuteSIU(sSend , iLen, sSend, iRead, 1000) ;
  18. return hRt;
  19. }
  20. /************************************************************************/
  21. /* 断电重启: nTimeToPowerOff -命令发送之后多久断电; */
  22. /* nPowerOffDuration -断电持续时间; */
  23. /* 单位:秒 */
  24. /************************************************************************/
  25. int GPIODevice::RestartPower(int nTimeToPowerOff, int nPowerOffDuration )
  26. {
  27. char sSend[256] = {0};
  28. sSend[0] = 'C' ;
  29. sSend[1] = ':' ;
  30. sSend[2] = 0;
  31. sSend[3] = (char)nTimeToPowerOff;
  32. sSend[4] = 0;
  33. sSend[5] = (char)nPowerOffDuration;
  34. int iRead = 255;
  35. int hRt = ExecuteSIU( sSend , 6, sSend, iRead, 1000 ) ;
  36. return hRt ;
  37. }
  38. int GPIODevice::Init()
  39. {
  40. char sSend[256]={"C:"};
  41. sSend[0] = 'I' ;
  42. int iRead = 255;
  43. int hRt = ExecuteSIU(sSend , 1 , sSend, iRead, 1000) ;
  44. return hRt;
  45. }
  46. int GPIODevice::GetFirmware(char* sOut)
  47. {
  48. char sSend[256]={"v"};
  49. int iRead = 255;
  50. int hRt = ExecuteSIU(sSend , 1 , sSend, iRead, 1000) ;
  51. if (hRt == 0)
  52. strcpy(sOut, sSend);
  53. else
  54. sOut[0]=0;
  55. return hRt;
  56. }
  57. int GPIODevice::OpenSIU(const char* sPort, int iBaud)
  58. {
  59. if (m_SerSIU.IsOpen())
  60. m_SerSIU.Close();
  61. if (!m_SerSIU.Open(sPort, iBaud))
  62. return GPIO_HARDWARE_ERROR;
  63. return GPIO_SUCCESS;
  64. }
  65. int GPIODevice::OpenYYB(const char* sPort, int iBaud)
  66. {
  67. if (m_SerYYB.IsOpen())
  68. m_SerYYB.Close();
  69. if (!m_SerYYB.Open(sPort, iBaud)) return GPIO_HARDWARE_ERROR;
  70. BYTE sSta[64];
  71. int iRet = GetYYBSta(sSta);
  72. if (iRet != 0)
  73. {
  74. m_SerYYB.Close();
  75. return GPIO_NOT_OPEN;
  76. }
  77. return 0;
  78. }
  79. int GPIODevice::OpenLCK(const char* sPort, int iBaud)
  80. {
  81. if (m_SerLCK.IsOpen())
  82. m_SerLCK.Close();
  83. if (!m_SerLCK.Open(sPort, iBaud)) return GPIO_HARDWARE_ERROR;
  84. return GPIO_SUCCESS;
  85. }
  86. int GPIODevice::Close()
  87. {
  88. m_SerSIU.Close();
  89. m_SerYYB.Close();
  90. m_SerLCK.Close();
  91. return 0;
  92. }
  93. int GPIODevice::GetStatus( short & dwState)
  94. {
  95. BYTE sSend[32]={0};
  96. int iRet = GetSIUSta(sSend);
  97. if (iRet == 0)
  98. {
  99. dwState = 0;
  100. //人体接近
  101. int iGet = ((sSend[0] >> 7) & 1);
  102. dwState |= iGet << 3;
  103. //电话摘机
  104. iGet = ((sSend[2] >> 0) & 1) ? 0 : 1;
  105. dwState |= iGet << 2;
  106. //开门
  107. iGet = ((sSend[1] >> 7) & 1) ? 0 : 1;
  108. dwState |= iGet << 1;
  109. //震动
  110. iGet = ((sSend[1] >> 4) & 1) ? 0 : 1;
  111. dwState |= iGet << 0;
  112. }
  113. else return iRet;
  114. if (m_SerYYB.IsOpen() && GetYYBSta(sSend) == 0)
  115. {
  116. dwState &= ~(1 << 2);
  117. int iGet = sSend[0] & 1;
  118. dwState |= iGet << 2;
  119. }
  120. if (m_SerLCK.IsOpen())
  121. {
  122. }
  123. return 0;
  124. }
  125. int GPIODevice::GetSIUSta(BYTE* sSta)
  126. {
  127. BYTE sSend[32]={0};
  128. sSend[0] = 's' ;
  129. int iRead = 31;
  130. int iRt = ExecuteSIU((char*)sSend , 1 , (char*)sSend, iRead, 1000) ;
  131. if (iRt == 0)
  132. {
  133. memcpy(sSta, sSend, iRead);
  134. sSta[iRead] = 0;
  135. return 0;
  136. }
  137. return iRt;
  138. }
  139. int GPIODevice::GetYYBSta(BYTE* sSta)
  140. {
  141. BYTE sSend[32]={0};
  142. sSend[0] = 0x13 ;
  143. sSend[1] = 0 ;
  144. int iRead = 31;
  145. int iRt = ExecuteYYB((char*)sSend , 2 , (char*)sSend, iRead, 1000) ;
  146. if (iRt == 0)
  147. {
  148. memcpy(sSta, sSend, iRead);
  149. sSta[iRead] = 0;
  150. return 0;
  151. }
  152. return iRt;
  153. }
  154. // 执行命令
  155. int GPIODevice::ExecuteSIU(char * pSend, int iLength, char* sRecv, int& iRecv, int iTimeOut )
  156. {
  157. if (iLength > 250) return GPIO_PARA_ERR;
  158. BYTE sBuf[260];
  159. sBuf[0] = SIUSTX ;
  160. sBuf[1] = SIUID ;
  161. sBuf[2] = 0;
  162. sBuf[3] = (BYTE) (iLength + 5) ;
  163. memcpy(sBuf + 4, pSend, iLength);
  164. BYTE BCC = 0;
  165. for(int i = 0 ; i < iLength + 4; i ++ )
  166. BCC ^= sBuf[i] ;
  167. sBuf[ iLength + 4 ] = BCC ;
  168. sBuf[ iLength + 5 ] = ETX ;
  169. m_SerSIU.Flush();
  170. int iLen = m_SerSIU.Send((char*)sBuf, iLength+6, 300);
  171. if (iTimeOut < 500) iTimeOut = 500;
  172. INT64 iNow = GetSystemTime();
  173. INT64 iEnd = iNow + iTimeOut;
  174. int iHeadLen = 6;
  175. for (int ia=0; ia<100 && iNow < iEnd; ia++)
  176. {
  177. if (m_SerSIU.Receive((char*)sBuf, 1, iTimeOut) < 0)
  178. {
  179. LOG4VTM(ERROR, "serial port date read failed");
  180. return GPIO_TIMEOUT;
  181. }
  182. if (sBuf[0] == STX_RET) break;
  183. else
  184. {
  185. LOG4VTM(ERROR, "serial port read data err");
  186. }
  187. iNow = GetSystemTime();
  188. }
  189. if (sBuf[0] != STX_RET)
  190. {
  191. LOG4VTM(ERROR, "serial port date format error");
  192. return GPIO_PARA_ERR;
  193. }
  194. iLen = m_SerSIU.Receive((char*)sBuf + 1, iHeadLen - 1, 100);
  195. if (iLen + 1 < iHeadLen)
  196. {
  197. LOG4VTM(ERROR, "serial port date read failed");
  198. return GPIO_TIMEOUT;
  199. }
  200. int iBodyLen = (sBuf[2] << 8) + sBuf[3] - iHeadLen;
  201. iLen = m_SerSIU.Receive((char*)sRecv, iBodyLen + 1, 100);
  202. if (iLen < iBodyLen)
  203. {
  204. LOG4VTM(ERROR, "serial port date read body failed");
  205. return GPIO_TIMEOUT;
  206. }
  207. iRecv = iBodyLen;
  208. sRecv[iRecv] = 0;
  209. if (sBuf[4] == 'S' || sBuf[4] == 'K' || sBuf[4] == 'D') return GPIO_SUCCESS;
  210. if (sBuf[4] == 'E') return GPIO_HARDWARE_ERROR;
  211. return 0;
  212. }
  213. // 执行命令
  214. int GPIODevice::ExecuteYYB(char * pSend, int iLength, char* sRecv, int& iRecv, int iTimeOut )
  215. {
  216. if (iLength > 250) return GPIO_PARA_ERR;
  217. BYTE sBuf[260];
  218. sBuf[0] = YYBSTX ;
  219. sBuf[1] = YYBID ;
  220. sBuf[2] = (BYTE) (iLength + 6) ;
  221. sBuf[3] = (BYTE) iLength;
  222. memcpy(sBuf + 4, pSend, iLength);
  223. BYTE BCC = 0;
  224. for(int i = 0 ; i < iLength + 4; i ++ )
  225. BCC ^= sBuf[i] ;
  226. sBuf[ iLength + 4 ] = BCC ;
  227. sBuf[ iLength + 5 ] = ETX ;
  228. m_SerYYB.Flush();
  229. m_SerYYB.Send((char*)sBuf, iLength+6, 300);
  230. if (iTimeOut < 500) iTimeOut = 500;
  231. INT64 iNow = GetSystemTime();
  232. INT64 iEnd = iNow + iTimeOut;
  233. int iHeadLen = 6;
  234. for (int ia=0; ia<100 && iNow < iEnd; ia++)
  235. {
  236. if (m_SerYYB.Receive((char*)sBuf, 1, iTimeOut) < 0)
  237. {
  238. LOG4VTM(ERROR, "serial port date read failed");
  239. return GPIO_TIMEOUT;
  240. }
  241. if (sBuf[0] == STX_RET) break;
  242. else
  243. {
  244. LOG4VTM(ERROR, "serial port read data err");
  245. }
  246. iNow = GetSystemTime();
  247. }
  248. if (sBuf[0] != STX_RET)
  249. {
  250. LOG4VTM(ERROR, "serial port date format error");
  251. return GPIO_PARA_ERR;
  252. }
  253. int iLen = m_SerYYB.Receive((char*)sBuf + 1, iHeadLen - 1, 100);
  254. if (iLen + 1 < iHeadLen)
  255. {
  256. LOG4VTM(ERROR, "serial port date read failed");
  257. return GPIO_TIMEOUT;
  258. }
  259. int iBodyLen = sBuf[3];
  260. iLen = m_SerYYB.Receive((char*)sBuf + iHeadLen, iBodyLen, 100);
  261. if (iLen < iBodyLen)
  262. {
  263. LOG4VTM(ERROR, "serial port date read body failed");
  264. return GPIO_TIMEOUT;
  265. }
  266. iRecv = iBodyLen;
  267. memcpy(sRecv, (char*)sBuf + 4, iBodyLen);
  268. sRecv[iRecv] = 0;
  269. return GPIO_SUCCESS;
  270. }