请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:EK-TM4C1294XL 工具/软件:Code Composer Studio
尊敬的 TI
您是否具有 板 EK-TM4C1294XL 的 TCP/IP 客户端示例?
请推荐我。
谢谢你
RATWO
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.
工具/软件:Code Composer Studio
尊敬的 TI
您是否具有 板 EK-TM4C1294XL 的 TCP/IP 客户端示例?
请推荐我。
谢谢你
RATWO
您好、Ratthawut、
我看到了您可以参考的这个客户示例。 您将需要微调 代码 以适合您的应用。 请注意、这绝不是一个经过全面验证的示例。 如果遇到错误或性能问题、您将负责进行修改。
#include
#include
#include
#include
include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/flash.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/rom_map.h"
#include "drivers/exctrintrune.h"
#include "drivers/out_out_out_out_utils/ld/ldise"#include "texpintrintrinh #include "out.h #include "out_unic.h"
ERR_t ECHO_POLL (void *arg、struct tcp_pcb);
void ECHO_ERROR (void *arg、ERR_t err);
err_t ECHO_SENT (void *arg、 struct tcp_pcb * tpcb、u16_t len);
err_t err;
uint32_t tcp_send_packet (void);
static struct tcp_pcb * testpcb;//pcb 全局
#define SYSTICKHZ 100
#define SYSTICKMS (1000/SYSTICKHZ)
#define SysTK_INT_PRIORITY 0x80
#define ETHERNET_INT_PRIORITY 0xC0
uINT32_t g_ui32IPAddress;
uINT32_t g_ui32ool 时钟;
易失性 bg_bled;
bIPAddrValid = 0;
//****Sys*
//驱动程序库遇到错误时调用的错误例程。
//
#ifdef debug
void
__error__(char *pcFilename、uint32_t ui32Line)
{
#endif
void
DisplayIPAddress (uint32_t ui32Addr)
{
char pcBuf[16];
//将 IP 地址转换为字符串。
usprintf (pcBuf、"%d.%d.%d.%d"、ui32Addr & 0xff、(ui32Addr >> 8)& 0xff、
(ui32Addr >> 16)& 0xff、(ui32Addr >> 24)& 0xff);
UARTprintf (pcBuf);
}
//*************
// lwIP 库支持任何与主机相关的计时器函数所需的。
//
void
lwIPHostTimerHandler (void)
{
uint32_t ui32NewIPAddress;
ui32NewIPAddress = lwIPLocalIPAddrGet ();//获取当前 IP 地址。
if (ui32NewIPAddress!= g_ui32IPAddress)//查看 IP 地址是否已更改。
{
if (ui32NewIPAddress == 0xffffffff)
{
UARTprintf ("正在等待链接。\n");
}
否则、如果(ui32NewIPAddress =0)
{
UARTprintf ("正在等待 IP 地址。\n");
}
其他
{
G_bIPAddrValid = 1;
UARTprintf ("IP 地址:");
DisplayIPAddress (ui32NewIPAddress);
UARTprintf ("\n\n");
}
G_ui32IPAddress = ui32NewIPAddress;
}
if ((ui32NewIPAddress =0)||(ui32NewIPAddress =0xffffffff))
{
//
//不执行任何操作并继续等待。
//
}
}
//*********
// SysTick 中断的中断处理程序。
//
void
SysTickIntHandler (void)
{
lwIPTimer (SYSTICKMS);
G_bled = true;
}
//*********
char MyData[1024];
err_t echo_receive (void * arg、struct tcp_pcb、struct pbuf * p、err_t err)
{
int len;
char *header="客户机:";
int header_len = strlen (标头);
UARTprintf ("接收到的数据。\n");
if (p = NULL){
UARTprintf ("远程主机关闭了连接。\n");
tcp_close (tpcb);
返回 ERR_ABRT;
} 否则{
tcp_recved (tpcb、p->t_t_len);
//支付负荷的大小
len = p->t_t_len + header_len;
strcpy (MyData、标头);
strcat (MyData、(char *) p->payload);
//释放数据包缓冲区
pbuf_free (p);
//检查输出缓冲器容量
如果(len > tcp_sndbuf (tpcb))
len= tcp_sndbuf (tpcb);
//将数据排队发送出去
//err = tcp_write (tpcb、MyData、len、0);
ERR = tcp_write (tpcb、MyData、len、0);
如果(err =ERR_OK){
UARTprintf ("回送数据");
}
否则、如果(err =ERR_MEM)
{
//内存不足,请稍后再试,推迟轮询*/
UARTprintf ("内存不足。\n");
} 其他
{
/*需要注意的其他问题*/
UARTprintf ("其他问题。\n");
}
tcp_sent (tpcb、NULL);//无需回拨*/
}
返回 ERR_OK;
}
uint32_t tcp_send_packet (void)
{
char *字符串="Hello World! \n\n";
ERR = tcp_write (testpcb、string、strlen (string)、tcp_write_flag_copy);
如果(err){
UARTprintf ("错误:代码:%d (tcp_send_packet::tcp_write)\n"、错误);
返回1;
}
/*现在发送*/
ERR = tcp_output (testpcb);
如果(err){
UARTprintf ("错误:代码:%d (tcp_send_packet::tcp_output)\n"、错误);
返回1;
}
返回0;
}
ERR_t connectCallback (void *arg、struct tcp_pcb、err_t err)
{
if (err =ERR_OK){
UARTprintf ("连接已建立。\n");
tcp_recv (testpcb、eco_receive);
tcp_sent (testpcb、NULL);
UARTprintf ("向服务器发送问候消息\n");
tcp_send_packet();
返回0;
} 否则{
UARTprintf ("未建立连接。\n");
返回1;
}
}
void echo_client_init (void)
{
/*创建 IP */
struct ip_addr server_addr;
/*创建控制块*/
testpcb = tcp_new();//testpcb 是一个全局结构 tcp_pcb
if (testpcb!=空)
{
IP4_ADDR (&server_addr、10、219、15、38);PC 计算机 TCP/IP
tcp_bind (testpcb、IP_ADDR_ANY、7000);
//现在连接*/
err = tcp_connect (testpcb、&server_addr、8000、connectCallback);
如果(err){
UARTprintf ("错误:代码:%d (tcp_connect)\n"、错误);
}
UARTprintf ("PCB created \n");
} 否则{
memp_free (MEMP_tcp_PCB、testpcb);
UARTprintf ("PCB 未创建\n");
}
}
int
main (void)
{
uint32_t ui32User0、ui32User1;
uint8_t pui8MACArray[8];
SysCtlMOSCConfigSet (SYSCTL_MOSC_HIGHFREQ);
G_ui32SysClock = MAP_SysCtlClockFreqSet ((SYSCTL_XTAL_25MHz |
SYSCTL_OSC_MAIN |
SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480)、120000000);
PinoutSet (true、false);
UARTStdioConfig (0、115200、g_ui32SysClock);
UARTprintf ("\033[2J\033[H");
UARTprintf ("以太网 lwIP TCP echo 客户端示例\n"\});
MAP_GPIOPinTypeGPIOOutput (GPIO_PORTN_BASE、GPIO_PIN_1);
MAP_GPIOPinWrite (GPIO_PORTN_BASE、GPIO_PIN_1、~GPIO_PIN_1);
MAP_SysTickPeriodSet (g_ui32SysClock / SYSTICKHZ);
map_SysTickEnable();
map_SysTickIntEnable();
map_FlashUserGet (&ui32User0、&ui32User1);
if ((ui32User0 == 0xffffffff)||(ui32User1 == 0xffffffff)
{
UARTprintf ("未对 MAC 进行编程!\n");
while (1)
{
}
}
UARTprintf ("正在等待 IP.\n");
pui8MACArray[0]=((ui32User0 >> 0)& 0xff);
pui8MACArray[1]=((ui32User0 >> 8)& 0xff);
pui8MACArray[2]=((ui32User0 >> 16)& 0xff);
pui8MACArray[3]=((ui32User1 >> 0)& 0xff);
pui8MACArray[4]=((ui32User1 >> 8)& 0xff);
pui8MACArray[5]=((ui32User1 >> 16)& 0xff);
lwIPInit (g_ui32SysClock、pui8MACArray、0、0、0、 ipaddr_use_dhcp);
while (g_bIPAddrValid ==0);
ECHO_CLIENT_INIT();
MAP_IntPrioritySet (INT_EMAC0、ETHERNET_INT_PRIORITY);
MAP_IntPrioritySet (FAULT_SysTick、SysTK_INT_PRIORITY);
//
//循环永久、处理 LED 闪烁。 所有工作都在中完成
//中断处理程序。
while (1)
{
while (g_bled == false)
{
}
//
//清除标志。
//
G_bled = false;
//
//切换 LED。
//
MAP_GPIOPinWrite (GPIO_PORTN_BASE、GPIO_PIN_1、
(MAP_GPIOPinRead (GPIO_PORTN_BASE、GPIO_PIN_1)^
GPIO_PIN_1);
}
}