|
楼主 |
发表于 2015-5-22 19:04:03
|
显示全部楼层
本帖最后由 jiangdou 于 2015-5-22 19:08 编辑
A10 A20 A31 A80 RK3188 RK3288 都可以用
接上页- void read_port(void)
- {
- fd_set rd;
- int nread,retval;
- struct timeval timeout;
- FD_ZERO(&rd);
- FD_SET(fd,&rd);
- timeout.tv_sec = 3;
- timeout.tv_usec = 0;
- while(IsReceve == 1);
- retval = select(fd+1,&rd,NULL,NULL,&timeout);
- switch(retval)
- {
- case 0:
- //printf("No data input within 1 seconds.\n");
- break;
- case -1:
- perror("select:");
- break;
- default:
- if( (nread = read(fd,msg,1024))>0 )
- {
- IsReceve =1;
- //printf("%sReceiveMessage: %s\n",msg,datetime);
- //printf("\n%sReceive %d bytes,Message is:\n%s\n",datetime,nread,msg);
- sprintf(buff, "%s", msg);//warning 去掉unsigned char buff[80] ->char buff[80]
- memset(msg,0,80);
- }
- break;
- }//end of switch
- }
- void create_thread(void)
- {
- int temp;
- memset(thread,0,sizeof(thread));
- if((temp = pthread_create(&thread[0],NULL,(void *)read_port,NULL)) != 0)
- printf("Create recv_thread failed!\n");
-
- }
- void wait_thread(void)
- {
- if(thread[0] != 0)
- {
- pthread_join(thread[0],NULL);//等待线程结束
- //printf("recev_thread end\n");
- }
- }
- //
- int rk3288_shut_down()//关机
- {
-
- show_error_logo();//显示SYSTEM ERROR!!
- sleep(6);//延时2S
- //xxx_xx();//关机命令
- //write(fd,"dou:shut#",9);//关机命令======="dou:shut#"
- for(;;){ //死循环
- sleep(3);
- }
- return 1;
- }
- //
- #define HOST_PORT 0
- //void keyID_parse()
- int main(void)
- {
- int j = 0;
- if((fd = Open_Port(HOST_PORT)) == -1)
- {
- perror("Open port");
- return -1;
- }
-
- if( Set_Port(fd,9600,8,'N',1) == -1)
- {
- perror("Set_Port");
- return -1;
- }
-
- //Serial_SendStr(fd,"Hello This is from Ubuntu\n");
- pthread_mutex_init(&mut,NULL);
- //###########################################################
- //取随机数,发送到MCU MCU解析dou:1234 a=1234 //keyID = (a * 2) - 3 send keyID to rk3288
- unsigned char x[2];
- unsigned int i;
- char buf[20];
- int local_ID;
- char read_buf[20];
- char *pp = read_buf;
- char *ppp = buff;// ->unsigned
- srand(time(0));
- i = rand();//取随机数
- x[0] = (char)((i >> 8) & 0xff);//16bit_height
- //printf("x[0] =%x xsize = %d\n", x[0], sizeof(x[0]));
- x[1] = (char)((i >> 24) & 0xff);//16bit_height
- //printf("x[1] =%x xsize = %d\n", x[1], sizeof(x[1]));
- sprintf(buf, "dou:%d#\r", ((x[1] << 8) | x[0]) & 0xffff);//sprintf(s, "", ,)//(((x[1] << 8) | x[0]) & 0xffff)
- //###########################################################
- while(1)//####
- {
- whi++;
- send_dou++;
- time(&now);
- tm_now = localtime(&now);
- datetime=asctime(tm_now);
-
- create_thread();
- wait_thread();//等待线程结束
-
- //printf("jiangdou while\n");
- //write(fd,"while...",8);
- usleep(3000);//毫秒延时
- write(fd,buf,strlen(buf));//key_id = "dou:1234#"
- if( IsReceve ==1)//表示有recv数据
- {
- //printf("Message is:%s\n",buff);//recv "dou:65151"
- //write(fd,"ifif...",7);
-
- bb = strstr(ppp, "dou:");//recv str = "dou:65151"
- if(bb != NULL){
-
- bb = strstr(ppp, ":");
- sprintf(read_buf, "%s",(bb + 1));
- key_id = StrToInt(pp);//key_id = 65151;
-
-
-
- if(reve_id == local_ID){
- //if(key_id == 2135){////"dou:2135\r\n");//收到KEY_ID
-
- goto go_on;// passwd success!!
-
- }else{
- //close(fd);
- ;
- if(send_dou > 5){
- rk3288_shut_down();//关机
- // passwd fali!!!
-
- }
-
- }
- }
-
-
- IsReceve = 0;
-
-
- }
-
-
- if(whi > 5){
- // fali!!! time out 5S!!
- whi = 0;
- //close(fd);
- rk3288_shut_down();//关机
- }
-
- }
- go_on:
- close(fd);
-
- return 0;
- }
复制代码 |
|