
之前有篇随笔整理了一种在裸机下用状态机+超时的机制来操作gprs/gsm,linux下就不需要了,本身有完善的调度机制,在等待的流程中直接sleep就行了。
下面是飞凌OK6410下的 demo, 其他基于linux的硬件平台都是一样操作,与前面那篇裸机状态下的处理作个对比。
main.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <termios.h> #include <unistd.h> #include <time.h> #include <errno.h> #include <string.h> #ifndef __TTY_H__ #define __TTY_H__ int tty_init(); int tty_end(); int tty_write(char *buf,int nbytes); int tty_writecmd(char *buf,int nbytes); int tty_read(char *buf,int nbytes); #endif #ifndef __GPRS_H__ #define __GPRS_H__ void gprs_init(); void gprs_msg(); void gprs_read(); #endif #define BAUDRATE B9600 #define COM0 "/dev/ttySAC0" #define COM1 "/dev/ttySAC1" #define COM2 "/dev/ttySAC2" static int fd; static struct termios oldtio,newtio; #define ENDMINITERM 27 #define FALSE 0 #define TRUE 1 volatile int STOP=FALSE; int GET_GPRS_OK=FALSE; int baud=B9600; int tty_end() { tcsetattr(fd,TCSANOW,&oldtio); close(fd); } int tty_read(char *buf,int nbytes) { return read(fd,buf,nbytes); } int tty_write(char *buf,int nbytes) { int i; ; i<nbytes; i++) { write(fd,&buf[i],); usleep(); } return tcdrain(fd); } int tty_writecmd(char *buf,int nbytes) { int i; ; i<nbytes; i++) { write(fd,&buf[i],); usleep(); } write(fd,); sleep(); return tcdrain(fd); } int tty_init() { fd = open(COM1, O_RDWR ); //| O_NONBLOCK);// ) { perror(COM1); exit(-); } tcgetattr(fd,&oldtio); /* save current modem settings */ bzero(&newtio, sizeof(newtio)); newtio.c_cflag = baud | /*CRTSCTS |*/ CS8 /*| CLOCAL | CREAD */; newtio.c_iflag = IGNPAR | ICRNL; newtio.c_oflag = ; newtio.c_lflag = ICANON; newtio.c_cc[VINTR] = ; /* Ctrl-c */ newtio.c_cc[VQUIT] = ; /* Ctrl-\ */ newtio.c_cc[VERASE] = ; /* del */ newtio.c_cc[VKILL] = ; /* @ */ newtio.c_cc[VEOF] = ; /* Ctrl-d */ newtio.c_cc[VTIME] = ; newtio.c_cc[VMIN] = ; newtio.c_cc[VSWTC] = ; /* '\0' */ newtio.c_cc[VSTART] = ; /* Ctrl-q */ newtio.c_cc[VSTOP] = ; /* Ctrl-s */ newtio.c_cc[VSUSP] = ; /* Ctrl-z */ newtio.c_cc[VEOL] = ; /* '\0' */ newtio.c_cc[VREPRINT] = ; /* Ctrl-r */ newtio.c_cc[VDISCARD] = ; /* Ctrl-u */ newtio.c_cc[VWERASE] = ; /* Ctrl-w */ newtio.c_cc[VLNEXT] = ; /* Ctrl-v */ newtio.c_cc[VEOL2] = ; /* '\0' */ tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio);/*set attrib */ ; } void gprs_msg() { char a; }; char text[]="Hello!Witech!"; tty_write("at", strlen("at")); sleep(); write(fd,); sleep(); tty_write("at+cmgf=1", strlen("at+cmgf=1")); sleep(); write(fd,); sleep(); tty_write("at+cmgs=13730191959", strlen("at+cmgs=13730191959")); sleep(); write(fd,); sleep(); tty_write("Hello!Witech!", strlen("Hello!Witech!")); sleep(); tty_write(ctl, ); sleep(); printf("sending is end!!\n"); } int main(void) { void * retval; tty_init(); printf("wait for sending a message!\n"); fflush(stdout); printf("sending......\n"); gprs_msg(); printf("Bye-Bye!\n"); tty_end(); exit(); }
很久以前收集的几个demo,附件打包下载http://files.cnblogs.com/files/dong1/Linux_gprs.rar
end