www.pudn.com > IrMobile.zip > Mobile.cpp
// Mobile.cpp: implementation of the CMobile class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Mobile.h" #include#include #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CMobile::CMobile() { cs = NULL; } CMobile::~CMobile() { } BOOL CMobile::ReadResponse() { char data[1024]; if (!cs) return FALSE; int len,i; for (i=0;i<300;i++) { len = cs->ReadData(data); m_LastResponese.Format(_T("%s"),data); if (len>0) break; Sleep(100); } if (len>0) { CString str(data); m_LastResponese = str; return TRUE; } return FALSE; } BOOL CMobile::ConnectMobile(int nPort, int Speed) { cs = new CSerial; return cs->Open(nPort,Speed); } void CMobile::DisConnectMobile() { cs->PortClose(); if( cs != NULL ) delete cs; } void CMobile::SendData(char *str) { char tosend[1024]; strcpy(tosend,str); strcat(tosend,"\r"); cs->SendData(tosend,strlen(tosend)+1); } BOOL CMobile::CloseEcho() { SendData("ATE0"); if (ReadResponse()) { return AnalysisResponse(); } return FALSE; } BOOL CMobile::AnalysisResponse() { int i; int fi,li; m_LastResponese.TrimLeft();m_LastResponese.TrimRight(); m_LastResponese += _T("\r\n"); //Dispart: m_LastQueryResult = _T(""); m_CommandResult = _T(""); for (fi=0,li=0,i=0;fi =0||m_LastQueryResult.Find(_T("OK"),0)>=0) return TRUE; else if (m_CommandResult.Find(_T("ERROR"),0)>=0||m_LastQueryResult.Find(_T("ERROR"),0)>=0) return FALSE; else if (m_CommandResult.Find(_T(">"),0)>=0||m_LastQueryResult.Find(_T(">"),0)>=0) return TRUE; else return FALSE; return FALSE; } CString CMobile::GetMobileFactory() { SendData("AT+CGMI"); if (ReadResponse()) { if (AnalysisResponse()) { return m_LastQueryResult; } } return _T(""); } CString CMobile::GetMobileModel() { SendData("AT+CGMM"); if (ReadResponse()) { if (AnalysisResponse()) { return m_LastQueryResult; } } return _T(""); } CString CMobile::GetMobileFactorySerial() { SendData("AT+GSN"); if (ReadResponse()) { if (AnalysisResponse()) { return m_LastQueryResult; } } return _T(""); } CString CMobile::GetMobileFirmwareVer() { SendData("AT+CGMR"); if (ReadResponse()) { if (AnalysisResponse()) { return m_LastQueryResult; } } return _T(""); } CString CMobile::GetMobileIMEI() { SendData("AT+CGSN"); if (ReadResponse()) { if (AnalysisResponse()) { return m_LastQueryResult; } } return _T(""); } BOOL GetBit(BYTE bitnum,BYTE data) { BOOL IsTrue = FALSE; switch (bitnum) { case 0: IsTrue = data & 0x01; break; case 1: IsTrue = data & 0x02; break; case 2: IsTrue = data & 0x04; break; case 3: IsTrue = data & 0x08; break; case 4: IsTrue = data & 0x10; break; case 5: IsTrue = data & 0x20; break; case 6: IsTrue = data & 0x40; break; case 7: IsTrue = data & 0x80; break; default: IsTrue = data & 0x01; break; } return IsTrue; } BYTE SetBit(BYTE bitnum,BYTE data,BOOL bit) { BYTE op; BYTE t = data; switch (bitnum) { case 0: op = 0x01; break; case 1: op = 0x02; break; case 2: op = 0x04; break; case 3: op = 0x08; break; case 4: op = 0x10; break; case 5: op = 0x20; break; case 6: op = 0x40; break; case 7: op = 0x80; break; default: op = 0x01; break; } if (bit) t = t | op; return t; } BOOL IfUnicode(char *Text) { int i,len; BYTE data[2048]; len = sizeof(Text); memcpy(data,Text,len); for (i=0;i > ((j) % 7); for (k=0;k<=j%7;k++) { coded[j] = SetBit(7-k,coded[j],GetBit((j%7)-k,data[i+1])); } j++; } int total = j; strcpy(output,""); char str[3]; for (i=0;i SendData(data,datalen); if (!ReadResponse()) return FALSE; if (!AnalysisResponse()) return FALSE; return TRUE; } BOOL CMobile::LockKeypad(BOOL bLock) { if (bLock) SendData("AT+CLCK=CS,1"); else SendData("AT+CLCK=CS,0"); if (ReadResponse()) { return AnalysisResponse(); } return FALSE; } BOOL CMobile::ShutdownMobile() { SendData("AT^SMSO"); if (ReadResponse()) { return AnalysisResponse(); } return FALSE; }