📄 com_gprs.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "rs232.h"#include <stdio.h>#include <pthread.h>#include <stdlib.h>#include <string.h>#include <dirent.h>#include <unistd.h>#include <unistd.h>#include <fcntl.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/time.h>#include <unistd.h>#include <sys/socket.h>#include <linux/in.h>#include <malloc.h>#include <time.h>#include <semaphore.h>#define DEST_IP "211.82.91.59"#define DEST_PORT 9000#define N 10#define TRUE 1#define FALSE 0struct sockaddr_in dest_addr;int sockfd;char buf[1024];//used to receive the datachar ackbuf[10];//receive the ack from the pcchar combuf[256];//receive the command frome the pcint num;//how mang bytes by the comrd function actuallysem_t mutex,full,avail;int portno;FILE *fp;int over=FALSE;void *comgprsxc(void * arg);void *usbxc(void * arg); //---------------------the readcom1 function--------------------------------//when the com1 can be read,to receive the data and write to a file,the data is send out by the gprsvoid readcom1(void){ int i; time_t timep; struct tm *p; num=ComRd(0,buf,1024); if(num>0) { time(&timep); p=localtime(&timep);//get the time fp=fopen("/data.txt","a+"); if(fp=NULL) { perror("open the file wrong\n"); } for(i=0;i<(num);i++) { fprintf(fp,"%x",buf[i]); } fprintf(fp,"%d.%d.%d,%d:%d:%d\n",(1900+p->tm_year),(1+p->tm_mon),(p->tm_mday),p->tm_hour,p->tm_min,p->tm_sec); fclose(fp); over=sendgprs(buf);//over=1,end the send ComWrt(portno,buf,num);//send the com1 the data,used to debug }//end the if }//------------------------the read command by the gprs------------------//used to receice the command from the remote pc,the command send to the com1void readgprs(void) { char *overstr="over"; if(recv(sockfd,combuf,sizeof(combuf),0)==-1) { perror("can not receive the command\n"); }//end the if if(strcmp(combuf,overstr)==0) { over=TRUE; } ComWrt(portno,combuf,strlen(combuf));}//end the readgprs function//---------------------use the select function to receive the com1 and the gprs ------------------void *comgprsxc(void * arg){ int maxfd; fd_set rdfds; struct timeval tv; maxfd=ports[portno].handle>sockfd ? ports[portno].handle:sockfd; FD_ZERO(&rdfds); FD_SET(ports[portno].handle,&rdfds); FD_SET(sockfd,&rdfds); tv.tv_sec=2; tv.tv_usec=0;if(over==FALSE){ while(FD_ISSET(ports[portno].handle,&rdfds)||FD_ISSET(sockfd,&rdfds)) { if(select(maxfd+1,&rdfds,NULL,NULL,&tv)<0) perror("select"); else{ if(FD_ISSET(ports[portno].handle,&rdfds)) { sem_wait(&avail); sem_wait(&mutex); readcom1(); sem_post(&full); sem_post(&mutex); } if(FD_ISSET(sockfd,&rdfds)) { readgprs(); } }//end the select; }//end the while} pthread_exit(0);}//end the comgprsxc//----------------------the usb pthread used to save the data-----------------------------------void *usbxc(void * arg){ if(over==FALSE){ char *order="cp /data.txt /tem/";//tem or tmp 注意mount /dev/da1 /tmp sem_wait(&full); sem_wait(&mutex); system(order); sem_post(&avail); sem_post(&mutex);}//end the if pthread_exit(0);}//end the usbxc//------------------------------------main(int argc, char *argv[]){ int ret; int i; char *initok="gprs init ok!"; pid_t pr; int semret; //-------------------------------------- pthread_t usbid,com_gprsid; int usb_ret,comgprs_ret; //----------------- portno=0;//open the com 1 semret=sem_init(&mutex,0,1); if(semret!=0) { perror("sem_init"); } semret=sem_init(&avail,0,N); if(semret!=0) { perror("sem_init"); } semret=sem_init(&full,0,0); if(semret!=0) { perror("sem_init"); } pr=fork(); if(pr==0) {if(execlp("PPP-dial-OK","PPP-dial-OK",NULL)<0) perror("execlp error!"); }else{ waitpid(pr,NULL,0); sleep(20); initgprs(); ret=OpenCom(portno,"/dev/ttyS0",115200);//串口0; if(ret==-1) { perror("The /dev/ttyS0 open error."); exit(1); } //ComWrt(portno,initok,strlen(initok)); printf("initok!\n"); mount("/dev/sda1","/tmp","vfat",0xc0ed0000,NULL); //------------the read pthread comgprs_ret=pthread_create(&com_gprsid,NULL,comgprsxc,NULL); if(comgprs_ret!=0) { printf("create pthread error!\n"); exit(1); } //--------------the usb pthread usb_ret=pthread_create(&usbid,NULL,usbxc,NULL); if(usb_ret!=0) { printf("creat pthread error!\n"); exit(1); }//------------------------------------------------------------ pthread_join(com_gprsid,NULL); pthread_join(usbid,NULL);//----------------------------------------------------------- printf("over the send!\n"); if((ret=CloseCom(portno)==-1)) { perror("Close com"); exit(1); } close(sockfd); umount("/tmp"); }//end else}//end the main//-------------------------------------------- /************************************************************************Function:initgprs**Description:** creat a socket, and connect is; **** Arguments:none****return:0 init failed, 1 init success********************************************************************/int initgprs(){ int result; int count; dest_addr.sin_family=AF_INET; dest_addr.sin_port=htons(DEST_PORT); dest_addr.sin_addr.s_addr=inet_addr(DEST_IP); bzero(&(dest_addr.sin_zero),8); if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { printf("sockfd=%d",sockfd); perror("Error opening socket\n"); exit(1); } result=connect(sockfd,(void *)&dest_addr,sizeof(dest_addr)); while ((count<5)&&(result==-1)) { printf(" can not connect\n"); count++; result=connect(sockfd,(struct sockaddr *)&dest_addr,sizeof(struct sockaddr)); } if (count==5) { printf(" connect failed\n"); exit(-1); return 0; } return 1;}/************************************************************************Function:sendgprs**Description:** send a string **** Arguments:** data point to the string sended **return:0 --correct, continue recerive and send,1---end the programmer********************************************************************/int sendgprs(char *data){ int rr; int len; int m; int cmp; printf("in the send function! the first time\n"); if(send(sockfd,data,num,0)==-1) { printf("wrong!!\n"); perror("can not send message\n"); exit(1); } printf("right!\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -