您的位置:首页 > 运维架构 > Linux

linux下串口gprs模块编程

2012-06-14 15:34 281 查看
1.AT+CMGF=0 设置为PDU模式发送中文编码短信

2.AT+CMGS=信息长度

信息长度为收信方号码段加上信息编码段十进制长度(即:从1100 0D91到最后的长度除以2)

3.发送短信

#include <termios.h>  //posix中断控制定义
#include <stdio.h>    //unix标准io定义
#include <stdlib.h>   //标准库定义
#include <unistd.h>   //linux标准函数定义
#include <fcntl.h>    //文件控制定义主要完成串口通信中文件读写操作
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

struct pdu_info
{
char cnswap[32];  //短信中心号码奇偶位交换用
char phswap[32];  //接收方号码奇偶位交换用
};

struct message_info
{
char cnnu[16];      //短信中心号码
char phnu[16];      //接收方号码
char message[128];  //短信内容
};
int serial_init(int fd)                    //串口初始化
{
printf ("init serial\n");
struct termios options;
tcgetattr(fd, &options);             //得到当前串口参数
options.c_cflag |= (CLOCAL | CREAD); //使能接收、使能本地状态
options.c_cflag &= ~CRTSCTS;         //取消硬件流控制
options.c_cflag &= ~CSIZE;           //设置字符大小
options.c_cflag |= CS8;              //数据为8位
options.c_cflag &= ~CSTOPB;          //一个停止位
options.c_cflag |= IGNPAR;           //忽略奇偶校验错误
options.c_oflag = 0;                 //指定终端控制信息
options.c_lflag = 0;                 //本地模式设置

cfsetispeed(&options, B9600);        //设置输入波特率为9600
cfsetospeed(&options, B9600);        //设置输出波特率为9600
tcsetattr(fd, TCSANOW, &options);    //激活新配置

return 0;
}
int send_high_message(int fd,struct message_info info) //温度超标
{
printf ("dealing\n");
char cmgf[] = "0";
char cmgs[4] = {'\0'};
char ch2[] = "0891";
char ch3[] = "1100";
char ch4[] = "0D91";
char ch5[] = "000800";
char msg[] = "0E8B66544AFF1A975E6CD551654FB5"; //中文短信uncode编码 “警告:温度超标”
char final[128];

struct pdu_info pdu;
int len;

memset(final, 0, sizeof(final));
strcpy(info.cnnu, "13010360500");  //短信中心的号码(归属地)
strcpy(info.phnu, "15821132676");  //接收方号码

swap(info.cnnu, pdu.cnswap);       //短信中心号码奇偶交换
swap(info.phnu, pdu.phswap);       //接收方号码奇偶位交换

printf ("swapped\n");
strcpy(final, ch2);                 //拼接0891
strcat(final, pdu.cnswap);          //拼接短信中心编码
strcat(final, ch3);                 //拼接1100
strcat(final, ch4);                 //拼接0D91
strcat(final, pdu.phswap);          //拼接接收方编码
strcat(final, ch5);                 //拼接000800
strcat(final, msg);                 //拼接短信内容编码
strcat(final, "\x1a");              //添加结束标志
puts(final);                        //显示拼接好的编码
printf ("string ok\n");

len = strlen(ch3) + strlen(pdu.phswap) + strlen(ch4) + strlen(ch5) +strlen(msg);
sprintf (cmgs, "%d", len/2);    //短信中心加短信内容编码的十进制长度赋值给cmgs
send(fd, cmgf, cmgs, final);

return 0;
}

int send_invade_message(int fd, struct message_info info) //非法入侵
{
printf ("dealing\n");
char cmgf[] = "0";
char cmgs[4] = {'\0'};
char ch2[] = "0891";
char ch3[] = "1100";
char ch4[] = "0D91";
char ch5[] = "000800";
char msg[] = "0E8b66544AFF1A6E295EA68D856807"; //”警告:非法入侵“
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, ch4);
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(ch4) + strlen(ch5) +strlen(msg);
sprintf (cmgs, "%d", len/2);

send(fd, cmgf, cmgs, final);

return 0;
}

int swap(char number[], char swaped[])   //短信中心号码与接收方号码奇偶位交换
{
char ch1[] = "86";
char tmp[16];
int i;

memset(swaped, 0, 32);
memset(tmp, 0, 16);
strcpy(swaped, number);
strcat(swaped, "F");                 //结尾拼接F
strcat(ch1, swaped);                 //开头拼接86
strcpy(swaped, ch1);

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)   //发送
{
printf ("sending\n");
int nread, nwrite;
char buf[128];
char reply[128];
memset(buf, 0, sizeof(buf));
strcpy(buf, "at\r");                     //\r 回车

nwrite = write(fd, buf, strlen(buf));      //除\0全部发送到串口
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模式中文短信发送
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=发送字符长度
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));                  //发送消息内容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_serial2", 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;    //温度超标
case 2: send_invade_message(fd, info); break;  //非法入侵
default:break;
}

close(fd);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: