| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- #include "gpiodevice.h"
- #include "log4vendor.h"
- GPIODevice::GPIODevice()
- {
- }
- GPIODevice::~GPIODevice()
- {
- }
- int GPIODevice::SetLight(int iIndex, char* sCmd, int iState)
- {
- char sSend[256]={0};
- strcpy(sSend, sCmd);
- int iLen = strlen(sCmd);
- sSend[iLen++] = iIndex;
- sSend[iLen++] = iState;
- int iRead = 255;
- int hRt = ExecuteSIU(sSend , iLen, sSend, iRead, 1000) ;
- return hRt;
- }
- /************************************************************************/
- /* 断电重启: nTimeToPowerOff -命令发送之后多久断电; */
- /* nPowerOffDuration -断电持续时间; */
- /* 单位:秒 */
- /************************************************************************/
- int GPIODevice::RestartPower(int nTimeToPowerOff, int nPowerOffDuration )
- {
- char sSend[256] = {0};
- sSend[0] = 'C' ;
- sSend[1] = ':' ;
- sSend[2] = 0;
- sSend[3] = (char)nTimeToPowerOff;
- sSend[4] = 0;
- sSend[5] = (char)nPowerOffDuration;
- int iRead = 255;
- int hRt = ExecuteSIU( sSend , 6, sSend, iRead, 1000 ) ;
- return hRt ;
- }
- int GPIODevice::Init()
- {
- char sSend[256]={"C:"};
- sSend[0] = 'I' ;
- int iRead = 255;
- int hRt = ExecuteSIU(sSend , 1 , sSend, iRead, 1000) ;
- return hRt;
- }
- int GPIODevice::GetFirmware(char* sOut)
- {
- char sSend[256]={"v"};
- int iRead = 255;
- int hRt = ExecuteSIU(sSend , 1 , sSend, iRead, 1000) ;
- if (hRt == 0)
- strcpy(sOut, sSend);
- else
- sOut[0]=0;
- return hRt;
- }
- int GPIODevice::OpenSIU(const char* sPort, int iBaud)
- {
- if (m_SerSIU.IsOpen())
- m_SerSIU.Close();
- if (!m_SerSIU.Open(sPort, iBaud))
- return GPIO_HARDWARE_ERROR;
- return GPIO_SUCCESS;
- }
- int GPIODevice::OpenYYB(const char* sPort, int iBaud)
- {
- if (m_SerYYB.IsOpen())
- m_SerYYB.Close();
- if (!m_SerYYB.Open(sPort, iBaud)) return GPIO_HARDWARE_ERROR;
- BYTE sSta[64];
- int iRet = GetYYBSta(sSta);
- if (iRet != 0)
- {
- m_SerYYB.Close();
- return GPIO_NOT_OPEN;
- }
- return 0;
- }
- int GPIODevice::OpenLCK(const char* sPort, int iBaud)
- {
- if (m_SerLCK.IsOpen())
- m_SerLCK.Close();
- if (!m_SerLCK.Open(sPort, iBaud)) return GPIO_HARDWARE_ERROR;
- return GPIO_SUCCESS;
- }
- int GPIODevice::Close()
- {
- m_SerSIU.Close();
- m_SerYYB.Close();
- m_SerLCK.Close();
- return 0;
- }
- int GPIODevice::GetStatus( short & dwState)
- {
- BYTE sSend[32]={0};
- int iRet = GetSIUSta(sSend);
- if (iRet == 0)
- {
- dwState = 0;
- //人体接近
- int iGet = ((sSend[0] >> 7) & 1);
- dwState |= iGet << 3;
- //电话摘机
- iGet = ((sSend[2] >> 0) & 1) ? 0 : 1;
- dwState |= iGet << 2;
- //开门
- iGet = ((sSend[1] >> 7) & 1) ? 0 : 1;
- dwState |= iGet << 1;
- //震动
- iGet = ((sSend[1] >> 4) & 1) ? 0 : 1;
- dwState |= iGet << 0;
- }
- else return iRet;
- if (m_SerYYB.IsOpen() && GetYYBSta(sSend) == 0)
- {
- dwState &= ~(1 << 2);
- int iGet = sSend[0] & 1;
- dwState |= iGet << 2;
- }
- if (m_SerLCK.IsOpen())
- {
- }
- return 0;
- }
- int GPIODevice::GetSIUSta(BYTE* sSta)
- {
- BYTE sSend[32]={0};
- sSend[0] = 's' ;
- int iRead = 31;
- int iRt = ExecuteSIU((char*)sSend , 1 , (char*)sSend, iRead, 1000) ;
- if (iRt == 0)
- {
- memcpy(sSta, sSend, iRead);
- sSta[iRead] = 0;
- return 0;
- }
- return iRt;
- }
- int GPIODevice::GetYYBSta(BYTE* sSta)
- {
- BYTE sSend[32]={0};
- sSend[0] = 0x13 ;
- sSend[1] = 0 ;
- int iRead = 31;
- int iRt = ExecuteYYB((char*)sSend , 2 , (char*)sSend, iRead, 1000) ;
- if (iRt == 0)
- {
- memcpy(sSta, sSend, iRead);
- sSta[iRead] = 0;
- return 0;
- }
- return iRt;
- }
- // 执行命令
- int GPIODevice::ExecuteSIU(char * pSend, int iLength, char* sRecv, int& iRecv, int iTimeOut )
- {
- if (iLength > 250) return GPIO_PARA_ERR;
- BYTE sBuf[260];
- sBuf[0] = SIUSTX ;
- sBuf[1] = SIUID ;
- sBuf[2] = 0;
- sBuf[3] = (BYTE) (iLength + 5) ;
- memcpy(sBuf + 4, pSend, iLength);
- BYTE BCC = 0;
- for(int i = 0 ; i < iLength + 4; i ++ )
- BCC ^= sBuf[i] ;
- sBuf[ iLength + 4 ] = BCC ;
- sBuf[ iLength + 5 ] = ETX ;
- m_SerSIU.Flush();
- int iLen = m_SerSIU.Send((char*)sBuf, iLength+6, 300);
- if (iTimeOut < 500) iTimeOut = 500;
- INT64 iNow = GetSystemTime();
- INT64 iEnd = iNow + iTimeOut;
- int iHeadLen = 6;
- for (int ia=0; ia<100 && iNow < iEnd; ia++)
- {
- if (m_SerSIU.Receive((char*)sBuf, 1, iTimeOut) < 0)
- {
- LOG4VTM(ERROR, "serial port date read failed");
- return GPIO_TIMEOUT;
- }
- if (sBuf[0] == STX_RET) break;
- else
- {
- LOG4VTM(ERROR, "serial port read data err");
- }
- iNow = GetSystemTime();
- }
- if (sBuf[0] != STX_RET)
- {
- LOG4VTM(ERROR, "serial port date format error");
- return GPIO_PARA_ERR;
- }
- iLen = m_SerSIU.Receive((char*)sBuf + 1, iHeadLen - 1, 100);
- if (iLen + 1 < iHeadLen)
- {
- LOG4VTM(ERROR, "serial port date read failed");
- return GPIO_TIMEOUT;
- }
- int iBodyLen = (sBuf[2] << 8) + sBuf[3] - iHeadLen;
- iLen = m_SerSIU.Receive((char*)sRecv, iBodyLen + 1, 100);
- if (iLen < iBodyLen)
- {
- LOG4VTM(ERROR, "serial port date read body failed");
- return GPIO_TIMEOUT;
- }
- iRecv = iBodyLen;
- sRecv[iRecv] = 0;
- if (sBuf[4] == 'S' || sBuf[4] == 'K' || sBuf[4] == 'D') return GPIO_SUCCESS;
- if (sBuf[4] == 'E') return GPIO_HARDWARE_ERROR;
- return 0;
- }
- // 执行命令
- int GPIODevice::ExecuteYYB(char * pSend, int iLength, char* sRecv, int& iRecv, int iTimeOut )
- {
- if (iLength > 250) return GPIO_PARA_ERR;
- BYTE sBuf[260];
- sBuf[0] = YYBSTX ;
- sBuf[1] = YYBID ;
- sBuf[2] = (BYTE) (iLength + 6) ;
- sBuf[3] = (BYTE) iLength;
- memcpy(sBuf + 4, pSend, iLength);
- BYTE BCC = 0;
- for(int i = 0 ; i < iLength + 4; i ++ )
- BCC ^= sBuf[i] ;
- sBuf[ iLength + 4 ] = BCC ;
- sBuf[ iLength + 5 ] = ETX ;
- m_SerYYB.Flush();
- m_SerYYB.Send((char*)sBuf, iLength+6, 300);
- if (iTimeOut < 500) iTimeOut = 500;
- INT64 iNow = GetSystemTime();
- INT64 iEnd = iNow + iTimeOut;
- int iHeadLen = 6;
- for (int ia=0; ia<100 && iNow < iEnd; ia++)
- {
- if (m_SerYYB.Receive((char*)sBuf, 1, iTimeOut) < 0)
- {
- LOG4VTM(ERROR, "serial port date read failed");
- return GPIO_TIMEOUT;
- }
- if (sBuf[0] == STX_RET) break;
- else
- {
- LOG4VTM(ERROR, "serial port read data err");
- }
- iNow = GetSystemTime();
- }
- if (sBuf[0] != STX_RET)
- {
- LOG4VTM(ERROR, "serial port date format error");
- return GPIO_PARA_ERR;
- }
- int iLen = m_SerYYB.Receive((char*)sBuf + 1, iHeadLen - 1, 100);
- if (iLen + 1 < iHeadLen)
- {
- LOG4VTM(ERROR, "serial port date read failed");
- return GPIO_TIMEOUT;
- }
- int iBodyLen = sBuf[3];
- iLen = m_SerYYB.Receive((char*)sBuf + iHeadLen, iBodyLen, 100);
- if (iLen < iBodyLen)
- {
- LOG4VTM(ERROR, "serial port date read body failed");
- return GPIO_TIMEOUT;
- }
- iRecv = iBodyLen;
- memcpy(sRecv, (char*)sBuf + 4, iBodyLen);
- sRecv[iRecv] = 0;
- return GPIO_SUCCESS;
- }
|