Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3044038
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2018-06-07 16:47:54

一、实验目的:

SIM71004G模块使用Linux C语言实现打电话发短信.


二、实验平台:

迅为itop4412开发板(运行Linux最小系统),SIM7100 4G模块.


三、实验流程

(1)串口编程

串口设置其实就相当于串口通信的协议: 主要是初始化设置。
波特率:是为了两者信号流能同步,;
数据位:是指又几位数据封装成一帧 ;
结束位:是指以帧传输数据时,协定好结束位,便于提取有效数据 ;
奇偶校验:检验数据的一种手段 .

(2)实现代码:

#include  #include  #include  #include  #include  #include  #include  #include  #include  int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop); int send_message(int fd); int call_number(int fd); int main (int argc, char **argv)
{ int fd; char select;

    fd = open( "/dev/ttyUSB2", O_RDWR|O_NOCTTY|O_NDELAY);/*ttyUSB2为SIM7100的AT指令口*/ if(fd<0){
        perror("Can't Open Serial Port"); return -1;
    } /*配置串口,波特率必须和4G模块的相同*/ set_opt( fd,115200,8,'N',1); printf("==========================================\n"); printf("gprs call number and send message\n"); printf("==========================================\n"); printf("enter your select:  's' is send message,  'c' is call number,   'q' is exit \n");

    select=getchar();/*去掉回车符*/ switch(select)
    { case 's':
                 send_message(fd); break; case 'c':
                 call_number(fd); break; case 'q': exit(0); break; default: break;
    }
    close(fd); return 0;
} /************************************************************
 * 功能: 串口参数配置
 * 输入参数: fd:open打开的文件描述符 nspeed:波特率 nBits:数据位数 
 * nEvent:奇偶校验 nStop:停止位

 ***********************************************************/ int set_opt(int fd,int nSpeed,int nBits,char nEvent,int nStop)
{ struct termios newttys1,oldttys1; if(tcgetattr(fd,&oldttys1)!=0) /*保存原先串口配置*/ {
        perror("Setupserial 1"); return -1;
    }

bzero(&newttys1,sizeof(newttys1)); /*将一段内存区域的内容全清为零*/ newttys1.c_cflag|=(CLOCAL|CREAD ); /*CREAD 开启串行数据接收,CLOCAL并打开本地连接模式*/ newttys1.c_cflag &=~CSIZE; /*设置数据位数*/ switch(nBits) /*选择数据位*/ { case 7:
                newttys1.c_cflag |=CS7; break; case 8:
                newttys1.c_cflag |=CS8; break;
 } switch( nEvent ) /*设置校验位 */ { case '0': /*奇校验  */ newttys1.c_cflag |= PARENB; /*开启奇偶校验 */ newttys1.c_iflag |= (INPCK | ISTRIP); /*INPCK打开输入奇偶校验;ISTRIP去除字符的第八个比特*/ newttys1.c_cflag |= PARODD; /*启用奇校验(默认为偶校验)*/ break; case 'E' : /*偶校验  */ newttys1.c_cflag |= PARENB; /*开启奇偶校验 */ newttys1.c_iflag |= ( INPCK | ISTRIP); /*打开输入奇偶校验并去除字符第八个比特  */ newttys1.c_cflag &= ~PARODD; /*启用偶校验;  */ break; case 'N': /*关闭奇偶校验*/ newttys1.c_cflag &= ~PARENB; break;
   } switch( nSpeed ) /*设置波特率 */ { case 2400:
                 cfsetispeed(&newttys1, B2400); /*设置输入速度*/ cfsetospeed(&newttys1, B2400); /*设置输出速度*/ break; case 4800:
                 cfsetispeed(&newttys1, B4800);
                 cfsetospeed(&newttys1, B4800); break; case 9600:
                 cfsetispeed(&newttys1, B9600);
                 cfsetospeed(&newttys1, B9600); break; case 115200:
                 cfsetispeed(&newttys1, B115200);
                 cfsetospeed(&newttys1, B115200); break; default:
                 cfsetispeed(&newttys1, B9600);
                 cfsetospeed(&newttys1, B9600); break;
     } if( nStop == 1) /*设置停止位;若停止位为1,则清除CSTOPB,若停止位为2,则激活CSTOPB*/ {
        newttys1.c_cflag &= ~CSTOPB; /*默认为送一位停止位; */ } else if( nStop == 2)
     {
        newttys1.c_cflag |= CSTOPB; /*CSTOPB表示送两位停止位;*/ } /*设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时*/ newttys1.c_cc[VTIME] = 0; /*非规范模式读取时的超时时间; */ newttys1.c_cc[VMIN]  = 0; /*非规范模式读取时的最小字符数;*/ tcflush(fd ,TCIFLUSH); /*tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */ /* 在完成配置后,需要激活配置使其生效*/ if((tcsetattr( fd, TCSANOW,&newttys1))!=0) /*TCSANOW不等数据传输完毕就立即改变属性*/ {
         perror("com set error"); return -1;
     } return 0;
} /**************************************************************************************
 *  功能:sim7110模块打电话
 *  实现:ATD+号码+;
 *************************************************************************************/ int call_number (int fd)
{
    getchar(); /*将缓冲区回车吃掉,输入c后的回车*/ int count=0; char call[20]="atd"; /*打电话的AT指令 */ char number[20]; char reply[128]; printf("enter you call number\n"); if(NULL==fgets(number,20,stdin)) /*输入电话号码,其中fgets在读入一个字符串后在字符串尾端默认加入\n字符*/ exit(0); /*这个语句的功能可以用gets实现,区别在于 fgets 读入的含 "\n"(最后一个字符),gets 不含 "\n"*/ while(strlen(number)!=12)           
    { printf("please again number\n"); if(NULL==fgets(number,20,stdin)) exit(0); if(count==3) exit(0);
        count++;
    }

    number[strlen(number)-1]='\0'; /*将刚才fgets读入的字符串尾端去除\n字符*/ strcat(call,number); strcat(call,";\r"); /* \r是换行字符*/ write(fd,call,strlen(call)); /*向串口拨打号码*/ printf("write %s\n",call);
    sleep(3); memset(reply,0,sizeof(reply));
    read(fd,reply,sizeof(reply)); /*AT指令*/ printf("%s\n",reply); printf("=================================================\n"); printf("number is calling,please press 'a' hang up \n"); printf("=================================================\n"); while('a'!=getchar()) printf("please again input 'a' to hung up \n"); memset(call,0,sizeof(call)); strcpy(call,"ATH\r"); 
    write(fd,call,strlen(call));
    sleep(1); memset(reply,0,sizeof(reply));
    read(fd,reply,sizeof(reply)); /*数据*/ printf("%s\n",reply); printf("has hung up\n"); return 0;
} /****************************************
 *  功能:SIM7100模块发短信函数
 *  实现:AT+CMGF=1 (回车)                
      AT+CMGS="号码" (回车)    
     >   (Ctrl + z)
 ****************************************/ int send_message (int fd)
{ int count=0; char cmgf[]="at+cmgf=1\r"; /*设置短信发送模式为text (0-PDU;1-text)*/ char cmgs[128]="at+cmgs=\""; /*AT+CMGS="号码"  \:连接字符串*/ char send_number[16]; /*电话号码*/ char message[128]; char reply[128];

    getchar(); /*吃掉缓冲区回车,输入s后的回车*/ printf("enter send_message number :\n"); if(NULL==fgets(send_number,16,stdin)) exit(0); while(12!=strlen(send_number))
    {
        getchar(); printf("please again input number\n"); if(NULL==fgets(send_number,16,stdin)) exit(0); if(count==3) exit(0);
        count++;
    }

    send_number[strlen(send_number)-1]='\0'; /*去除字符串末端读入的换行符\n;*/ strcat(cmgs,send_number); strcat(cmgs,"\"\r"); /*cmgs:at+cmgs="号码"*/ printf("enter send_message :\n"); if(NULL==fgets(message,128,stdin)) exit(0);

    message[strlen(message)-1]='\0'; /*"\x1a对应的控制字符为:SUB:意为后面只跟可打印输出的字符*/ strcat(message,"\x1a"); /*可以打印的*/ /* write 模式 */ write(fd,cmgf,strlen(cmgf)); printf("write %s\n",cmgf);
    sleep(2); memset(reply,0,sizeof(reply));
    read(fd,reply,sizeof(reply)); printf("%s\n",reply); /* write 命令 */ write(fd,cmgs,strlen(cmgs)); printf("write %s\n",cmgs);
    sleep(5); /*write 信息*/ write(fd,message,strlen(message)); printf("write %s\n",message);
    sleep(4); memset(reply,0,sizeof(reply));
    read(fd,reply,sizeof(reply)); printf("%s\n",reply); return 0;
} 
  • 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
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
(3)编译测试:

将dial.c文件拷贝到ubuntu下,在该目录下使用:
arm-none-linux-gnueabi-gcc -o dial dial.c -static 进行编译,得到编译出来的dial执行文件,使用U盘拷贝到开发板上。
chmod 777 dial :修改权限
./dial :执行
可以得到:
这里写图片描述

发短信执行成功!!!打电话同样可以实现。

阅读(2621) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~