Joke Collection Website - Blessing messages - How to use AT command to send SMS?
How to use AT command to send SMS?
# include & ltstdlib.h & gt
# include & ltunistd.h & gt
# include & ltsys/types . h & gt;
# include & ltsys/stat . h & gt;
# include & ltfcntl.h & gt
# include & lttermios.h & gt
# include & lt error number & gt
# include & lticonv.h & gt? //code conversion
# Definition? STTY_DEV? "/dev/ttyS0 "
# Definition? BUFF_SIZE? 5 12
# Definition? MSG_SIZE? 140// capacity of short messages
Charles? Tel [20] = "+8613476090094"; //Define the target mobile phone number for sending SMS.
Charles? SMS _ number[20]= "+86 13800200500 "; //Define the number of short message service centers.
Charles? Sms_gb23 12[MSG_SIZE]= "the best? Greetings! ”; ? //Define the content of the short message
Charles? SMS _ utf8[MSG _ SIZE];
Charles? * SMS _ in = SMS _ GB 23 12; //Convert the input gb23 12 code into unicode code.
Charles? * sms _ out = sms _ utf8
int? gb23 12_len,? utf8 _ len
int? SetOption(int? FD); ? //Set the parameters of serial communication
Invalid? TransPhone(); ? //Convert the cell phone number format
Invalid? trans MSC(); //Convert SMSC number
int? TransMsg(); ? //Convert SMS content
int? Master ()
{
int? stty_fd,n;
Charles? buffer[BUFF _ SIZE];
//Open the serial port
stty_fd=open(STTY_DEV,O _ RDWR);
if(- 1==stty_fd){
Perror ("open? Equipment ");
Return? 0;
}
Printf ("Open? Equipment? Success! \ n ");
//Set serial port parameters
If (0! =SetOption(stty_fd)){
close(stty _ FD);
Return? 0;
}
printf("Set? Serial? Options? Success! \ n ");
TransPhone();
trans MSC();
If (1! =TransMsg()){
Perror ("convert? short msg ");
close(stty _ FD);
Return? 0;
}
Printf ("Message? Convert? Success! \ n ");
//Set the PDU mode.
strcpy(buffer," AT+CMGF = 0 \ n "); //0 means PDU mode, 1 means text mode.
write(stty_fd,buffer,strlen(buffer)); //Write configuration command
n=read(stty_fd,buffer,BUFF _ SIZE); //After writing AT+CMGF=0 into the serial port, if the system supports PDU mode, return returns "OK" to the serial port.
If (n < = 0) {
Perror ("set? pdu? Mode ");
close(stty _ FD);
Return? 0;
}
If (0! =strncmp(buffer," OK ",2)){
Perror ("set? pdu? Mode ");
close(stty _ FD);
Return? 0;
}
//Send a message
sprintf(buffer," AT+CMGS=%d\n ",utf8 _ len); //Write a message command
write(stty_fd,buffer,strlen(buffer));
write(stty_fd,sms_utf8,utf8 _ len); //Write the message content
Printf ("Send? Leave a message All right! \ n ");
close(stty _ FD);
}
int? SetOption(int? fd)
{
struct? termios? opt
//Get the current serial port configuration
tcgetattr(FD & amp; opt);
tcflush(fd,TCIOFLUSH);
//Set the baud rate
cfsetispeed(& amp; opt,b 19200);
cfsetospeed(& amp; opt,b 19200);
//Set data bits -8 data bits
opt.c_cflag? & amp=? ~ CSIZE
opt.c_cflag? |=? CS8;
//Set parity-no parity.
opt.c_cflag? & amp=? ~ PARENB
opt.c_iflag? & amp=? ~ INPCK
//Set stop bit-1 stop bit
opt.c_cflag? & amp=? ~ CSTOPB
//Set timeout-15 seconds
opt . c _ cc[VTIME]= 150;
opt . c _ cc[VMIN]= 0;
//Set the writing device
If (0! =tcsetattr(fd,TCSANOW,& ampopt)){
Perror ("set? Podrat ");
Return? - 1;
}
tcflush(fd,TCIOFLUSH);
Return? 0;
}
Invalid? Microphone ()
{
int? I,str_len,tmp
If(phone[0]='+'){// Delete the'+'at the beginning of the number.
for(I = 0; I & ltstrlen (telephone)-1; I++)//strlen function gets the actual length of the character array, excluding the following' \0'.
Telephone [I]= telephone [I+1];
phone[I]= ' \ 0 ';
}
Str_len=strlen (telephone); ? //If the number (excluding'+') is odd, add' f' at the end.
if((strlen(phone)%2)! =0){
phone[str _ len]= ' F ';
phone[str _ len+ 1]= ' \ 0 ';
}
for(I = 0; I & ltstrlen (telephone); I+=2){// Exchange odd and even digits of numbers.
tmp = phone[I];
Telephone [I]= telephone [I+1];
Telephone [I+1] = tmp;
}
}
Invalid? TransSmsc()
{
int? I,str_len,tmp
TransPhone(SMS _ number);
str _ len = strlen(SMS _ number); ? //All characters are moved back two bytes.
for(I = strlen(SMS _ number)+2; Me! =0; I-)
SMS number [I]= SMS number [I-2];
SMS _ number[str _ len+3]= ' \ 0 ';
strncpy(sms_number," 9 1 ",2); ? //Start with "9 1".
tmp = strlen(SMS _ number)/2;
str _ len = strlen(SMS _ number); ? //All characters are moved back two bytes.
for(I = strlen(SMS _ number)+2; Me! =0; I-)
SMS number [I]= SMS number [I-2];
SMS _ number[str _ len+3]= ' \ 0 ';
SMS _ number[0]=(char)(tmp/ 10)+0x 30; ? //Convert the string length value from plastic to characters and write it at the beginning of the short message string.
SMS _ number[ 1]=(char)(tmp % 10)+0x 30;
}
int? TransMsg()
{
iconv_t? cd=iconv_open("utf-8 "," GB 23 12 "); //Set the type conversion GB 2312 = = > utf-8
if(0==cd){
Perror ("Create? iconv? Handle! ”);
Return? 0;
}
GB 23 12 _ len = strlen(SMS _ GB 23 12);
utf8 _ len = MSG _ SIZE
If(- 1==iconv(cd,& input & gb2312 _ len&; SMS _ out & amp; utf8_len)){
Perror ("convert? Code ");
Return? 0;
}
iconv _ close(CD);
Return? 1;
}
- Previous article:A summary of the idolized copy of high-quality friends circle (60 articles)
- Next article:Causes and solutions of crash
- Related articles
- Funny greetings for brothers' marriage
- 2022 Spring Festival greetings text messages to my girlfriend
- What does the tone include?
- Is it meaningful to send group blessings on WeChat?
- Why do I always get two identical messages?
- Terrible and romantic Valentine's Day greetings
- If someone else finds a watch, can they use it?
- 10 Summary of Visiting Consolation Work during the Spring Festival
- I successfully deducted100000 from the mobile phone WeChat payment change pass to the Shanghai Pudong Development Bank card. Why didn't I get the message that I received the money?
- My mobile phone is a flow card, so it costs very fast. I haven't received any text messages recently.