Liby's Blog

 汝鬼耶,抑人耶?

Linux下串口GPRS设备的短信读取发送与数据处理

with 2 comments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h> //标准输入输出定义
#include <stdlib.h> //标准函数库定义
#include <unistd.h> //Unix 标准函数定义
#include <fcntl.h> //文件控制定义
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h> //PPSIX 终端控制定义
#include <time.h>
#include <string.h>
#include <errno.h> //错误号定义
 
static int fd_gsm;
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
	struct termios newtio,oldtio;
	if  ( tcgetattr( fd,&oldtio)  !=  0) { 
		perror("SetupSerial 1");
		return -1;
	}
	bzero( &newtio, sizeof( newtio ) );
	newtio.c_cflag  |=  CLOCAL | CREAD;
	newtio.c_cflag &= ~CSIZE;
 
	switch( nBits )
	{
	case 7:
		newtio.c_cflag |= CS7;
		break;
	case 8:
		newtio.c_cflag |= CS8;
		break;
	}
 
	switch( nEvent )
	{
	case 'O':
		newtio.c_cflag |= PARENB;
		newtio.c_cflag |= PARODD;
		newtio.c_iflag |= (INPCK | ISTRIP);
		break;
	case 'E': 
		newtio.c_iflag |= (INPCK | ISTRIP);
		newtio.c_cflag |= PARENB;
		newtio.c_cflag &= ~PARODD;
		break;
	case 'N':  
		newtio.c_cflag &= ~PARENB;
		break;
	}
 
	switch( nSpeed )
	{
	case 2400:
		cfsetispeed(&newtio, B2400);
		cfsetospeed(&newtio, B2400);
		break;
	case 4800:
		cfsetispeed(&newtio, B4800);
		cfsetospeed(&newtio, B4800);
		break;
	case 9600:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	case 19200:
		cfsetispeed(&newtio, B19200);
		cfsetospeed(&newtio, B19200);
		break;
	case 115200:
		cfsetispeed(&newtio, B115200);
		cfsetospeed(&newtio, B115200);
		break;
	case 460800:
		cfsetispeed(&newtio, B460800);
		cfsetospeed(&newtio, B460800);
		break;
	default:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	}
	if( nStop == 1 )
		newtio.c_cflag &=  ~CSTOPB;
	else if ( nStop == 2 )
	newtio.c_cflag |=  CSTOPB;
	newtio.c_cc[VTIME]  = 0;//重要
	newtio.c_cc[VMIN] = 100;//返回的最小值  重要
 
/*	newtio.c_cc[VTIME] = 30;    //30 seconds
    	newtio.c_cc[VMIN] = 100;     //least bytes
    	newtio.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);//非规范模式
 
	newtio.c_cc[VTIME] = 30;    //30 seconds
	newtio.c_cc[VTIME] = 3;    //3 seconds
*/
	tcflush(fd,TCIFLUSH);
	if((tcsetattr(fd,TCSANOW,&newtio))!=0)
	{
		perror("com set error");
		return -1;
	}
	printf("set done!\n\r");
	return 0;
}
 
int read_info(char *buf)//rt3[100]
{
	int nux=0;
	nux=read(fd_gsm,buf,1);
 
	if(nux!=0)//如果读到非零0个数据
	{
		while(buf[0]!='\0')
		{		
			buf++;
			ir++;
			read(fd_gsm,buf,1);
			usleep(50);
		}
	}
	else
	{
		printf("read zero !\n");
	}
	tcflush(fd_gsm,TCIOFLUSH);//清空读写缓存//
	return 0;
}
 
int main(void)
{
	int set_gsm;
	int i=0;
	char reply[128];
	char data[123];
	char *d1=NULL;
	char *d2=NULL;
	char *d3=NULL;
	char *indx=NULL;
	char rdmsg[123]="AT+CMGR=";
	char cde[123];
 
 
 
 
	fd_gsm=open("/dev/ttyUSB0", O_RDWR|O_NONBLOCK);//打开串口
 
	if (fd_gsm==-1)
 
		exit(1);
 
 
 
	set_gsm=set_opt(fd_gsm,19200,8,'N',1);//设置串口属性
 
	if (set_gsm==-1)
 
		exit(1);
 
 
 
	write(fd_gsm,"AT\r",3);
	sleep(2);
	bzero(reply,128);
	read_info(reply);
	printf("%s\n",reply);
 
	write(fd_gsm,"AT+CMGF=1\r",strlen("AT+CMGF=1\r"));
	sleep(2);
	bzero(reply,128);
	read_info(reply);
	printf("%s\n",reply);
 
	write(fd_gsm,"AT+CNMI=2,1\r",strlen("AT+CNMI=2,1\r"));
	sleep(2);
	bzero(reply,128);
	read_info(reply);
	printf("%s\n",reply);
 
	while(1)
	{
		//read COM data
		bzero(reply,128);
		read_info(reply);
		printf("%d times reading %s\n",i++,reply);
 
		if(reply[2]=='+'&&reply[3]=='C')//数据有换行符,+CMTI: "MT",40,"+C"非第0元素开始
		{
			//sleep(2);
			indx=reply;
			indx=indx+14;
			printf("Index of MSG %s\n",indx);
			strcat(indx,"\r");//40 + \r
			strcat(rdmsg,indx);//at+cmgr= + 40 +\r 
			write(fd_gsm,rdmsg,strlen(rdmsg));
			sleep(2);
			bzero(reply,128);
			read_info(reply);
			printf("%s\n",reply);
 
			sscanf(reply,"%*[^\"]\"%*[^\"]\"%*[^\"]\"%*[^\"]\"%*[^\"]\"%*[^\"]\"%s",data);//获取短信内容
			printf("message:%s\n",data);
			d1=strtok(data,",");
			d2=strtok(NULL,",");
			d3=strtok(NULL,",");
			printf("d1 %s\nd2 %s\nd3 %s\n",d1,d2,d3);
		}
		sleep(2);
	}
 
/*
write(fd_gsm,"AT+CMGS=18888888888\r",strlen("AT+CMGS=18888888888\r"));
sleep(2);
bzero(reply,128);
read_info(reply);
printf("%s\n",reply);
 
write(fd_gsm,"12345",strlen("12345"));
sleep(2);
write(fd_gsm,"\x1a",1);
bzero(reply,128);
read_info(reply);
printf("%s\n",reply);
*/
	close(fd_gsm);//关闭设备文件
	return 0;
}

Written by liby

6 3rd, 2012 at 22:25

Posted in 技事巧事

Tagged with , , ,

2 Responses to 'Linux下串口GPRS设备的短信读取发送与数据处理'

Subscribe to comments with RSS or TrackBack to 'Linux下串口GPRS设备的短信读取发送与数据处理'.

  1. I think this web site has got some rattling great information for everyone. “Drunkenness is temporary suicide.” by Bertrand Russell.

  2. 写的太好了,谢谢你的无私。

    layjune

    6 8月 13 at 15:24

Leave a Reply