if ( connect(sock, (struct sockaddr*)&addr, sizeof(addr)) != -1 )
{
printf("connect ok\n");
if((sockLen=send(sock,"connect ok",strlen("connect ok"),0))==-1)
{
printf("send error\n");
goto innererror;
}
while(1)
{
fd_set readfds;
tv.tv_sec = 0; //设置超时时间
tv.tv_usec = 50000;
FD_ZERO(&readfds);
FD_SET(sock,&readfds);
ret = select(sock+1,&readfds,NULL,NULL,&tv);
if(FD_ISSET(sock,&readfds)) //检测sock中有无数据
{
if((sockLen=recv(sock,chCmd,sizeof(chCmd),0))>0)
{
printf("chCmd=%s\n",chCmd);
gpio.port = E;
gpio.num = 0;
gpio.data = 1;
write(fd_gpio,&gpio,sizeof(struct gpio_config)); //485串口发送
write(fd_uart[GPRS_PORT485],chCmd,strlen(chCmd)); //将获取数据发送到主机
}
else if(!sockLen)
{
printf("%d client close",sockLen);
goto innererror;
}
}
else //网络无数据时检测485串口有无数据
{
bzero(string,1024);
gpio.port = E;
gpio.num = 0;
gpio.data = 0;
write(fd_gpio,&gpio,sizeof(struct gpio_config)); //485串口接收
tcflush(fd_uart[GPRS_PORT485],TCIOFLUSH);
read_datas_tty(fd_uart[GPRS_PORT485],string,0,200000);
if(strlen(string) != 0)
{
if((sockLen=send(sock,string,strlen(string),0))==-1)
{
printf("send error\n");
}
}
}
}
}
阅读(1973) | 评论(0) | 转发(0) |