A few days ago, I feel that I practiced VC, so I thought, I usually send a text message so tired, my mobile phone keyboard is not easy, and I have a data line, why can't I send SMS directly? If you think about it, you will start to find information!
Since the procedures are involved, it is only the key main part.
First, I have to understand what communication between the mobile phone and the computer, my mobile phone is an infrared interface, and the computer is connected to an infrared adapter, you can connect with the phone, and Windows is to treat the infrared equipment as a serial port. Therefore, the key is how to use a program to control the COM port to send and receive data. I have found a lot of information online and then start writing code:
Enumeral all kinds of serial ports in the system: This requires an operational registry to implement, the code is as follows:
Void csendmsgdlg :: getAllcom () {HKEY HKEY; long ret; osversioninfo osvi; bool bosversioninfoEx; char keyinfo [100], comm_name [200], valuename [200]; int i; dword style, reserved, cbdata, cbvaluename;
ZeroMemory (& osvi, sizeof (OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); memset (keyinfo, 0,100); strcpy (keyinfo, "HARDWARE // DEVICEMAP // SERIALCOMM"); i = 0; sType = REG_SZ; Reserved = 0; bOsVersionInfoEx = GetVersionEx (& osvi); ret = RegOpenKeyEx (HKEY_LOCAL_MACHINE, keyinfo, 0, KEY_ALL_ACCESS, & hKey); if (ret == ERROR_SUCCESS) {if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) {do {cbData = 200; cbValueName = 200; MEMSET (Comm_name, 0,200); MEMSET (Valuename, 0,200); RET = RegenumValue (HKEY, I, VALUENAME, & CBVALUENAME, NULL, & Stype, (lpbyte) Comm_name, & CBData; if (Ret == Error_Success) {/ /m_list.add(comm_name); m_comm.addstring (comm_name); i ;}} while (RET == Error_Success);}} RegcloseKey (HKEY);
Get all the serial ports, now you should open the serial port. Generally speaking, there are two serial ports yourself, and the flag of the infrared line is generally COM3.
Open the code of the serial port:
Void csendmsgdlg :: onbutton1 () {char str [100]; DWORD DWTHREADID; MEMSET (STR, 0, 100); int SEL = m_comm.getcursel (); if (SEL == -1) {AFXMESSAGEBOX ("Sorry, please select one ! Interface "); return;} m_comm.GetLBText (sel, str); m_hCom = CreateFile (str, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if (m_hCom == INVALID_HANDLE_VALUE) {AfxMessageBox (" I'm sorry , Connection failure! "); Return;
ASSERT (m_hCom = INVALID_HANDLE_VALUE!); SetCommMask (m_hCom, EV_RXCHAR | EV_TXEMPTY); // set event-driven type SetupComm (m_hCom, 1024,512); // set the input, size of the output buffer PurgeComm (m_hCom, PURGE_TXABORT | PURGE_RXABORT | Purge_txclear | purge_rxclear); // Clean net input, output buffer command commands commands; // Define the timeout structure, and fill in the structure
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF; CommTimeOuts.ReadTotalTimeoutMultiplier = 0; CommTimeOuts.ReadTotalTimeoutConstant = 0; CommTimeOuts.WriteTotalTimeoutMultiplier = 0; CommTimeOuts.WriteTotalTimeoutConstant = 5000; SetCommTimeouts (m_hCom, & CommTimeOuts); // set allows read and write operations timeout DCB dcb; GetcommState (M_HCOM, & DCB); // Read serial port original parameter setting dcb.baudrate = 9600; dcb.bytesize = 8; dcb.parity = noparity; dcb.stopbits = onestopbit; dcb.fbinary = true; dcb.fparity = false Setcommstate (M_HCOM, & DCB); // Serial port parameter configuration MEMSET (& M_Overlapped, 0, Sizeof (Overlapped)); MEMSET (& M_Overlapped, 0, Sizeof (overlapped);
m_overlappedread.hevent = CreateEvent (NULL, TRUE, FALSE, NULL); m_overlappedwrite.hevent = CreateEvent (Null, true, false, null); m_hexit = CreateEvent (null, null, false, null);
hCommWatchThread = CreateThread ((LPSECURITY_ATTRIBUTES) NULL, 0, (LPTHREAD_START_ROUTINE) CommWatchProc, this, 0, & dwThreadID); if (hCommWatchThread == NULL) {AfxMessageBox ( "Sorry, connection failed!"); return;}
ASSERT (hcommwatchthread! = Null);
m_bconnected = true;
m_type = 0;
Senddata ("AT / R / N", Strlen ("AT / R / N")); PurgeComm (M_HCOM, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
m_conn.enableWindow (false); m_view.enablewindow (true); m_send.enableWindow (true);}
Since a parameter "file_flag_overlapped" is set in CreateFile, communicate with the serial port in an asynchronous manner, so start a thread specifically as a thread that receives data:
Uint csendmsgdlg :: CommwatchProc (lParam Parm) {
Csendmsgdlg * DLG = (csendmsgdlg *) PARM; Handle M_Hcomdev = DLG-> M_HCOM; DWORD DWBYTESREAD; Char Buffer [8000]; DWORD RET;
While (True) {MEMSET (Buffer, 0,8000); int R = DLG-> ReadData (Buffer, 8000); // Trace ("% s", buffer); if (strlen (buffer)! = 0) DLG -> ProcessData (buffer); Sleep (1000);} return 0;}
Below is a function of reading and writing COM ports:
int CSendMsgDlg :: SendData (char * buffer, DWORD dwBytesWritten) {BOOL bWriteStat; DWORD dwBytesRead; bWriteStat = WriteFile (m_hCom, buffer, dwBytesWritten, & dwBytesWritten, & m_OverlappedWrite); if (! bWriteStat) {if (GetLastError () == ERROR_IO_PENDING) {WaitForsingleObject (m_overlappedread.hevent, 1000); return ((int) dwbytesread;} return (0);} return ((int) dwbytesread;}
int CSendMsgDlg :: ReadData (char * buffer, DWORD dwBytesRead) {BOOL bReadStatus; bReadStatus = ReadFile (m_hCom, buffer, dwBytesRead, & dwBytesRead, & m_OverlappedRead); if (! bReadStatus) {if (GetLastError () == ERROR_IO_PENDING) {WaitForSingleObject ( m_overlappedread.hevent, 1000); Return (INT) DWBYTESREAD;} return (0);} return ((int) dwbytesread);} Of course, the method here is not a unique choice. The above is mainly the operation of the COM port, which has nothing to do with the mobile phone and SMS, the following part is part of the mobile phone text message:
First, I know how to use the mobile AT command set, we need not much instructions now, only read SMS and send text messages.
There are a lot of information about other orders from the AT directive, there is no mention here.
Command: AT CMGL
Command Description: Get SMS list
Format example:
AT CMGL CMGL: 1, 1,, 380891683108200205f0240D91683128500474F7000850403191611200126211572856FE4E6699865B664E6054620021 CMGL: 2, 1,, 700891683108200205f0240D91683128500474F70008504031919113003262114E0076F45C31662F4E09597D5B66751F0020563F563F51765B9E6211662F57287BEE74039986770B4EBA5BB68DF3821E CMGL: 3, 1,, 640891683108200205f0240D91683128500474F70008504031913272002C5F53713667094E864E0D8FC790FD662F59274E005C0F59B959B94E864F608001725B8FD860F354035AE98349 CMGL: 4, 1,, 820891683108200205f0240D91683128500474F70008504031918284003E4F608D767D27542C4F6076848BFE542700206B6A5FC3773C8FD84E0D5C1100204E004F1A80015E0863D095EE4F604F6053EF522B556590FD4E0D4F1A0020 CMGL: 5, 1,, 1540891683108200205f0240D91683128500474F70008504031916344008690A34F60600E4E484E0D4ECE59345F0059CB542C002080AF5B9A662F5FEB80038BD54E864F60624D77406025002056F0559D5496556189814E0D5C3157507B2C4E006392572880015E08773C76AE5E954E0B5C314E0D56F04E860020621173B05728592959294E5F662F4EE5549655617EF46301751F547D554A89814E0D65E9776177404E86 CMGL : 6, 1,, 880891683108200205f0240D91683128500474F70008504031914422004490A34F605C3153BB4E70901A51FA9898768480015E08002089814E0D5C31627E4EBA66FF4F6080030020603B776189C95BB9661380D662404EE56211767D59294E0D7761 CMGL: 7, 1,, 630891683108701305f0240BA13178855898F10008503013011285202C4F60684C4E0A7684004C0049004E005500584E2D5348501F621162FF5BBF820D88C54E004E0B884C4E0DFF1F CMGL: 8, 1,, 620891683108200205f0240D91683128500474F70008504031913523002A54CE54404F6090A34E48806A660E597D597D770B4E66591A505A70B9989880AF5B9A5C3180FD8FC77684 CMGL: 9, 1,, 320891683108301105f0040D91683139116779F30008503013120565000C53EF4EE563A752364E865417
CMGL: 10, 1,, 360891683108301105f0040D91683139116779F300085030131285230010662F4E0D662F7F51901F5F886162554A CMGL: 11, 1,, 380891683108301105f0040D91683139116779F30008503013220031001259295440002090A3662F4E0D662F4E2D6BD2 CMGL: 12, 1,, 380891683108301105f0040D91683139116779F30008503013221055001290A36211548B529E554A91CD542F884C5417 CMGL: 16, 1,, 1510891683108705305f0040BA13178536816F3000850402090311220848FD979CD65F650194F6057284E0A73ED4E865427FF0C621160F395EE95EE4F604E004E9B4E8B60C530024F6053EF4EE5544A8BC9621153BB5E74672C79D1658779D159276982662F591A5C114E0A7EBF541730029EBB70E64F604E8630024F604E5F628A5F2068A6534E768475358BDD544A8BC962115427FF0C514D5F97621165E0804A
CMGL: 17, 1,, 1020891683108301105f0040D91683139116779F300085040306112230052636E8BF46C5F6D9B76840028003400305929653B514B59275B6682F18BED56DB7EA700290020633A597D7684002E002053EF4EE55E2E4F608BA1521260278FC77EA7002E00204F604E708BD598984E865417 CMGL: 18, 1,, 850891683108705305f0040BA13178536816F3000850404022206220424ECA59298BB25230534A8DEF5C316CA175354E86FF0C611F89C9602A602A768430024F604EE5540E8FD8662F53EB621155E654AA5427611F89C96BD48F834EB25207 CMGL: 20, 1,, 560891683108705505f0040D91683175804276F800085040508182950024534E4E3A76847EB34E9B9E1F4EBA5E26774088AB5B504E0A73ED3000771F768460506016 CMGL: 21, 1,, 410891683108701305f0040BA13178858581F80008504080905461201666534F1F300067094E8B76F86C42300056DE75358BDD CMGL: 22, 1,, 900891683108705505f0040D91683175804276F8000850408011033000464F60660E59297ED9623F4E1C62534E2A75358BDD300053F778016211665A4E0A544A8BC94F603000621190A353EF4EE54E0A7F514E8630004ECA592998864E867B148BB0672C CMGL: 23, 1., 660891683108705505f0040d91683175804276f80008504080223543002E0 031003300350030003700330031003400320036003253EB52185E086BCD30004F605E2E6211628A94B153E07740 CMGL: 24, 1,, 520891683108705505f0040D91683175804276F80008504080227571002000310036003500300020621173B057286CA194B1300089817B4953D15DE58D44 CMGL: 25, 1,, 380891683108705505f0040D91683175804276F8000850408022950400124F6073B057284E0D4F1A997F6B7B54273000 CMGL: 26, 1,, 480891683108705505f0040D91683175804276F80008504080322082001C534E4E3A898162DB4E2A5199006A00610076006176844F6067654E0D CMGL: 27, 1,, 440891683108200205f0240D91683128500474F7000850409000416200184F60771F5F3A4EE5540E53EF4E0D65629A9A62704F604E86
CMGL: 28, 1,, 740891683108200205f0240D91683128500474F700085040900081500036621165394E864EE5540E4E0D4F1A6B3A8D1F60A880014EBA5BB64E86002C4F606B3A8D1F621162114E5F4E0D4F1A62A560287684002E CMGL: 29, 1,, 820891683108200205f0240D91683128500474F70008504090002273003E62116DF14FE155846709558462A560766709607662A5002C5BF94E864F607ED9621163024E86591A5C115C0F65F64E864EC04E4865F650195230592A9633 CMGL: 30, 1,, 820891683108200205f0240D91683128500474F70008504090009222003E62118FD85F975929592963D091924F60002054CE4F608FD94E2A783481115B5000204F608D767D277ED9621163024E0A8FC751E05929621168C067E553BB CMGL: 31, 1,, 580891683108200205f0240D91683128500474F7000850409000535400266211521A6D825B8C630775326CB97B495E725B8C4E8662115C31776100204F60572873A95565 CMGL: 32, 1,, 1080891683108200205f0240D91683128500474F700085040900014410058776189C94E4B524D6D82630775326CB95C314F1A505A4E2A597D68A6800C4E1468A690FD4F1A5B9E73B04E0D4FE14F608BD58BD5002C62114E0D8DDF4F6073A94E86660E59298FD85F9765E98D7751FA53BB73A900380038 CMGL: 33, 1 , 76 0891683108200205f0240D91683128500474F7000850409000542200385FD84E864F60662F753776844E864E0D8FC76CA14E8B73B057286D41884C75374EBA6D82630775326CB90020621177414E0D5F00773C4E86 CMGL: 34, 1,, 1540891683108200205f0240D91683128500474F7000850401190207100864E00592954B14FE9676552304E0053E38BB8613F4E9565C1002C62115F2F4E0B81708BB84E864E2A613F8FD85F804E9591CC62544E2A786C5E01002E4F604E5F60F38BB8613F4F464F605F2F817065F64E0D5C0F5FC37FFB51654E9591CC002E621188AB60CA54464E86002C5583558381EA8BED9053003A00208FD8771F7075563F00200021 CMGL: 35, 1,, 660891683108705505f0040D91683175804276F80008504031418592002E653E4E2A00760073007376845B8988C565874EF65230670D52A156684E0A97623000628A57305740544A8BC96211
CMGL: 36, 1,, 420891683108200205f0240D91683128500474F70008504031815581001680017CCA6D82795E522B5FD87ED96211630200510051 CMGL: 37, 1,, 720891683108200205f0240D91683128500474F70008504031819571003454CE54DF4F608FD84E0A8BFE002C626B76F273ED5427003F770B676562116BCF592965E94E0A90FD5F9763D091924F604E006B21 CMGL: 38, 1,, 1580891683108705505f0040D91683157011065F00008504001227091008A84288FBE59C66D3E624B4E0B53BB89C25BDF654C60C5002C4E0D4E004F1A513F624B4E0B53065FD956DE6765002C752898DF6307548C4E2D63074F5C51FA0056578B624B52BF002C84288FBE59C69AD8517476848BF4201C62114EEC80DC52294E86003F201D624B4E0B8BF4201C522B4ED65988626F4E86002C5C31526954B14EEC4FE94E863002201D CMGL: 40, 1,, 340891683108200205f0240D91683128500474F70008504001226243000E84288FBE6BCD662F621190E84E0B a bit long Oh, this is just my reading from my phone Out of the data, after the coding, if you are interested, you can "translate" out to see what is :)
Recommend an address on the SMS PDU format:
http://shuixin13.mblogger.cn/posts/10087.aspx
The information here is already full, and all of the operations can be included.
OK, reading mobile phone text messages is not our main purpose, here is not Rosso, just take a closer study of the documentation in the SMS PDU format, basically there will be no problem, our main purpose is to send text messages, now focus Tell this part well:
Texting instruction format and encoding format can also refer to the address above, it is also very detailed, I am not here, the key problem is now, how to encode and decode according to the format.
First, let's establish a class to save the data you want to send. Each field is indicated by the corresponding type, and there is a "package" function, which is combined with each part.
class CMsgSend {public: CString GetMsgData (); void Pack (CString c_number, CString s_number, CString msg); CMsgSend (); virtual ~ CMsgSend (); CString m_strData; CString SCA; CString PDUType; CString MR; CString DA; CString PID CSTRING DCS; CSTRING VP; CSTRING UDL; CSTRING UD;}; where m_strdata saves the "package", please refer to the documentation of the meaning of the other member variables.
Pack is a package function, requires three parameters: SMS service center number, receive SMS mobile phone number and information content.
In addition, we also need a tool class to implement coding and decoding:
class CMyTools {public: static void HexToChar (CString sHex, char * p); // Convert hex character array static CString DeCodeChinese (CString strSrc); // Chinese decoded static CString EnCodeChinese (CString strSrc); // Chinese Code Static CString Swapconvert (CString Str); // Exchange, for example 1234567890 transform to 2143658709, what is the specific document static Byte Hextochar (CSTRING STR); // Hex STRING DecodeEnglish (CSTRING SRCSTR) /// - English Decoding STATIC CSTRING ENCODEENGLISH (CSTRING SRCSTR); // English Code}; cstring cmytools :: EncodeEnglish (CString srcstr) {CSTRING RESULT; BYTE CUR, C1, C2; CSTRING TMP; INT LEN, I = 0, J = 0; len = srcstr.getlength () - 1; result = "; while (i <= len) {c1 = srcstr.getat (i); if (i
CSTRING CMYTOOLS :: DecodeEnglish (CString srcstr) {cstring strdest, strdata; int N = 0, I, FLAG = 0, J = 0; strdata = "; int Len = srcstr.getlength (); for (i = 0; I LEN = LEN / 2 LEN / 2/8; for (i = 0; I CSTRING CMYTOOLS :: SwapConvert (cstring str) {cstring result; result = str; for (int i = 0; i CSTRING CMYTOOLS :: EncodeChinese (cstring strsrc) { CSTRING STRESULT = "" "" ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, For (i = 0; i MultibyToWideChar (CP_ACP, MB_PREComposed, (LPSTSTSTR) strsrc, len, wc, len / 2); for (i = 0; i Void CMyTools :: Hextochar (CString Shex, Char * P) {Unsigned Char Bytetohex [] = {'0', '1', '2', '3', '4', '5', '6', ' 7 ',' 8 ',' 9 ',' A ',' B ',' C ',' D ',' E ',' F '}; int Nlen = shex.getlength (); char * chbuf = P , // Record the first address of the memory block * PCH; CString STEMP, SLEFT, SRIGHT; For (int i = 0; i The coded decoded tool is all, now look at the "package" function: Void cmsgsend :: PACK (CString C_Number, CString S_Number, CString MSG) {Int Len; SCA = "00"; // PDUTTPE PDUTYPE = "31"; // mr mr = "00"; // da S_Number = S_NUMBER "F"; LEN = S_Number.getLength (); DA.Format ("% 2.2x81", LEN-1); DA = DA CMYTOOLS :: SwapConvert (s_number); // PID PID = "00"; // DCS DCS = "08"; // vp vp = "a7"; // UD UD = CMYTOOLS :: EncodeChinese (MSG); // UDL UDL.Format ("% 2.2x", ud.getlength () / 2); M_StrData = SCA PDUTYPE MR DA PID DCS VP UDL UD;} For example, I want to send one content to 1387598800 is Hello information, SMS center number is 8613800731500, we can call: Msg.Pack ("8613800731500", "Hello"); sendmsg = msg.getmsgdata (); M.Format ("AT CMGS =% D / R / N", (SendMsg.getLength () - MSG .Sca.getlength ()) / 2); Senddata (LPCTSTR) M, M.GetLength ()); M.Format ("% s% c", sendmsg, 26); Senddata ((LPSTR) LPCTSTR) M, M.GETLENGTH ()); specific encoding procedure The above documentation is already in detail, nor is it more. Here, because the reasons are omitted, the arctic principle is these, the core code is the above coded decoding thing, some code I also found it from the Internet and modified it. In fact, use AT instructions to do a lot of things, and even call them, there is no problem, and the AT directive to make the phone is very simple: ATD13875998800, as long as this instruction is sent, the phone will call 13875998800. However, after practice, I found that the called mobile phone could not answer normally. Tips to connect to the computer device to answer, how to implement it, there is no research .....