本帖最后由 比神乐 于 2021-1-31 13:50 编辑
今天捣鼓了一下网口试验。
TCP客户端。
代码:
/* Includes -----------------------------------------------------------------*/
#include "at32_board.h"
#include "main.h"
#include "eth_config.h"
/** @addtogroup AT32F407_StdPeriph_Examples
* @{
*/
/** @addtogroup ETH_TCP_Server
* @{
*/
/* Gloable variables ---------------------------------------------------------*/
__IO uint32_t LocalTime = 0; /* this variable is used to create a time reference incremented by 10ms */
uint32_t timingdelay;
/* Gloable functions ---------------------------------------------------------*/
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Main Function.
* @param None
* @retval None
*/
int main(void)
{
unsigned char tcp_data[] = "TCP client experiment!\n";
struct tcp_pcb *pcb;
uint32_t Status;
/* --------BSP Init ------------------------------------------------*/
AT32_Board_Init();
UART_Print_Init(115200);
/* Setup AT32 system (clocks, Ethernet, GPIO, NVIC)*/
Status = System_Setup();
while(Status == ETH_DRIVER_FAIL);
/* Initilaize LwIP satck, IP configuration and MAC configuration*/
LwIP_Init();
/* Initialize TCP client module */
TCP_Client_Init(TCP_LOCAL_PORT,TCP_SERVER_PORT,TCP_SERVER_IP); //Initialize TCP client
while(1)
{
pcb = Check_TCP_Connect();
if(pcb != 0)
{
TCP_Client_Send_Data(pcb,tcp_data,sizeof(tcp_data)); //Send data to TCP server actively
}
Delay_s(0xfffff); //necessary delay
System_Periodic_Handle(); //Update TCP timers
}
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Inserts a delay time.
* @param nCount: number of 10ms periods to wait for.
* @retval None
*/
void Delay(uint32_t nCount)
{
/* Capture the current local time */
timingdelay = LocalTime + nCount;
/* wait until the desired delay finish */
while(timingdelay > LocalTime)
{
}
}
/**
* @brief Updates the system local time
* @param None
* @retval None
*/
void Time_Update(void)
{
LocalTime += SYSTEMTICK_PERIOD_MS;
}
/**
* @brief Handles the periodic tasks of the system
* @param None
* @retval None
*/
void System_Periodic_Handle(void)
{
/* LwIP periodic services are done here */
LwIP_Periodic_Handle(LocalTime);
}
/* TCP server and client configuration*/
#define TCP_LOCAL_PORT 1030
#define TCP_SERVER_PORT 1031
#define TCP_SERVER_IP 192,168,1,19 //server IP
static uint8_t G_IP[ADDR_LENGTH] = {192, 168, 1, 37};
static uint8_t G_GW[ADDR_LENGTH] = {192, 168, 1, 187};
static uint8_t G_MASK[ADDR_LENGTH] = {255, 255, 255, 0};
效果图:
注意还要关闭防火墙。
|