完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
问题描述:我在wlan_staion的代码增加了一些代码,主要是增加了BsdTcpClient代码,使其作为客户端
在wlan_ap的代码里增加了BsdTcpServer代码,使其成为服务器。其他一些必要的修改都已完成。针对BsdTcpClient中的 |
|
相关推荐
3 个讨论
|
|
一个黄人 发表于 2020-6-16 13:14 是的,我在getting_started_with_wlan_ap这个例子的基础上修改了ConfigureMode()和 WlanAPMode()函数 static int ConfigureMode(int iMode) [ char pcSsidName[33]="TP_LINK_478BE8"; long lRetVal = -1; //UART_PRINT("Enter the AP SSID name: "); // GetSsidName(pcSsidName,33); lRetVal = sl_WlanSetMode(ROLE_AP); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(pcSsidName), (unsigned char*)pcSsidName); ASSERT_ON_ERROR(lRetVal); UART_PRINT("Device is configured in AP mode: %snr",pcSsidName); /* Restart Network processor */ lRetVal = sl_Stop(SL_STOP_TIMEOUT); // reset status bits CLR_STATUS_BIT_ALL(g_ulStatus); return sl_Start(NULL,NULL,NULL); ] //**************************************************************************** // //! brief Opening a TCP server side socket and receiving data //! //! This function opens a TCP socket in Listen mode and waits for an incoming //! TCP connection. //! If a socket connection is established then the function will try to read //! 1000 TCP packets from the connected client. //! //! param[in] port number on which the server will be listening on //! //! return 0 on success, -1 on error. //! //! note This function will wait for an incoming connection till //! one is established // // int BsdTcpServer(unsigned short usPort) [ SlSockAddrIn_t sAddr; SlSockAddrIn_t sLocalAddr; //int iCounter; int iAddrSize; int iSockID; int iStatus; // int iNewSockID; //long lLoopCount = 0; long lNonBlocking = 1; // int iTestBufLen; //filling the TCP server socket address sLocalAddr.sin_family = SL_AF_INET; sLocalAddr.sin_port = sl_Htons((unsigned short)usPort); sLocalAddr.sin_addr.s_addr = 0; // creating a TCP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( iSockID < 0 ) [ // error ASSERT_ON_ERROR(SOCKET_CREATE_ERROR); ] iAddrSize = sizeof(SlSockAddrIn_t); // binding the TCP socket to the TCP server address iStatus = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize); if( iStatus < 0 ) [ // error sl_Close(iSockID); ASSERT_ON_ERROR(BIND_ERROR); ] // putting the socket for listening to the incoming TCP connection iStatus = sl_Listen(iSockID, 0); if( iStatus < 0 ) [ sl_Close(iSockID); ASSERT_ON_ERROR(LISTEN_ERROR); ] // setting socket option to make the socket as non blocking iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking)); if( iStatus < 0 ) [ sl_Close(iSockID); ASSERT_ON_ERROR(SOCKET_OPT_ERROR); ] iNewSockID = SL_EAGAIN; // waiting for an incoming TCP connection while( iNewSockID < 0 ) [ // accepts a connection form a TCP client, if there is any // otherwise returns SL_EAGAIN iNewSockID = sl_Accept(iSockID, ( struct SlSockAddr_t *)&sAddr, (SlSocklen_t*)&iAddrSize); if( iNewSockID == SL_EAGAIN ) [ MAP_UtilsDelay(10000); ] else if( iNewSockID < 0 ) [ // error sl_Close(iNewSockID); sl_Close(iSockID); ASSERT_ON_ERROR(ACCEPT_ERROR); ] ] UART_PRINT("rn SEND:ppp"); while(iFlag) [ osi_Sleep(100); ] // close the connected socket after receiving from connected TCP client iStatus = sl_Close(iNewSockID); ASSERT_ON_ERROR(iStatus); // close the listening socket iStatus = sl_Close(iSockID); ASSERT_ON_ERROR(iStatus); return SUCCESS; ] //**************************************************************************** // //! brief start simplelink, wait for the sta to connect to the device and //! run the ping test for that sta //! //! param pvparameters is the pointer to the list of parameters that can be //! passed to the task while creating it //! //! return None // //**************************************************************************** void WlanAPMode( void *pvParameters ) [ //int iTestResult = 0; unsigned char ucDHCP; long lRetVal = -1; InitializeAppVariables(); // // Following function configure the device to default state by cleaning // the persistent settings stored in NVMEM (viz. connection profiles & // policies, power policy etc) // // Applications may choose to skip this step if the developer is sure // that the device is in its default state at start of applicaton // // Note that all profiles and persistent settings that were done on the // device will be lost // lRetVal = ConfigureSimpleLinkToDefaultState(); if(lRetVal < 0) [ if (DEVICE_NOT_IN_STATION_MODE == lRetVal) UART_PRINT("Failed to configure the device in its default state nr"); LOOP_FOREVER(); ] UART_PRINT("Device is configured in default state nr"); // // Asumption is that the device is configured in station mode already // and it is in its default state // lRetVal = sl_Start(NULL,NULL,NULL); if (lRetVal < 0) [ UART_PRINT("Failed to start the device nr"); LOOP_FOREVER(); ] UART_PRINT("Device started as STATION nr"); // // Configure the networking mode and ssid name(for AP mode) // if(lRetVal != ROLE_AP) [ if(ConfigureMode(lRetVal) != ROLE_AP) [ UART_PRINT("Unable to set AP mode, exiting Application...nr"); sl_Stop(SL_STOP_TIMEOUT); LOOP_FOREVER(); ] ] while(!IS_IP_ACQUIRED(g_ulStatus)) [ //looping till ip is acquired ] unsigned char len = sizeof(SlNetCfgIpV4Args_t); SlNetCfgIpV4Args_t ipV4 = [0]; // get network configuration lRetVal = sl_NetCfgGet(SL_IPV4_AP_P2P_GO_GET_INFO,&ucDHCP,&len, (unsigned char *)&ipV4); if (lRetVal < 0) [ UART_PRINT("Failed to get network configuration nr"); LOOP_FOREVER(); ] UART_PRINT("Connect a client to Devicenr"); while(!IS_IP_LEASED(g_ulStatus)) [ //wating for the client to connect ] UART_PRINT("Client is connected to Devicenr"); lRetVal = BsdTcpServer(5001); if(lRetVal <0) [ UART_PRINT("TCP Server failedn/r"); ] // revert to STA mode lRetVal = sl_WlanSetMode(ROLE_STA); if(lRetVal < 0) [ ERR_PRINT(lRetVal); LOOP_FOREVER(); ] // Switch off Network processor lRetVal = sl_Stop(SL_STOP_TIMEOUT); // UART_PRINT("WLAN AP example executed successfully"); while(1); ] //*************************************************************************** //Sendtask function void SendTask(void *pvParameters) [ char cTxBuf[100]; char cGetChar; int iStatus; int iCounter=0; while(1) [ // uart reciver char cGetChar = MAP_UARTCharGetNonBlocking(UARTA0_BASE); if((cGetChar != 0xff)||(iNewSockID > 0)) [ //echo char MAP_UARTCharPut(UARTA0_BASE,cGetChar); //save reciver char cTxBuf[iCounter++] = cGetChar; //Enter(0x0d)or Esc(0x1b) if((cGetChar ==0x0d) || (cGetChar == 0x1b)) [ //echo and save line feed MAP_UARTCharPut(UARTA0_BASE,0x0a); cTxBuf[iCounter++] = 0x0a; //send tcp data package iStatus = sl_Send(iNewSockID,cTxBuf,iCounter,0); if((iStatus > 0)&& (cGetChar == 0x0d)) [ // enter UART_PRINT("send:"); iCounter = 0; ] else [ //send error or enter UART_PRINT("r senddata end nr"); iFlag = 0; ] ] ] osi_Sleep(100); ] ] //receive TCO data package void ReceiveTask(void *pvParameters) [ char cRxBuf[100]; int iStatus; while(1) [ if(iNewSockID > 0) [ //receiver TCP datapage iStatus = sl_Recv(iNewSockID,cRxBuf,100,0); if(iStatus>0) [ //enter if(cRxBuf[iStatus-2] == 0X0d) [ //save char end cRxBuf[iStatus]=0; UART_PRINT("nr receive:"); //SEND CHAR Message(cRxBuf); ] else [ //ESC UART_PRINT("nr receive date endnr"); iFlag = 0; ] ] ] osi_Sleep(100); ] ] 这里是作为服务器的。在getting_started_with_wlan_station例子里面做了相应更改 // wireless transfer int WuartTransfer(unsigned long ulBase,int iSockID) [ char cTxBuf[100]; char cRxBuf[100]; char cGetChar; int iStatus; int iCounter=0; UART_PRINT("rn send:hello wuart ap "); while(1) [ // uart reciver char cGetChar = MAP_UARTCharGetNonBlocking(ulBase); if(cGetChar != 0xff) [ //echo char MAP_UARTCharPut(ulBase,cGetChar); //save reciver char cTxBuf[iCounter++] = cGetChar; //Enter(0x0d)or Esc(0x1b) if((cGetChar ==0x0d) || (cGetChar == 0x1b)) [ //echo and save line feed MAP_UARTCharPut(ulBase,0x0a); cTxBuf[iCounter++] = 0x0a; //send tcp data package iStatus = sl_Send(iSockID,cTxBuf,iCounter,0); if(iStatus <= 0) [ // error processor ASSERT_ON_ERROR(sl_Close(iSockID)); UART_PRINT("send data errornr"); break; ] //enter if(cGetChar == 0x0d) [ UART_PRINT("send:"); iCounter = 0; ] //Esc else break; ] ] //receive TCO data package iStatus = sl_Recv(iSockID,cRxBuf,100,0); if(iStatus > 0) [ //Enter if(cRxBuf[iStatus-2] == 0x0d) [ //save char end (0x00) cRxBuf[iStatus] = 0; UART_PRINT("rn receive:"); //send char string Message(cRxBuf); UART_PRINT("SEND:"); ] else break; ] ] return(iStatus); ] //***************************************************************************** //! brief Opening a TCP client side socket and sending data //! //! This function opens a TCP socket and tries to connect to a Server IP_ADDR //! waiting on port PORT_NUM. //! If the socket connection is successful then the function will send 1000 //! TCP packets to the server. //! //! param[in] port number on which the server will be listening on //! //! return 0 on success, -1 on Error. // int BsdTcpClient(unsigned short usPort) [ // int iCounter; // short sTestBufLen; SlSockAddrIn_t sAddr; int iAddrSize; int iSockID; int iStatus; // long lLoopCount = 0; // filling the buffer /* for (iCounter=0 ; iCounter g_cBsdBuf[iCounter] = (char)(iCounter % 10); ] sTestBufLen = BUF_SIZE; */ //filling the TCP server socket address sAddr.sin_family = SL_AF_INET; sAddr.sin_port = sl_Htons((unsigned short)usPort); sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulDestinationIp); iAddrSize = sizeof(SlSockAddrIn_t); // creating a TCP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( iSockID < 0 ) [ ASSERT_ON_ERROR(SOCKET_CREATE_ERROR); ] // connecting to TCP server iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize); if( iStatus < 0 ) [ // error sl_Close(iSockID); ASSERT_ON_ERROR(CONNECT_ERROR); ] //set non-blocking mode use for wireless transfer long lNonblocking = 1; iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING,&lNonblocking,sizeof(lNonblocking)); if(iStatus < 0) [ sl_Close(iSockID); UART_PRINT("TCP Client failednr"); ] //wireless uart transfer iStatus = WuartTransfer(UARTA0_BASE,iSockID); if (iStatus<0) [ UART_PRINT("WUART Transfer failednr"); ] iStatus = sl_Close(iSockID); //closing the socket after sending 1000 packets ASSERT_ON_ERROR(iStatus); return SUCCESS; ] 使其成为客户端。这里客户当需要服务器的地址,而我作为服务器的那块板的地址是192.168.1.95。 |
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
545 浏览 0 评论
1609 浏览 0 评论
2045 浏览 0 评论
为啥BQ7693003DBTR芯片在和BQ769X0盒子通讯时收不到信号?
1507 浏览 0 评论
DSP 28027F 开发板 XDS100v2调试探针诊断日志显示了 Error -150 (SC_ERR_FTDI_FAIL)如何解决
1331 浏览 0 评论
AT32F407在USART2 DMA发送数据时,接包接到了要发送的数据,程序还是处于等待传输完成的标识判断中,为什么?
1756浏览 29评论
2781浏览 23评论
请问下tpa3220实际测试引脚功能和官方资料不符,哪位大佬可以帮忙解答下
1723浏览 20评论
请教下关于TAS5825PEVM评估模块原理图中不太明白的地方,寻求答疑
1634浏览 14评论
两个TMP117传感器一个可以正常读取温度值,一个读取的值一直是0,为什么?
1645浏览 13评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-1 19:07 , Processed in 0.759714 second(s), Total 52, Slave 42 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
3377