예제 #1
0
int main()
{
    cout<<"Open serial port."<<endl;
    int fd = serialOpen("/dev/ttyAMA0",115200);
    int datalength,dataflag;
    char ch;
    char buf[RX_BUF_SIZE];
    int  rx_buf_len;
    my.initDB("localhost" , "root", "root" , "" );
    while(1)
    {
        int data = serialDataAvail(fd);
	while (data > 0) {
            ch = serialGetchar(fd);
            cout<<ch;
            buf[rx_buf_len] = ch;
            if ((rx_buf_len > 2) && (ch == 0x0A && buf[rx_buf_len - 1] == 0x0D)) {
                //cout<<"FD:"<<fd<<". "<<"Start waiting."<<endl;
                buf[rx_buf_len - 1] = 0;
                cout<<"Data:"<<buf<<endl;
                char *data = buf;
                Data_Parse(data);
                rx_buf_len = 0;
                memset(buf, 0, sizeof(buf));
            } else {
                if (rx_buf_len < RX_BUF_SIZE) {
                    rx_buf_len++;
                }
            }
        data = serialDataAvail(fd);
        }
        usleep(80);
    }
    serialClose(fd);
    return 0;
}
int main(){
	//互斥锁初始化
	pthread_mutex_init(&mutexsql, NULL);
	//-----SQL更新线程--------
	pthread_t sqlupdatethread;
	pthread_create(&sqlupdatethread,NULL,sqlupdate_start,(void *)0);
	//-----打印当前时间-------
	GetTime();
	//-----mysql初始化--------
	int row=0;
	int col=0;	
    MySqlDB.initDB("localhost" , "root", "xiabo891219" , "r114" );//链接数据库
	char***r = MySqlDB.executeSQL("select * from people;",&row,&col);//打印所有信息
	Free3point(r,row,col);
	char***r1 = MySqlDB.executeSQL("select * from opentime;",&row,&col);//打印所有信息
	Free3point(r1,row,col);
	//MySqlDB.executeSQL("select peopleworkbegin from people where idpeople>0;",&row,&col);//打印所有信息
	//MySqlDB.executeSQL("update people p set p.peopletimetotal=0,p.peopletodayworktime=0 where idpeople>0;",&row,&col);
	printf("\n");
	//-----socket-------------
    int listenfd,connectfd;
    struct sockaddr_in server;
    struct sockaddr_in client;

    if(-1==(listenfd=socket(AF_INET,SOCK_STREAM,0))){
        perror("create socket error\n");
        exit(1);
    }

    int opt=SO_REUSEADDR;
    setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT);
    server.sin_addr.s_addr=htonl(INADDR_ANY);

    if(-1==(bind(listenfd,(struct sockaddr*)&server,sizeof(struct sockaddr)))){
        perror("bind error\n");
        exit(1);
    }

    if(-1==(listen(listenfd,BACKLOG))){
        perror("listen error\n");
        exit(1);
    }

    socklen_t len=sizeof(client);
    targs arg;

    while(1){
        if(-1==(connectfd=accept(listenfd,(struct sockaddr *)&client,&len))){
            perror("accept error\n");
            exit(1);
        }
        arg.connectfd=connectfd;
        arg.client=client;

        pthread_t tid;
        if(pthread_create(&tid,NULL,process,(void *)&arg)){
            perror("create thread error\n");
            exit(1);
        }
    }
    close(listenfd);
    return 0;
}