forked from MoeClub/Note
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendat.c
267 lines (239 loc) · 5.62 KB
/
sendat.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#ifndef _UART_H_
#define _UART_H_
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
int serial_init(char *device, int speed, int databits, int parity, int stopbits, int RTSCTS, int newline);
int serial_write(int fd, void *src, int len);
int serial_read(int fd, char *buf, int len);
//串口默认初始化接口
#define serial_default(device) serial_init(device, 1500000, 8, 'n', 1, 0, 1)
#endif
/**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄
*@param speed 类型 int 波特率
*@param databits 类型 int 数据位 取值为 7 或者8
*@param stopbits 类型 int 停止位 取值为 1 或者2
*@param parity 类型 char 效验类型 取值为N,E,O,S
*@param newline 类型 int 新的一行输出
*/
int set_parity(int fd, int speed, int databits, int parity, int stopbits, int RTSCTS, int newline)
{
struct termios options;
if ( tcgetattr(fd, &options) != 0) {
return -1;
}
cfsetispeed(&options, speed);
cfsetospeed(&options, speed);
options.c_cflag &= ~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
return -1;
}
options.c_iflag |= INPCK;
cfmakeraw(&options);
//options.c_lflag |= (ICANON | ECHO | ECHOE);
//options.c_lflag &= ~(ICANON | ECHO | ECHOE);
//options.c_iflag &= ~(IXON | IXOFF);
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
return -1;
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
return -1;
}
/* Set rts/cts */
if (RTSCTS)
{
options.c_cflag |= CRTSCTS;
}
if (newline)
{
options.c_lflag |= ICANON;
}
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd, TCSANOW, &options) != 0)
{
return -1;
}
// printf("set_Parity\n");
return 0;
}
/**
*@brief
*@param device 串口设备
*@param speed 串口速度
*@param databits,parity,stopbits,RTSCTS,分别为数据位,校验位,停止位,rtscts位
*@param newline 接收数据结尾是否加换行符?
*/
int serial_init(char *device, int speed, int databits, int parity, int stopbits, int RTSCTS, int newline)
{
int fd, ret;
fd = open(device, O_RDWR|O_NOCTTY);//O_NONBLOCK 非阻塞, O_WRONLY 只读写, O_RDONLY 只读, O_RDWR 读写,O_NOCTTY 阻塞
if (-1 == fd) {
fprintf(stderr, "ERROR: init %s failed\n", device);
return -1;
}
ret = set_parity(fd, speed, databits, parity, stopbits, RTSCTS, newline);
if (ret < 0) {
fprintf(stderr, "ERROR: set parity failed\n");
return -1;
}
return fd;
}
/**
*@brief
*@param fd 串口端口号文件描述符
*@param src 需要通过串口发送的数据
*@param len 需要发送的数据长度
*@param 成功返回0, 否则返回-1
*/
int serial_write(int fd, void *src, int len)
{
int ret = write(fd, src, len);
if (len != ret) {
fprintf(stderr, "ERROR: write serial failed!\n");
return -1;
}
return 0;
}
/**
*@brief
*@param fd 串口端口号文件描述符
*@param src 串口接收数据的指针
*@param len 需要接收的数据长度
*@param 成功返回0, 否则返回-1
*/
int serial_read(int fd, char *buf, int len)
{
int ret = read(fd, buf, len-1);
if (-1 == ret) {
fprintf(stderr, "ERROR: read serial failed!\n");
return -1;
}
buf[ret] = '\0';
return ret;
}
static void timeout()
{
fprintf(stderr, "No response from modem.\n");
exit(1);
}
/*字符包含判断*/
static int starts_with(const char* prefix, const char* str)
{
while(*prefix)
{
if (*prefix++ != *str++)
{
return 0;
}
}
return 1;
}
/*判断是否存在*/
int file_exist(const char* filename)
{
if (filename && access(filename, F_OK) == 0) {
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
if(argc != 3)
{
fprintf(stderr, "Usage: sendat 2 'ATI'\n");
return 1;
}
char device[16];
sprintf(device, "/dev/ttyUSB%s", argv[1]);
char *message= argv[2];
if (*message=='\0')
{
fprintf(stderr, "ERROR: AT Command Absent.\n");
return 1;
}
if(file_exist(device)==0)
{
fprintf(stderr, "ERROR: AT Device Absent.\n");
return 1;
}
signal(SIGALRM, timeout);
alarm(5);
char *rn= "\r\n";
char buff[1024];
int fd = serial_default(device);
if(fd < 0) return 1;
char *msg= strcat(message, rn);
serial_write(fd, msg, strlen(msg));
int ret=0;
while(1) {
int read = serial_read(fd, buff, sizeof(buff));
printf("%s", buff);
if(starts_with("OK", buff)) {
break;
}
if(starts_with("ERROR", buff)) {
ret=1;
break;
}
if(starts_with("COMMAND NOT SUPPORT", buff)) {
ret=1;
break;
}
if(starts_with("+CME ERROR", buff)) {
ret=1;
break;
}
if(starts_with("+CMS ERROR", buff)) {
ret=1;
break;
}
}
close(fd);
return ret;
}