【cc3200】Wuart

參考郭書軍老師的《CC3200應用》
這裏寫圖片描述
每次發完,以16進制發送1b退出,然後再進行新的收發循環

int BsdTcpClient(unsigned short usPort)
{

    SlSockAddrIn_t  sAddr;
    int             iAddrSize;
    int             iSockID;
    int             iStatus;

    //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);
    }

    //設置非阻塞模式
    long lNonBlocking=1;
    iStatus=sl_SetSockOpt(iSockID,SL_SOL_SOCKET,SL_SO_NONBLOCKING,&lNonBlocking,sizeof(lNonBlocking));
    if(iStatus<0){
        sl_Close(iSockID);
    }

    iStatus=WuartTransfer(UARTA0_BASE,iSockID);


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

    return SUCCESS;
}
int WuartTransfer(unsigned long ulBase,int iSockID){

    UART_PRINT("進入WuartTransfer\n\r\n\r");
    int iStatus=0;
    char cTxBuf[100];
    char cRxBuf[100];
    char cGetChar;
    char iCounter=0;

    while(1){

        cGetChar=MAP_UARTCharGetNonBlocking(ulBase);

        if(cGetChar!=0xff){

            MAP_UARTCharPut(ulBase,cGetChar);
            cTxBuf[iCounter++]=cGetChar;

            //0x0d回車Enter,0x1b退出Esc
            if(cGetChar==0x0d||cGetChar==0x1b){

                UART_PRINT("當檢測到回車或退出時進入這裏\n\r\n\r");

                cTxBuf[iCounter++]=0x0a;
                iStatus=sl_Send(iSockID,cTxBuf,iCounter,0);//發送給服務端

                if(iStatus<=0){
                        ASSERT_ON_ERROR(sl_Close(iSockID));
                        UART_PRINT("發送數據失敗\n\r\n\r");
                        break;
                }

                //判斷輸入的到底是哪個
                if(cGetChar==0x0d){
                    iCounter=0;
                }else{
                    break;
                }
            }//if(cGetChar==0x0d||cGetChar==0x1b)

        }//if(cGetChar!=0xff)

        iStatus=sl_Recv(iSockID,cRxBuf,100,0);

        if(iStatus>0){
            if( cRxBuf[iStatus-2]==0x0d ){
                cRxBuf[iStatus]=0;
                UART_PRINT("\r\n\r\nsl_Recv方法執行成功,iStatus=%d,接收到的數據爲\r\n\r\n",iStatus);
                UART_PRINT("\r\n\r\n%s\r\n\r\n",cRxBuf);

            }else{
                break;
            }
        }//if(iStatus>0)

    }//while(1)

    return iStatus;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章