TA的每日心情 | 奋斗 2013-8-15 09:52 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]初来乍到
|
本帖最后由 dsgthlr 于 2014-5-4 11:40 编辑
如题,想用EMW3161模块做一个WIFI无线摄像头,现在摄像头图像采集已经做好,但是怎么通过这个模块传送,我想在官方给的例子上修改,摄像是用模块作为服务器采集好摄像头图像,当有客户机连接服务器时候就通过WIFI把图像传出去,现在问题是刚接触,对TCP不熟悉,不知道具体是个怎么调试方法。现在我想实现的功能就是利用TCP&UDP建立连接,连接到模块,然后TCP发送数据到模块,模块把接收到的数据发送到到TCP&UDP显示。现在连接是连接上了,PING了下也有连通。但是就是收不到数据。附上我在官方例子上修改的代码。- void MainTask(void)
- {
- int fd = -1;
- int fd_udp = -1;
- int len;
- int i, j;
- int clientfd[8]; //客户机
- int so = -1;
-
- struct sockaddr_t addr; //本地或端点地址
- fd_set readfds, exceptfds; //套接字集合,异常套接字集合
- struct timeval_t t;
- socklen_t addrLen;
- int con = -1;
- u8 *pSend;
- u8 *pBlock;
- u8 iErrCnt = 0;
- char bConnect = 0;
-
- #ifdef DynamicMemAlloc //动态SRAM分配
- int bufferSize;
- libConfig.tcp_buf_dynamic = mxEnable;
- lib_config(&libConfig);
- #endif
-
- mxchipInit(); //设置软件库运行参数
- InitNetData();
-
- readConfig(&configParas); //读取FLASH存取的网络SSID,KEY等,从0X08004000开始
- //OpenEasylink(60); //启动EASY LINKER配置功能
- if(0)//configSuccess)
- {
- int cnt = 12;
-
- SetupStation();
- while(wifi_up==0)
- {
- msleep(500);
- cnt--;
- if(cnt==0)
- break;
- }
- if(cnt==0)
- SetupSoftAP();
- else
- {
- //save SSID & Key from EasyLink to FLASH
- memcpy(configParas.wifi_ssid, wNetConfig.wifi_ssid, 32);
- memcpy(configParas.wifi_key, wNetConfig.wifi_key, 32);
- updateConfig(&configParas);
- }
- } else
- {
- if(configParas.wifi_ssid[0]==0) //如果没有网络连接,自己建热点
- SetupSoftAP();
- else//自动连入可连接网络
- {
- int cnt = 20;
-
- strcpy((char*)wNetConfig.wifi_ssid, configParas.wifi_ssid);
- strcpy((char*)wNetConfig.wifi_key, configParas.wifi_key);
- SetupStation();
- while(wifi_up==0)
- {
- msleep(500);
- cnt--;
- if(cnt==0)
- break;
- }
- if(cnt==0)
- {
- wlan_disconnect();
- SetupSoftAP();
- }
- }
- }
-
- InitCamera(); //初始化摄像头
-
- for(i=0;i<8;i++)
- clientfd[i] = -1;
-
- //<font color="Red">set_tcp_keepalive(3, 60); </font>//设置TCP保持连接的参数
- http_init();
- while(1)
- {
- mxchipTick(); //执行软件库的功能块
- http_tick();
- if (fd==-1)
- {
- fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //为通讯创建一个TCP协议端点,
- #ifdef DynamicMemAlloc
- bufferSize = 5*1024;
- //setsockopt(fd,0,SO_RDBUFLEN,&bufferSize,4);
- bufferSize = 8*1024;
- setsockopt(fd,0,SO_WRBUFLEN,&bufferSize,4);//设置套接字运行参数
- #endif
- addr.s_port = 8421;
- bind(fd, &addr, sizeof(addr)); //设定本地端口
- listen(fd, 0);
- }
- /*Establish a UDP(用户数据报协议) port to receive any data sent to this port*/
- if (fd_udp==-1)
- {
- fd_udp = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP);
- addr.s_port = 8090;
- bind(fd_udp, &addr, sizeof(addr));
- }
-
- FD_ZERO(&readfds); //清空&readfds 指针指向的套接字集合
- FD_SET(fd, &readfds); //在指针&readfds指向的套接字集合中增加套接字fd
- FD_SET(fd_udp, &readfds);
-
- for(i=0;i<8;i++)
- {
- if (clientfd[i] != -1)
- FD_SET(clientfd[i], &readfds);
- }
- t.tv_sec = 0;
- t.tv_usec = 0;
- select(1, &readfds, NULL, &exceptfds, &t);//获取端口套接字状态,是否收到数据
- if(FD_ISSET(fd, &readfds))//套接字是否被置位
- {
- j = accept(fd, &addr, &len); //接受远程设备连接请求
- if (j > 0)
- {
- for(i=0;i<8;i++)
- {
- if (clientfd[i] == -1)
- {
- clientfd[i] = j;
- break;
- }
- }
- }
- }
- for(i=0;i<8;i++)
- {
- if (clientfd[i] != -1)//有等待接收端点
- {
- <font color="Red">if(FD_ISSET(clientfd[i], &readfds))
- {
- con = recv(clientfd[i], buf, 1*1024, 0); //接收接收端点发送的数据
- send(clientfd[i], buf, strlen(buf), 0);<font face="宋体">//发送接收到的数据</font>
- /*if (con > 0)</font>
- {
- if((buf[0]=='G') && (buf[1]=='E') &&(buf[2]=='T') & (bConnect==0))
- {
- if(send(clientfd[i], cSendBack, strlen(cSendBack), 0)>0)
- {
- so = clientfd[i];
- bConnect = 1;
- Set_Read_CodecDataC();
- msleep(2);
- }
- }
- }
-
- else
- {
- close(clientfd[i]);
- if(clientfd[i]==so)
- {
- so = -1;
- bConnect = 0;
- }
-
- clientfd[i] = -1;
- }*/
- }
- else if (FD_ISSET(clientfd[i], &exceptfds))
- {
- if(clientfd[i]==so)
- {
- bConnect = 0;
- so = -1;
- }
-
- clientfd[i] = -1;
- }
- }
- }
-
- if(bConnect!=0)
- {
- if(CheckReadFinish()>0)
- {
- pSend = CodecSendBuffer + 3;
-
- pBlock = CodecRxBuffer;
- for(i=0;i < BLOCK_CNT; i++)
- {
- if(*pBlock==0xFF)
- {
- if(pBlock[1]==0)
- *pSend = 0xA0;
- else
- *pSend = 0xA1;
- }
-
-
- pSend++;
- pBlock += 2;
- memcpy(pSend,pBlock,BLOCK_LEN - 2);
-
- pSend += (BLOCK_LEN + 1);
- pBlock += BLOCK_LEN - 2;
- }
- Set_Read_CodecDataC(); //获取摄像头图像
- if(so!=-1) //发送摄像头数据
- {
- send(so, CodecSendBuffer, sizeof(CodecSendBuffer), 0);
- }
-
- iErrCnt = 0;
- }
- else
- {
- iErrCnt++;
- #if 0
- if(iErrCnt>20)
- {
- ResetCamera();
- iErrCnt = 0;
- }
-
- #endif
- Set_Read_CodecDataC();
- }
- }
- /*Read data from udp and send data back */
- if (FD_ISSET(fd_udp, &readfds))
- {
- con = recvfrom(fd_udp, buf, sizeof(buf), 0, &addr, &addrLen);
- con = DoUDPCmd(buf);
- if(con)
- sendto(fd_udp, buf, con, 0, &addr, sizeof(struct sockaddr_t));
- }
- }
- }
复制代码 保持TCP连接是不是客户机一直要发送应答信号?还有红色部分是我修改的,还有备注是参照PDF修改。求各位大神解答,目前我想实现的功能就是用TCP&UD工具连接到模块,实现双通信,跪求建议和解决方法
|
|