This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

cc3200 无线UART程序设计



问题描述:我在wlan_staion的代码增加了一些代码,主要是增加了BsdTcpClient代码,使其作为客户端

                  在wlan_ap的代码里增加了BsdTcpServer代码,使其成为服务器。其他一些必要的修改都已完成。针对BsdTcpClient中的

 sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulDestinationIp);中的参数g_ulDestinationIp
修改  #define IP_ADDR             0xc0a8015f /* 192.168.1.95*/。其中192.168.1.95是路由器分配给另一块板的地址,这块板做服务器。
尽管如此,还是不能实现连接。
实验显示如下
 
请问为什么总是无法建立连接?
感谢!
  • Hi, Endi Mati,

    你的TCP 服务器是建立在做AP的这个板上吗?那它的IP默认应该是192.168.1.1哦。

    我不是太明白你说的192.168.1.195的IP是怎么回事,能否描述一下, 谢谢。

  • 是的,我在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: %s\n\r",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("\r\n 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 \n\r");

    LOOP_FOREVER();
    }

    UART_PRINT("Device is configured in default state \n\r");

    //
    // 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 \n\r");
    LOOP_FOREVER();
    }

    UART_PRINT("Device started as STATION \n\r");

    //
    // 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...\n\r");
    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 \n\r");
    LOOP_FOREVER();
    }

    UART_PRINT("Connect a client to Device\n\r");
    while(!IS_IP_LEASED(g_ulStatus))
    {
    //wating for the client to connect
    }
    UART_PRINT("Client is connected to Device\n\r");

    lRetVal = BsdTcpServer(5001);
    if(lRetVal <0)
    {
    UART_PRINT("TCP Server failed\n/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 \n\r");
    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("\n\r receive:");
    //SEND CHAR
    Message(cRxBuf);


    }
    else
    {
    //ESC
    UART_PRINT("\n\r receive date end\n\r");
    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("\r\n 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 error\n\r");
    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("\r\n 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<BUF_SIZE ; 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 failed\n\r");


    }
    //wireless uart transfer
    iStatus = WuartTransfer(UARTA0_BASE,iSockID);
    if (iStatus<0)
    {
    UART_PRINT("WUART Transfer failed\n\r");

    }

    iStatus = sl_Close(iSockID);
    //closing the socket after sending 1000 packets
    ASSERT_ON_ERROR(iStatus);

    return SUCCESS;
    }

    使其成为客户端。这里客户当需要服务器的地址,而我作为服务器的那块板的地址是192.168.1.95。

  • 感谢,我已解决。