#include "tcpserverc.h" #include "lwip/netif.h" #include "lwip/ip.h" #include "lwip/tcp.h" #include "lwip/init.h" #include "netif/etharp.h" #include "lwip/udp.h" #include "lwip/pbuf.h" #include #include #include "main.h" #include "ht1200m.h" #include "user_lib.h" #include "communication_protocol.h" #include "user_gpio.h" #include "ch438q.h" err_t (*tcpecho_accept_hart[TCP_MAX])(void *arg, struct tcp_pcb *newpcb, err_t err); struct tcp_pcb *server_pcb_hart[TCP_MAX] = {0}; struct tcp_pcb *server_pcb_control = NULL; // communication_di_t *user_communication_di = NULL; // communication_do_t *user_communication_do = NULL; // communication_ai_t *user_communication_ai = NULL; communication_ao_t *user_communication_ao = NULL; uint8_t tcp_echo_flags[TCP_MAX] = {0}; uart_t hart_uart[TCP_MAX] = {0}; /*接收回调函数*/ static err_t tcpecho_recv_hart1(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART1] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART1] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART1].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART1].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART1, hart_uart[TCP_HART1].tx_data, p->tot_len); hart_uart[TCP_HART1].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART1].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart2(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART2] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART2] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART2].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART2].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART2, hart_uart[TCP_HART2].tx_data, p->tot_len); hart_uart[TCP_HART2].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART2].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart3(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART3] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART3] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART3].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART3].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART3, hart_uart[TCP_HART3].tx_data, p->tot_len); hart_uart[TCP_HART3].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART3].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart4(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART4] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART4] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART4].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART4].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART4, hart_uart[TCP_HART4].tx_data, p->tot_len); hart_uart[TCP_HART4].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART4].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart5(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART5] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART5] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART5].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART5].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART5, hart_uart[TCP_HART5].tx_data, p->tot_len); hart_uart[TCP_HART5].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART5].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart6(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART6] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART6] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART6].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART6].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART6, hart_uart[TCP_HART6].tx_data, p->tot_len); hart_uart[TCP_HART6].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART6].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart7(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART7] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART7] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART7].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART7].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART7, hart_uart[TCP_HART7].tx_data, p->tot_len); hart_uart[TCP_HART7].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART7].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart8(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART8] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART8] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART8].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART8].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART8, hart_uart[TCP_HART8].tx_data, p->tot_len); hart_uart[TCP_HART8].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART8].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart9(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART9] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART9] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART9].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART9].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART9, hart_uart[TCP_HART9].tx_data, p->tot_len); hart_uart[TCP_HART9].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART9].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart10(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART10] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART10] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART10].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART10].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART10, hart_uart[TCP_HART10].tx_data, p->tot_len); hart_uart[TCP_HART10].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART10].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart11(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART11] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART11] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART11].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART11].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART11, hart_uart[TCP_HART11].tx_data, p->tot_len); hart_uart[TCP_HART11].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART11].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart12(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART12] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART12] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART12].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART12].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART12, hart_uart[TCP_HART12].tx_data, p->tot_len); hart_uart[TCP_HART12].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART12].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart13(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART13] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART13] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART13].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART13].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART13, hart_uart[TCP_HART13].tx_data, p->tot_len); hart_uart[TCP_HART13].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART13].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart14(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART14] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART14] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART14].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART14].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART14, hart_uart[TCP_HART14].tx_data, p->tot_len); hart_uart[TCP_HART14].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART14].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart15(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART15] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART15] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART15].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART15].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART15, hart_uart[TCP_HART15].tx_data, p->tot_len); hart_uart[TCP_HART15].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART15].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_hart16(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { // 对应接收数据连接的控制块 接收到的数据 if (p != NULL) { /* 更新窗口*/ tcp_echo_flags[TCP_HART16] = 1; tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_hart[TCP_HART16] = tpcb; // 直接赋值 memset(hart_uart[TCP_HART16].tx_data, 0, TCP_TRANSMIT_MAX); memcpy(hart_uart[TCP_HART16].tx_data, (int *)p->payload, p->tot_len); ch438_send_data(TCP_HART16, hart_uart[TCP_HART16].tx_data, p->tot_len); hart_uart[TCP_HART16].tx_length = p->tot_len; // 更新发送数据长度 hart_uart[TCP_HART16].tx_times++; pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { uint8_t tcp_rx_data[128] = {0}; // 接受数据缓存区 uint8_t tcp_tx_data[128] = {0}; // 发送数据缓存区 uint8_t rx_data_len = 0; uint8_t tx_data_len = 0; communication_data_u communication_data; if (p != NULL) { /* 更新窗口*/ tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_control = tpcb; // 直接赋值 memcpy(tcp_rx_data, (int *)p->payload, p->tot_len); rx_data_len = p->tot_len; /*1. 对接收的数据做异或校验、帧头帧尾判断,校验失败返回信息,校验通过继续下一步、校验数据从帧头后面到校验位结束*/ if (tcp_rx_data[0] != 0xAA || tcp_rx_data[rx_data_len - 1] != 0x3C) // 帧头帧尾判断 { tx_data_len = COM_ERROR_CODE_SIZE; communication_exception(tcp_tx_data, tcp_rx_data, FRAMING_ERROR); tcp_write(tpcb, tcp_tx_data, tx_data_len, 1); } else { if (tcp_rx_data[rx_data_len - 2] != xor_compute(tcp_rx_data + 1, rx_data_len - 3)) // 异或校验 { tx_data_len = COM_ERROR_CODE_SIZE; communication_exception(tcp_tx_data, tcp_rx_data, CHECK_ERROR); tcp_write(tpcb, tcp_tx_data, tx_data_len, 1); } else { memcpy(communication_data.data, tcp_rx_data + 5, tcp_rx_data[4]); if (tcp_rx_data[3] == WRITE_ANALOG_CMD) // 写模拟量指令 { /*写操作,将数据写入寄存器,组包返回*/ user_communication_ao = &communication_data.ao_data; communication_set_ao(user_communication_ao); tcp_write(tpcb, tcp_rx_data, rx_data_len, 1); } else { // 返回命令号错误 tx_data_len = COM_ERROR_CODE_SIZE; communication_exception(tcp_tx_data, tcp_rx_data, COMMAND_ERROR); tcp_write(tpcb, tcp_tx_data, tx_data_len, 1); } } /*2. 判断所要执行的操作 读或写指令*/ /*3. 对要发送的数据进行校验,组包,返回数据*/ } pbuf_free(p); } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } return ERR_OK; } static err_t tcpecho_accept_hart1(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 // 形参的数量和类型必须一致 { tcp_recv(newpcb, tcpecho_recv_hart1); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart2(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 // 形参的数量和类型必须一致 { tcp_recv(newpcb, tcpecho_recv_hart2); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart3(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 // 形参的数量和类型必须一致 { tcp_recv(newpcb, tcpecho_recv_hart3); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart4(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 // 形参的数量和类型必须一致 { tcp_recv(newpcb, tcpecho_recv_hart4); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart5(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 // 形参的数量和类型必须一致 { tcp_recv(newpcb, tcpecho_recv_hart5); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart6(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 // 形参的数量和类型必须一致 { tcp_recv(newpcb, tcpecho_recv_hart6); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart7(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 // 形参的数量和类型必须一致 { tcp_recv(newpcb, tcpecho_recv_hart7); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart8(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 // 形参的数量和类型必须一致 { tcp_recv(newpcb, tcpecho_recv_hart8); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart9(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 { tcp_recv(newpcb, tcpecho_recv_hart9); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart10(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 { tcp_recv(newpcb, tcpecho_recv_hart10); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart11(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 { tcp_recv(newpcb, tcpecho_recv_hart11); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart12(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 { tcp_recv(newpcb, tcpecho_recv_hart12); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart13(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 { tcp_recv(newpcb, tcpecho_recv_hart13); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart14(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 { tcp_recv(newpcb, tcpecho_recv_hart14); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart15(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 { tcp_recv(newpcb, tcpecho_recv_hart15); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_hart16(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 { tcp_recv(newpcb, tcpecho_recv_hart16); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } static err_t tcpecho_accept_control(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 { tcp_recv(newpcb, tcpecho_recv_control); // 当收到数据时,回调用户自己写的tcpecho_recv return ERR_OK; } void tcp_echo_init(void) { struct tcp_pcb *server_hart[TCP_MAX] = {0}; struct tcp_pcb *server_control = NULL; tcpecho_accept_hart[TCP_HART1] = tcpecho_accept_hart1; tcpecho_accept_hart[TCP_HART2] = tcpecho_accept_hart2; tcpecho_accept_hart[TCP_HART3] = tcpecho_accept_hart3; tcpecho_accept_hart[TCP_HART4] = tcpecho_accept_hart4; tcpecho_accept_hart[TCP_HART5] = tcpecho_accept_hart5; tcpecho_accept_hart[TCP_HART6] = tcpecho_accept_hart6; tcpecho_accept_hart[TCP_HART7] = tcpecho_accept_hart7; tcpecho_accept_hart[TCP_HART8] = tcpecho_accept_hart8; tcpecho_accept_hart[TCP_HART9] = tcpecho_accept_hart9; tcpecho_accept_hart[TCP_HART10] = tcpecho_accept_hart10; tcpecho_accept_hart[TCP_HART11] = tcpecho_accept_hart11; tcpecho_accept_hart[TCP_HART12] = tcpecho_accept_hart12; tcpecho_accept_hart[TCP_HART13] = tcpecho_accept_hart13; tcpecho_accept_hart[TCP_HART14] = tcpecho_accept_hart14; tcpecho_accept_hart[TCP_HART15] = tcpecho_accept_hart15; tcpecho_accept_hart[TCP_HART16] = tcpecho_accept_hart16; /* 创建一路HART */ for (uint8_t i = TCP_HART1; i < TCP_MAX; i++) { server_hart[i] = NULL; tcp_echo_flags[i] = 0; server_hart[i] = tcp_new(); /* 绑定TCP控制块 */ tcp_bind(server_hart[i], IP_ADDR_ANY, TCP_PORT_HART1 + i); /* 进入监听状态 */ server_hart[i] = tcp_listen(server_hart[i]); /* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */ tcp_accept(server_hart[i], tcpecho_accept_hart[i]); // 侦听到连接后,回调用户编写的tcpecho_accept } /* 创建控制块 */ server_control = tcp_new(); /* 绑定TCP控制块 */ tcp_bind(server_control, IP_ADDR_ANY, TCP_PORT_CONTROL); /* 进入监听状态 */ server_control = tcp_listen(server_control); /* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */ tcp_accept(server_control, tcpecho_accept_control); // 侦听到连接后,回调用户编写的tcpecho_accept } void user_send_data_hart(uint8_t ch, uint8_t *data, uint16_t len) { HAL_GPIO_TogglePin(PE5_LED_GPIO_Port, PE5_LED_Pin); // 接收数据时,LED闪烁 tcp_write(server_pcb_hart[ch], data, len, 1); } // void user_send_data_hart9(uint8_t *data, uint16_t len) // { // tcp_write(server_pcb_hart9, data, len, 1); // } // void user_send_data_control(uint8_t *data, uint16_t len) // { // tcp_write(server_pcb_control, data, len, 1); // }