Send a normal short message using the serial port

zhaozj2021-02-17  81

AT instruction document

SMS_PDU-Mode documentation

Short message text encoding the original code (download to PJ Naughter home page)

PDU encoding example:

The setting of the bit in // message can refer to the description in the document

// Red word represents different places in MFCSMS

CString csmsmessage :: createpduMessage () const {cbeyteaRay message; // message.setsize (0, 280);

// first Thing to do is work on the service center number (! Addencodedsmscnumber (message, m_sservicecenternumber, m_servicecenternumberaddresStype) Return_T ("");

// Form the SMS-SUBMIT octetBYTE bySMSSubmit = 0x1; // By default set this PDU to an SMS-SUBMITif (m_bUseValidityPeriod) {if (m_bUseRelativeValidityPeriod) bySMSSubmit | = 0x10; elsebySMSSubmit | = 0x18;} if (m_bRequireStatusReport) bySMSSubmit | = 0x20; message.add (bysmsSubmit);

// next is the tp-message-reference. We let the phone set the message.add (0x0);

// Next is the Phone numberASSERT (m_sPhoneNumber.GetLength ()); // Must specify a number to deliver the message toif (! AddEncodedPhoneNumber (message, m_sPhoneNumber, m_PhoneNumberAddressType)) return _T ( "");

// next is the protocol identifierMessage.Add (0x0);

// next is the data encoding scheme (this code always use pdu Encoding) message.add (0x8);

// Next is the validity periodif (m_bUseValidityPeriod) {if (m_bUseRelativeValidityPeriod) {// Relative Validity period is pseudo logarithmitly encoded into 1 octetBYTE byValidityPeriod = 0; if (m_RelativeValidityPeriod <= CTimeSpan (0, 12, 0, 0)) byValidityPeriod = (BYTE) ((m_RelativeValidityPeriod.GetTotalSeconds () / 300) - 1); else if (m_RelativeValidityPeriod <= CTimeSpan (1, 0, 0, 0)) byValidityPeriod = (BYTE) (((m_RelativeValidityPeriod.GetTotalSeconds () - 43200) / 1800) 143); else if (m_RelativeValidityPeriod <= CTimeSpan (30, 0, 0, 0)) byValidityPeriod = (BYTE) ((m_RelativeValidityPeriod.GetTotalSeconds () / 86400) 166); else byValidityPeriod = (BYTE) ( (m_RelativeValidityPeriod.GetTotalSeconds () / 604800) 192); message.Add (byValidityPeriod);} else {// Absolute Validity period is encoded into 7 octetsmessage.Add (SwappedNibble (m_AbsoluteValidityPeriod.wYear% 100)); message.Add ( Swappednibble (M_AbsoluteValidityPeriod.Wmonth); message.add (swappednibble (m_absolute (m_absolutevalidityperiod.wday); message.add (SwappedNibble (m_AbsoluteValidityPeriod.wHour)); message.Add (SwappedNibble (m_AbsoluteValidityPeriod.wMinute)); message.Add (SwappedNibble (m_AbsoluteValidityPeriod.wSecond));

Long nlocaltimezone = labs (m_timezone); byte bytimezone = swappednibble (nlocaltimezone / 15); if (m_timezone <0) ByTimezone | = 0x80; message.add (bytimezone);}}

// next is the length of the user dataint nMesetLength = m_sMessage.getLength (); int nactuallen = 0; for (int J = 0; j = 0 && nnibble <= 127) Nactuallen = Nactuallen 2; Elsenactuallen = Nactuallen 1;

} IF (NMESSAGALENGTH> 255) {trace ("csmsmessage :: createpduMessage, cannot Encode% s it is for 255 / n"), m_sMessage); return_t ("");} if NMessageLength == 0) {trace ("csmsmessage :: createpduMessage, failing as no message))); return_t (" ");} // initially we just add a place Holder As it will be updated later on once we have added the actual dataint nDataLengthIndex = message.GetSize (); message.Add ((BYTE) nActualLen); // Finally before we return from the function, convert from the BYTE array representation to the string versionWCHAR wcBuffer [256]; char cBuffer [256]; memset (cBuffer, 0,256); memset (wcBuffer, 0,256 * 2); MultiByteToWideChar (CP_ACP, 0, m_sMessage, nMessageLength, wcBuffer, 256); memcpy ((void *) cBuffer (void *) wcbuffer, 256); char TMP; for (int K = 0; k

For (k = 0; k > 4; if (nnibble1 > 9) pszdata [npduoutputindex] = (tchar) (nnibble1 - 10 _t ('a')); elsepszdata [npduoutputindex] = (tchar) (NNIbble1 _t ('0')); npduoutputIndex; int nbble2 = BYDATA & 0XF; if (nnibble2> 9) pszdata [npduoutputindex] = (tchar) (NNIBBLE2 - 10 _T ('a')); elsepszdata [npduoutputIndex] = (tchar) (nnibble2 _t ('0')); npduoutputindex;} SMSG.ReleaseBuffer (npduoutputIndex); return smsg;}

Use the AT command to send the end message example:

// Check to see letter portchar * pszatmsg = "at / R"; // Test connection successfully write (pszatmsg, strlen (pszatmsg)); if (! WaitforialResponse ("OK", DWTIMEOUT , 32)) {CSTRING SERROR; SERROR.LOADSTRING (IDS_SMSSERIALPORT_FAIL_RECEIVE_RESPONSE_FROM_MODEM); AFXTHROWSMSEXCEPTION (SERROR, E_SMS_NO_MODEM_RESPONSE);}

char * pszSelectMessageMsg = "AT CSMS = 0 / r"; // Select Message serviceWrite (pszSelectMessageMsg, strlen (pszSelectMessageMsg)); if (! WaitForSerialResponse ( "OK", dwTimeout, 32)) {CString sError; sError.LoadString ( IDS_SMSSERIALPORT_MODEM_DOES_NOT_SUPPPPORT_SMS); AfXTHROWSMSEXCEPTION (SERROR, E_SMS_NO_SMS_SUPPORT);}

char * pszMessageFormat = "AT CMGF = 0 / r"; // Set the transfer mode to PDUWrite (pszMessageFormat, strlen (pszMessageFormat)); if (WaitForSerialResponse ( "OK", dwTimeout, 32)!) {CString sError; sError .LoadString (IDS_SMSSERIALPORT_FAIL_SET_PDU_MODE); AfxThrowSMSException (sError, E_SMS_SET_PDU_MODE);} CString sSendCommand; sSendCommand.Format (_T ( "AT CMGS =% d / r"), nMsgSize); // Prepare to send the PDULPSTR pszAsciiSendCommand = T2A (( LPTSTR) (LPCTSTR) sSendCommand); Write (pszAsciiSendCommand, strlen (pszAsciiSendCommand)); if (WaitForSerialResponse ( ">", dwTimeout, 32)) {CString sError;! sError.LoadString (IDS_SMSSERIALPORT_FAIL_SEND_SMS_MESSAGE_TO_MODEM); AfxThrowSMSException (sError, E_SMS_SEND_MESSAGE_TO_MODEM); }

// And finally write out the PDU as a stringLPSTR pszAsciiPDU = T2A ((LPTSTR) (LPCTSTR) sData); int nAsciiPDULength = strlen (pszAsciiPDU); Write (pszAsciiPDU, nAsciiPDULength); // pszAsciiPDU Write a short message PDU encoder ( "/ x1A", 1); if (! WaitForSerialResponse ( "OK", dwTimeout, 32 nAsciiPDULength)) {CString sError; sError.LoadString (IDS_SMSSERIALPORT_FAIL_SEND_SMS_MESSAGE); AfxThrowSMSException (sError, E_SMS_SEND_MESSAGE);}

my home page

转载请注明原文地址:https://www.9cbs.com/read-31128.html

New Post(0)