📄 modemdriver.cpp
字号:
tcdrain(m_nFdPort); tcsetattr(m_nFdPort, TCSANOW, &t); bRet = true; } return bRet;}/** Initialise le modem */bool CModemDriver::InitModem(){ bool bRet = false; if ((m_sInitString1 == NULL) || (m_sInitString2 == NULL)) { m_nErrorNbr = 4; } else// envoie des sequences d'initialisation// 1ere if (!SendCommand(m_sInitString1)) { m_nErrorNbr = 0; } else//Attend reponsse ok du modem if (!WaitFor((char *) "OK", 2000, NULL, NULL)) { m_nErrorNbr = 9; } else// 2eme if (!SendCommand(m_sInitString2)) { m_nErrorNbr = 6; }//Attend reponsse ok du modem if (!WaitFor((char *) "OK", 2000, NULL, NULL)) { m_nErrorNbr = 9; } else bRet = true; return bRet;}/** Ferme le port de communication */bool CModemDriver::ClosePort(){ bool bRet = false; struct termios temptty; if (!m_bPortIsOpen) bRet = false; else { tcgetattr(m_nFdPort, &temptty); cfsetospeed(&temptty, B0); cfsetispeed(&temptty, B0); tcsetattr(m_nFdPort, TCSAFLUSH, &oldt); sleep(1); // 0.01 - 3.0 secs tcsetattr(m_nFdPort, TCSAFLUSH, &oldt); close(m_nFdPort); } m_bPortIsOpen = false; return bRet;}/** Attend Timeout sec de recevoir la sequence Seq */bool CModemDriver::WaitFor(char *sSeq, int nTimeout, char *sRetour, int *nLongueur){ bool bRet = false; int nLon, nDebLon; char *sTempBuffer = new char[200]; char *sDebBuffer = sTempBuffer; char *sDebSeq = sSeq; char c; nLon = strlen(sSeq); nDebLon = nLon; memset(sTempBuffer, 0, 200); while ((nLon) && (sTempBuffer < (sDebBuffer + 199))) { if (nTimeout != 0) c = TMread(nTimeout); else c = Mread(); if ((nTimeout != 0) && (c == 0)) break; *sTempBuffer = toupper(c); if (sRetour) sTempBuffer++; if (c == *sSeq) { sSeq++; nLon--; } else { sSeq = sDebSeq; nLon = nDebLon; } } if (sRetour) memcpy(sRetour, sDebBuffer, (sTempBuffer - sDebBuffer)); if (nLongueur) *nLongueur = (sTempBuffer - sDebBuffer); if (!nLon) bRet = true; delete(sDebBuffer); return bRet;}/** Compose le numero et se connecte au fournisseur */bool CModemDriver::Dial(){ bool bRet = false; Byte *sTempBuffer = new Byte[200]; int nLongueur; int nLon; m_cMsg->Printf("%C%M%t%s\n", 130, m_sPhoneNbr); m_cMsg->Printf("%CT1");// Normalise la ligne sprintf((char *) sTempBuffer, "%s%s", (char *) m_sDialCommand, (char *) m_sPhoneNbr);// Numerote if (!SendCommand((char *) sTempBuffer)) { m_nErrorNbr = 11; } else if (!WaitFor ((char *) "ONNECT", 60000, (char *) sTempBuffer, &nLongueur)) { m_nErrorNbr = 12; } else { // deduit la vitesse de connection if (WaitFor((char *) "\r", 2000, (char *) sTempBuffer, &nLongueur)) { nLon = nLongueur - 1; memcpy(m_sConnectSpeed, sTempBuffer, nLon); m_sConnectSpeed[nLon] = 0; } else sprintf(m_sConnectSpeed, "unknow"); bRet = true; } delete(sTempBuffer); return bRet;}/** Teste si le modem est present */bool CModemDriver::IsAvailable(){ bool bRet = false; char cCheck[200]; if (strstr(m_sForceDetect, "Yes") != NULL) bRet = true; else { sprintf(cCheck, "%s\r", m_sCheckString); // teste un open sur le device if (InitPort()) { bRet = sn_modem("0", "ATI3\r"); if (!m_bAutoOk) { SendCommand("ATZ\r"); if (WaitFor((char *) "OK", 2000, NULL, NULL)) bRet = true; tcflush(m_nFdPort, TCIOFLUSH); tcsetattr(m_nFdPort, TCSANOW, &oldt); tcsetattr(m_nFdPort, TCSANOW, &oldt); close(m_nFdPort); } else bRet = true; } } return bRet;}/** controle si le modem supporte les commandes AT **/bool CModemDriver::sn_modem(char *escapechar, char *command){ bool bRet = true; char loc_buff[50]; bRet = SendCommand(command); if (bRet == true) { while (read(m_nFdPort, &loc_buff, 50) < 1); if (loc_buff) { fprintf(stdout, "%s \n", loc_buff); } else { fprintf(stdout, "Windmodem is not support \n"); } } return (bRet);}/** Fournit le pointeur sur la structure des parametres */CParamConfig *CModemDriver::GetRequest(){ return m_cParam;}/** envoie une commande au modem */bool CModemDriver::SendCommand(char *buffer){ bool bRet = false; char *pTmpBuffer = new char[30]; sprintf((char *) pTmpBuffer, "%s\r", buffer); if (write(m_nFdPort, pTmpBuffer, strlen((const char *) pTmpBuffer)) > (-1)) bRet = true; delete(pTmpBuffer); return bRet;}/** fournit la vitesse */int CModemDriver::GetSpeed(){ return atoi(m_sConnectSpeed);}/** force la lecture de 1 caratere */Byte CModemDriver::Mread(){ char cRet; while (read(m_nFdPort, &cRet, 1) < 1); return cRet;}/** force la lecture de 1 caractere avec un timeout */Byte CModemDriver::TMread(int nTimeout){ Byte cRet = 0; int nTime = 0; while ((read(m_nFdPort, &cRet, 1) < 1) && (nTime < nTimeout)) { usleep(1000); nTime++; } if (nTime == nTimeout) cRet = 0; return cRet;}/** definie si connecter ou non */bool CModemDriver::IsConnected(){ int status; if (strstr(m_sDevice, "ttyI") == NULL) { ioctl(m_nFdPort, TIOCMGET, &status); if (!(status & TIOCM_CAR)) m_bIsConnected = false; } return m_bIsConnected;}/** recherche auto du modem */bool CModemDriver::FindAuto(){ char *Modem[] = { "/dev/ttyS0", "/dev/ttyS0", "/dev/cua0", "/dev/ttyI0", "/dev/ttyS1", "/dev/cua1", "/dev/ttyI1" }; bool bRet = false; struct termios t, oldt, temptty; int i; char *PosModem; char sTmp[50]; if (m_bAutoOk) return true; if (strstr(m_sDEV, "Auto") == NULL) { strcpy(m_sDevice, m_sDEV); bRet = true; } else { // recherche Modem[0] = m_sAutoDev; for (i = 0; i < 7; i++) { if ((m_nFdPort = open(Modem[i], O_RDWR | O_NDELAY)) < 0) continue; // init le port tcgetattr(m_nFdPort, &t); tcgetattr(m_nFdPort, &oldt); tcdrain(m_nFdPort); tcflush(m_nFdPort, TCIOFLUSH); if (strstr(Modem[i], "ttyI") != NULL) t.c_cc[VMIN] = 0; // nonblocking else t.c_cc[VMIN] = 1; // nonblocking t.c_cc[VTIME] = 0; t.c_oflag = 0; t.c_lflag = 0; t.c_cflag &= ~(CSIZE | CSTOPB | PARENB); t.c_cflag |= CS8 | CREAD; t.c_cflag |= CLOCAL; // ignore modem status lines t.c_iflag = IGNBRK | IGNPAR /* | ISTRIP */ ; t.c_lflag &= ~ICANON; // non-canonical mode t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE); t.c_cflag |= CRTSCTS; cfgetispeed(&t); if (strstr(Modem[i], "ttyI") != NULL) { cfsetispeed(&t, B38400); // auto-match to output speed cfsetospeed(&t, B38400); } else { cfsetispeed(&t, B9600); // auto-match to output speed cfsetospeed(&t, B9600); } tcsetattr(m_nFdPort, TCSANOW, &t); tcdrain(m_nFdPort); tcsetattr(m_nFdPort, TCSANOW, &t); SendCommand("ATZ\r"); if (WaitFor((char *) "OK", 200, NULL, NULL)) { PosModem = Modem[i]; strcpy(m_sDevice, PosModem); tcflush(m_nFdPort, TCIOFLUSH); tcsetattr(m_nFdPort, TCSANOW, &oldt); tcsetattr(m_nFdPort, TCSANOW, &oldt); close(m_nFdPort); bRet = true; m_bAutoOk = true; break; } tcflush(m_nFdPort, TCIOFLUSH); tcsetattr(m_nFdPort, TCSANOW, &oldt); tcsetattr(m_nFdPort, TCSANOW, &oldt); close(m_nFdPort); } }// vitesse de connection !! if (strstr(m_sSpeed, "Auto") == NULL) nSpeed = strtol(m_sSpeed, NULL, 10); else if (strstr(m_sDevice, "ttyI") != NULL) nSpeed = 38400; else nSpeed = 57600; return bRet;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -