Enet_tcp -客户端.

2019-03-24 14:13发布

这是我根据网友academic以太网学习的例子改的,作为客户端链接主机的时候一直没有反应。请教大家问题出在哪里?
#include <string.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_types.h"
#include "inc/hw_sysctl.h"

#include "driverlib/ethernet.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"

#include "drivers/rit128x96x4.h"

#include "utils/lwiplib.h"
#include "utils/ustdlib.h"

#include "lwip/tcp.h"

extern err_t tcp_process(struct tcp_pcb *pcb);

//#include "D:StellarisWare hird_partylwip-1.3.1srccore cp_in.c"


#define My_Mac_ID         {0X00,0x14,0x79,0x0F,0x1D,0xE3}  //存储以太网控制器的物理地址,即MAC地址

#define MY_IP_ID               {192,168,14,210}                   //以太网通信的IP地址
#define IP_MARK_ID           {255,255,255,0}                           //255.255.255.0,子网掩码
#define MY_GATEWAY_ID   {192,168,14,121}                     //以太网通信的网关地址
#define TELENET_IP_ID   {192,168,14,121}                   //远程IP地址
const static unsigned char TCPData[]="192.168.14.121";
static const unsigned char pucMACAddress[]=My_Mac_ID;
static const unsigned char IPAddress[] = MY_IP_ID;
static const unsigned char TELIPAddress[] = TELENET_IP_ID;
static const unsigned char NetMaskAddr[] = IP_MARK_ID;
static const unsigned char GwWayAddr[] = MY_GATEWAY_ID;
#define SYSTICKHZ                100
#define SYSTICKMS               (1000 / SYSTICKHZ)
#define SYSTICKUS               (1000000 / SYSTICKHZ)
#define SYSTICKNS               (1000000000 / SYSTICKHZ)

static struct tcp_pcb *g_psPCB = NULL;

struct ip_addr ulTELIPAddr;


typedef struct
{
    unsigned long ulRetryCount;               
    tBoolean bCONTROLSent;               
}
tAPPState;

struct pbuf *fp=NULL;
tAPPState *fpState=NULL;       

void
SysTickIntHandler(void)
{
   
         lwIPTimer(SYSTICKMS);

}

void
UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
    //
    // Loop while there are more characters to send.
    //
    while(ulCount--)
    {
        //
        // Write the next character to the UART.
        //
        UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++);
    }
}
//当数据被正确发送到远程主机后(收到ACK),该函数会被调用。
static err_t
App_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
{
   LWIP_UNUSED_ARG(len);

   RIT128x96x4StringDraw("sent", 15, 16, 4);

        if(!arg)
        {
                return ERR_OK;
        }

        tcp_arg(pcb, NULL);
        tcp_sent(pcb, NULL);
        tcp_recv(pcb, NULL);
        mem_free(arg);
        tcp_close(pcb);
       
        return ERR_OK;

}

static err_t
APP_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
        tAPPState *pState;
        char *q;
        unsigned int i;
        q=(char*) malloc(p->len);
        for(i=0;i<=p->len;i++)
                *(q+i)=*((char*)p->payload+i);
        pState = arg;
        fp=p;
        if((err == ERR_OK) && (p != NULL))
        {
                tcp_recved(pcb, p->tot_len);
                 
                if(strncmp(p->payload, "LED ON", 6) == 0)
                {
                         GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0);
                         pbuf_free(p);
                         tcp_sent(pcb, App_sent);
                }
                if(strncmp(p->payload, "LED OFF", 7) == 0)
                {
                         GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0);
                         pbuf_free(p);
                         tcp_sent(pcb, App_sent);
                }
       
        //处理接收部分。
                UARTSend((unsigned char *)p->payload,p->len);
                RIT128x96x4Enable(1000000);
                RIT128x96x4StringDraw("                          ",0,16,15);
                RIT128x96x4StringDraw(q,0,16, 15);
        //        RIT128x96x4Disable();
        //发送数据
                tcp_input(p,NULL);
//                send_data();
                free(q);
       
        }

         if((err == ERR_OK) && (p == NULL))
    {
        //
        // Close the connection.
        //
        tcp_arg(pcb, NULL);
        tcp_sent(pcb, NULL);
        tcp_recv(pcb, NULL);
        mem_free(pState);
        tcp_close(pcb);
    }



    return ERR_OK;  
}
/*
static void
APP_error(void *arg, err_t err)
{
//   处理错误
    if(arg != NULL)
    {
        mem_free(arg);
    }
}  

static err_t
APP_poll(void *arg, struct tcp_pcb *pcb)
{
    tAPPState *pState;

   
    pState = arg;

  
    pState->ulRetryCount++;

   
    if(pState->ulRetryCount++ >= 120)
    {        //约120s之后无数据发送就会断开就断开连接。
        tcp_abort(pcb);
        return(ERR_ABRT);
    }

  
    return ERR_OK;
}        */


static err_t
APP_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
tAPPState *pState;
tcp_setprio(pcb, TCP_PRIO_MIN);
pState = mem_malloc(sizeof(tAPPState));
fpState=pState;
if(pState == NULL)
    {
        return(ERR_MEM);
    }
    pState->ulRetryCount = 0;
    pState->bCONTROLSent = false;

        tcp_arg(pcb, pState);

        tcp_recv(pcb, APP_recv);

//        tcp_err(pcb, APP_error);
        //这里设置2每500ms就调用一次。4就是1s
        //注意其中的APP_poll只有当协议栈中没有数据进出时,才会被调用。
        //我们可以用它来发送某些数据。
        //
//        tcp_poll(pcb, APP_poll, 4);
        RIT128x96x4Enable(1000000);
        RIT128x96x4StringDraw("connect success",0,8, 15);
        tcp_write(pcb,"hello! ",6,TCP_WRITE_FLAG_COPY);
        tcp_output(pcb);
       
        return(ERR_OK);

}

void
io_init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_0);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,1);
}

static err_t
SUC_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{
        tAPPState *pState;
        tcp_setprio(tpcb, TCP_PRIO_MIN);
        pState = mem_malloc(sizeof(tAPPState));

        if(pState == NULL)
        {
                return(ERR_MEM);
        }
    pState->ulRetryCount = 0;
    pState->bCONTROLSent = false;
        RIT128x96x4Enable(1000000);
        RIT128x96x4StringDraw("connected", 15, 0, 15);
        RIT128x96x4Disable();
        tcp_write(tpcb,"192.168.14.210",15,TCP_WRITE_FLAG_COPY);
        tcp_output(tpcb);
        tcp_sent(tpcb, App_sent);
        return err;
}

err_t TCP_SER_Poll(void *arg, struct tcp_pcb *pcb)
{
        err_t eError;
       
        eError = tcp_connect(pcb, &ulTELIPAddr, 1027, SUC_connected);

        if(eError != ERR_OK)
    {
        
        return 1;
    }
        return 0;
}




int
main(void)
{
       
   struct ip_addr ulIPAddr,ulNetMask,ulGWAddr;
   struct tcp_pcb *Pcb1;
   err_t ret_err ;

   struct pbuf *p;

   p = pbuf_alloc(PBUF_RAW,sizeof(TCPData),PBUF_RAM);
   p->payload=(void *)TCPData;

      if(REVISION_IS_A2)
    {
        SysCtlLDOSet(SYSCTL_LDO_2_75V);
    }

    SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_8MHZ);

        RIT128x96x4Init(1000000);
    RIT128x96x4StringDraw("guest:wellcome", 0, 0, 15);
//        RIT128x96x4Disable();
          
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH);
    SysCtlPeripheralReset(SYSCTL_PERIPH_ETH);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);

        io_init();       

    SysTickPeriodSet(SysCtlClockGet() / SYSTICKHZ);
    SysTickEnable();
    SysTickIntEnable();

        SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Enable processor interrupts.
    //
    IntMasterEnable();

    //
    // Set GPIO A0 and A1 as UART pins.
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Configure the UART for 115,200, 8-N-1 operation.
    //
    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));

    //
    // Enable the UART interrupt.
    //
    IntEnable(INT_UART0);
    UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);

    //


       
        IP4_ADDR(&ulIPAddr,IPAddress[3],IPAddress[2],IPAddress[1],IPAddress[0]);
    IP4_ADDR(&ulNetMask,NetMaskAddr[3],NetMaskAddr[2],NetMaskAddr[1],NetMaskAddr[0]);
    IP4_ADDR(&ulGWAddr,GwWayAddr[3],GwWayAddr[2],GwWayAddr[1],GwWayAddr[0]);

        IP4_ADDR(&ulTELIPAddr,TELIPAddress[3],TELIPAddress[2],TELIPAddress[1],TELIPAddress[0]);
    lwIPInit(pucMACAddress,ulIPAddr.addr, ulNetMask.addr, ulGWAddr.addr, IPADDR_USE_STATIC);


    g_psPCB=Pcb1 = tcp_new();

//        Pcb1 = (struct tcp_pcb *)0x200013b0 ;


        ret_err=tcp_bind(Pcb1, IP_ADDR_ANY, 1027) ;

        tcp_arg(Pcb1, NULL);

        tcp_recv(Pcb1, APP_recv);

    if(ret_err==ERR_OK)  
        {
                UARTSend("ERR_OK_bind ",15);
                RIT128x96x4StringDraw("ERR_OK_bind",0,24, 15);       
        }
        else
                UARTSend("ERR_USE_bind ",15);
        //tcp_bind(Pcb1, &ulIPAddr,1027);
    //Pcb1 = listen(Pcb1);
    tcp_accept(Pcb1, APP_accept);  //
        tcp_sent(Pcb1, App_sent);
        tcp_poll(Pcb1, TCP_SER_Poll, 4);

        if(ERR_OK==tcp_connect(Pcb1, &ulTELIPAddr, 1027,SUC_connected))
        {
                UARTSend("ERR_OK_con ",15);
                RIT128x96x4StringDraw("ERR_OK_con",0,32, 15);
        }
        else
                UARTSend("ERR_VAL ",8);  

        //tcp_process(Pcb1);
       
        while(1)
        {
        }


} 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
7条回答
academic
2019-03-25 05:07
楼主看看这个函数IP4_ADDR()和tcp_connect()的用法就知道问题在那里了。

一周热门 更多>

相关问题

    相关文章