开始的时候PC作为服务器,无法接受数据,经过几天,终于发现了错误所在,可是不知道为什么,麻烦各位帮我看看!
const static unsigned char test_data[]="This is LwIP TCP Client 在Luminary Cortex-M3上的测试!
";
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_types.h"
#include "driverlib/ethernet.h"
#include "driverlib/flash.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "utils/locator.h"
#include "utils/lwiplib.h"
#include "utils/uartstdio.h"
#include "utils/ustdlib.h"
#include "grlib/grlib.h"
//*****************************************************************************
//
// Defines for setting up the system clock.
//
//*****************************************************************************
#define SYSTICKHZ 100
#define SYSTICKMS (1000 / SYSTICKHZ)
#define SYSTICKUS (1000000 / SYSTICKHZ)
#define SYSTICKNS (1000000000 / SYSTICKHZ)
//*****************************************************************************
//
// Interrupt priority definitions. The top 3 bits of these values are
// significant with lower values indicating higher priority interrupts.
//
//*****************************************************************************
#define SYSTICK_INT_PRIORITY 0x80
#define ETHERNET_INT_PRIORITY 0xC0
//*****************************************************************************
//
//the ip of PC
//
//*****************************************************************************
#define ip_PC {192,168,0,101}
static const unsigned char pcIPAddress[]=ip_PC;
//*****************************************************************************
//
//address for Mac,IP,Mack,gateway address
//
//*****************************************************************************
#define My_Mac_ID {0X00,0x14,0x97,0x0F,0x1D,0xE3} //存储以太网控制器的物理地址,即MAC地址
#define MY_IP_ID {192,168,0,100} //以太网通信的IP地址
#define IP_MARK_ID {255,255,255,0} //255.255.255.0,子网掩码
#define MY_GATEWAY_ID {192,168,0,1} //以太网通信的网关地址
static const unsigned char pucMACAddress[]=My_Mac_ID;
static const unsigned char IPAddress[] = MY_IP_ID;
static const unsigned char NetMaskAddr[] = IP_MARK_ID;
static const unsigned char GwWayAddr[] = MY_GATEWAY_ID;
//*****************************************************************************
//
//define a tcp_pcb struct for the tcp_write
//
//*****************************************************************************
static struct tcp_pcb *g_psPCB = NULL;
//*****************************************************************************
//
//a struct for the state of the pcb
//
//*****************************************************************************
typedef struct
{
unsigned long ulRetryCount;
tBoolean bCONTROLSent;
}
tAPPState;
//*****************************************************************************
//
//this function will used after the connection is builded
//
//*****************************************************************************
static err_t tcp_trans(void *arg,struct tcp_pcb *tpcb,err_t err)
{
tAPPState *pState;
pState = arg;
if((err == ERR_OK) && (test_data != NULL))
{
tcp_write(tpcb,test_data,sizeof(test_data),0);
}
if((err == ERR_OK) && (test_data == NULL))
{
//
// Close the connection.
//
mem_free(pState);
tcp_close(tpcb);
}
return ERR_OK;
}
//*****************************************************************************
//
// The interrupt handler for the SysTick interrupt.
//
//*****************************************************************************
void
SysTickIntHandler(void)
{
//
// Call the lwIP timer handler.
//
lwIPTimer(SYSTICKMS);
}
//*****************************************************************************
//
//this is the main function
//
//*****************************************************************************
int main()
{
struct ip_addr ulIPAddr,ulNetMask,ulGWAddr,pIPAddress;
struct tcp_pcb *tcp_pcb_conn;
//
// Set the system clock to run at 50MHz from the PLL.
//
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
//
// Enable and Reset the Ethernet Controller.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH);
SysCtlPeripheralReset(SYSCTL_PERIPH_ETH);
// Enable Port F for Ethernet LEDs.
// LED0 Bit 3 Output
// LED1 Bit 2 Output
//
GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
//
// Configure SysTick for a periodic interrupt.
//
SysTickPeriodSet(SysCtlClockGet() / SYSTICKHZ);
SysTickEnable();
SysTickIntEnable();
//
// Enable processor interrupts.
//
IntMasterEnable();
//
// Initialze the lwIP library, using the stacic ip.
//
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(&pIPAddress,pcIPAddress[3],pcIPAddress[2],pcIPAddress[1],pcIPAddress[0]);
lwIPInit(pucMACAddress,ulIPAddr.addr, ulNetMask.addr, ulGWAddr.addr, IPADDR_USE_STATIC);
//
//create a TCP connection betweent LM and PC
//
tcp_pcb_conn=tcp_new();
tcp_bind (tcp_pcb_conn,&ulIPAddr, 1026);
tcp_connect(tcp_pcb_conn,&pIPAddress,1026,tcp_trans);
while(1)
{
}
}
上面是开始的程序,后来发现PC机IP的定义和初始化修改后,问题解决了!
大体如下:
开始时候的定义:
#define ip_PC {192,168,1,101}
static const unsigned char pcIPAddress[]=ip_PC;
IP4_ADDR(&pIPAddress,pcIPAddress[3],pcIPAddress[2],pcIPAddress[1],pcIPAddress[0]);
这样再通过pIPAddress进行传递
新的程序是这样的:
IP4_ADDR(&pIPAddress,192,168,1,101);
再通过pIPAddress进行传递PC机的IP
我感觉没有什么变化,可是不修改就无法接受数据,麻烦各位看看是为什么,谢谢!
此帖出自
小平头技术问答
一周热门 更多>