Serial Port GPRS module programming in Linux

Source: Internet
Author: User

1. At + cmgf = 0 is set to PDU mode to send Chinese-encoded text messages

2. At + cmgs = Information Length

The length of the information is the recipient's number segment plus the decimal length of the Information encoding segment (I .e., from 1100 0d91 to the last length divided by 2)

3. send SMS

# Include <termios. h> // POSIX interrupt control Definition

# Include <stdio. h> // UNIX standard Io Definition
# Include <stdlib. h> // standard library Definition
# Include <unistd. h> // Linux Standard Function Definition
# Include <fcntl. h> // file control definition mainly completes file read/write operations in Serial Communication
# Include <string. h>
# Include <sys/types. h>
# Include <sys/STAT. h>

Struct pdu_info
{
Char cnswap [32]; // use the parity switch of the SMS center number
Char phswap [32]; // used for parity switching of the receiver's number
};

Struct message_info
{
Char cnnu [16]; // SMS center number
Char phnu [16]; // recipient's number
Char message [128]; // text message content
};
Int serial_init (int fd) // serial port Initialization
{
Printf ("init serial \ n ");
Struct termios options;
Tcgetattr (FD, & options); // obtain the parameters of the current serial port.
Options. c_cflag | = (clocal | cread); // enables receiving and enabling local states
Options. c_cflag & = ~ Crtscts; // cancels hardware flow control.
Options. c_cflag & = ~ Csize; // set the character size
Options. c_cflag | = cs8; // The data is 8 bits.
Options. c_cflag & = ~ Cstopb; // a stop bit
Options. c_cflag | = ignpar; // ignore parity errors
Options. c_oflag = 0; // specify the terminal control information.
Options. c_lflag = 0; // set local mode

Cfsetispeed (& options, b9600); // you can specify 9600 as the input baud rate.
Cfsetospeed (& options, b9600); // set the output baud rate to 9600
Tcsetattr (FD, tcsanow, & options); // activate the new configuration

Return 0;
}
Int send_high_message (int fd, struct message_info info) // the temperature exceeds the threshold.
{
Printf ("dealing \ n ");
Char cmgf [] = "0 ";
Char cmgs [4] = {'\ 0 '};
Char CH2 [] = "0891 ";
Char CH3 [] = "1100 ";
Char methane [] = "0d91 ";
Char CH5 [] = "000800 ";
Char MSG [] = "0e8b66544aff1a975e6cd551654fb5"; // the Chinese text message uncode code "Warning: the temperature exceeds the threshold"
Char final [128];

Struct pdu_info PDU;
Int Len;

Memset (final, 0, sizeof (FINAL ));
Strcpy (info. cnnu, "13010360500"); // The number of the SMS Center (region)
Strcpy (info. phnu, "15821132676"); // recipient's number

Swap (info. cnnu, PDU. cnswap); // parity of the SMS center number
Swap (info. phnu, PDU. phswap); // receiver number parity

Printf ("swapped \ n ");
Strcpy (final, CH2); // spliced 0891
Strcat (final, PDU. cnswap); // concatenate the SMS center code
Strcat (final, CH3); // splice 1100
Strcat (final, methane); // concatenate 0d91
Strcat (final, PDU. phswap); // concatenate the receiver Encoding
Strcat (final, CH5); // splice 000800
Strcat (final, MSG); // concatenate Text Message Content Encoding
Strcat (final, "\ x1a"); // Add end flag
Puts (final); // display the spliced Encoding
Printf ("string OK \ n ");

Len = strlen (CH3) + strlen (PDU. phswap) + strlen (methane) + strlen (CH5) + strlen (MSG );
Sprintf (cmgs, "% d", Len/2); // The decimal length of text message content encoding in the SMS center is assigned to cmgs.
Send (FD, cmgf, cmgs, final );

Return 0;
}

Int send_invade_message (int fd, struct message_info info) // illegal intrusion
{
Printf ("dealing \ n ");
Char cmgf [] = "0 ";
Char cmgs [4] = {'\ 0 '};
Char CH2 [] = "0891 ";
Char CH3 [] = "1100 ";
Char methane [] = "0d91 ";
Char CH5 [] = "000800 ";
Char MSG [] = "0e8b66544aff1a6e295ea68d856807"; // "Warning: illegal intrusion"
Char final [128];

Struct pdu_info PDU;
Int Len;

Memset (final, 0, sizeof (FINAL ));
Strcpy (info. cnnu, "13010360500 ");
Strcpy (info. phnu, "15821132676 ");

Swap (info. phnu, PDU. phswap );
Swap (info. cnnu, PDU. cnswap );

Printf ("swapped \ n ");
Strcpy (final, CH2 );
Strcat (final, PDU. cnswap );
Strcat (final, CH3 );
Strcat (final, methane );
Strcat (final, PDU. phswap );
Strcat (final, CH5 );
Strcat (final, MSG );
Strcat (final, "\ x1a ");
Puts (final );
Printf ("string OK \ n ");

Len = strlen (CH3) + strlen (PDU. phswap) + strlen (methane) + strlen (CH5) + strlen (MSG );
Sprintf (cmgs, "% d", Len/2 );

Send (FD, cmgf, cmgs, final );

Return 0;
}

Int swap (char number [], char swaped []) // exchange the parity between the SMS center number and the receiver number
{
Char character [] = "86 ";
Char TMP [16];
Int I;

Memset (swaped, 0, 32 );
Memset (TMP, 0, 16 );
Strcpy (swaped, number );
Strcat (swaped, "F"); // end splicing F
Strcat (starting, swaped); // starts with 86
Strcpy (swaped, callback );

For (I = 0; I <= (strlen (swaped)-1); I + = 2)
{
Printf ("swapping \ n ");
TMP [I + 1] = swaped [I];
TMP [I] = swaped [I + 1];
}
Strcpy (swaped, TMP );

Return 0;
}
Int send (int fd, char * cmgf, int * cmgs, char * message) // send
{
Printf ("sending \ n ");
Int nread, nwrite;
Char Buf [128];
Char reply [128];
Memset (BUF, 0, sizeof (BUF ));
Strcpy (BUF, "At \ r ");
// \ R press ENTER

Nwrite = write (FD, Buf, strlen (BUF); // All except \ 0 are sent to the serial port
Printf ("nwrite = % d, % s \ n", nwrite, Buf );

Memset (reply, 0, sizeof (reply ));
Sleep (1 );
Nread = read (FD, reply, sizeof (reply ));
Printf ("nread = % d, % s \ n", nread, reply );

Memset (BUF, 0, sizeof (BUF); // At + cmgf = 0 PDU mode Chinese text message sending
Strcpy (BUF, "At + cmgf = ");
Strcat (BUF, cmgf );
Strcat (BUF, "\ r ");
Nwrite = write (FD, Buf, strlen (BUF ));
Printf ("nwrite = % d, % s \ n", nwrite, Buf );

Memset (reply, 0, sizeof (reply ));
Sleep (1 );
Read (FD, reply, sizeof (reply ));
Printf ("nread = % d, % s \ n", nread, reply );

Memset (BUF, 0, sizeof (BUF); // At + cmgs = Sending character Length
Strcpy (BUF, "At + cmgs = ");
Strcat (BUF, cmgs );
Strcat (BUF, "\ r ");
Nwrite = write (FD, Buf, strlen (BUF ));
Printf ("nwrite = % d, % s \ n", nwrite, Buf );

Memset (reply, 0, sizeof (reply ));
Sleep (1 );
Nread = read (FD, reply, sizeof (reply ));
Printf ("nread = % d, % s \ n", nread, reply );

Memset (BUF, 0, sizeof (BUF); // send message content message
Strcpy (BUF, message );
Nwrite = write (FD, Buf, strlen (BUF ));
Printf ("nwrite = % d, % s \ n", nwrite, Buf );

Memset (reply, 0, sizeof (reply ));
Sleep (1 );
Nread = read (FD, reply, sizeof (reply ));
Printf ("nread = % d, % s \ n", nread, reply );

}
Int main (INT argc, char * argv [])
{
Int FD;
Int warning_type;
Struct message_info Info;

If (FD = open ("/dev/s3c2410_serial", o_rdwr | o_noctty | o_ndelay) =-1)
{
Perror ("connot open serial port 1 ");
}

Serial_init (FD );

Printf ("input 1 or 2 to send diffrent message \ n ");
Scanf ("% d", & warning_type );

Switch (warning_type)
{
Case 1: send_high_message (FD, Info); break; // the temperature exceeds the threshold.
Case 2: send_invade_message (FD, Info); break; // illegal intrusion
Default: break;
}

Close (FD );

Return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.