stm32单片机tcp
时间: 2023-12-23 07:26:51 浏览: 152
STM32单片机可以通过TCP/IP协议栈实现TCP通信。下面提供两个模板供您参考:
1. STM32作TCP服务器,实现PC多客户端连接的模板
```c
// 多连接处理部分的代码在tcp_server_demo.c中
// 其他型号型号可参考修改
#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/api.h"
#include "lwip/sys.h"
#include "lwip/tcp.h"
#include "lwip/sockets.h"
#include "lwip/inet.h"
#define TCP_SERVER_PORT 5005
#define TCP_SERVER_MAX_CONNECTIONS 2
static void tcp_server_thread(void *arg)
{
int sock, newsock, n;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
char recv_buf[1024];
char send_buf[1024];
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("socket error\n");
return;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(TCP_SERVER_PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
printf("bind error\n");
return;
}
if (listen(sock, TCP_SERVER_MAX_CONNECTIONS) < 0) {
printf("listen error\n");
return;
}
while (1) {
client_addr_len = sizeof(client_addr);
newsock = accept(sock, (struct sockaddr *)&client_addr, &client_addr_len);
if (newsock < 0) {
printf("accept error\n");
continue;
}
while (1) {
n = recv(newsock, recv_buf, sizeof(recv_buf), 0);
if (n <= 0) {
printf("recv error\n");
break;
}
// 处理接收到的数据
// ...
sprintf(send_buf, "received %d bytes\n", n);
n = send(newsock, send_buf, strlen(send_buf), 0);
if (n <= 0) {
printf("send error\n");
break;
}
}
close(newsock);
}
}
void tcp_server_init(void)
{
sys_thread_new("tcp_server_thread", tcp_server_thread, NULL, 1024, 2);
}
```
2. 基于正点原子STM32F407开发板调试,ModbusTCP服务器源程序
```c
// 采用LWIP网络通讯库
// 使用 modbus poll工具调试通过
// 该工程可直接作为模板开发
#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/api.h"
#include "lwip/sys.h"
#include "lwip/tcp.h"
#include "lwip/sockets.h"
#include "lwip/inet.h"
#define MODBUS_TCP_PORT 502
static void modbus_tcp_thread(void *arg)
{
int sock, newsock, n;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
char recv_buf[1024];
char send_buf[1024];
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("socket error\n");
return;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MODBUS_TCP_PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
printf("bind error\n");
return;
}
if (listen(sock, 1) < 0) {
printf("listen error\n");
return;
}
while (1) {
client_addr_len = sizeof(client_addr);
newsock = accept(sock, (struct sockaddr *)&client_addr, &client_addr_len);
if (newsock < 0) {
printf("accept error\n");
continue;
}
while (1) {
n = recv(newsock, recv_buf, sizeof(recv_buf), 0);
if (n <= 0) {
printf("recv error\n");
break;
}
// 处理接收到的数据
// ...
n = send(newsock, send_buf, strlen(send_buf), 0);
if (n <= 0) {
printf("send error\n");
break;
}
}
close(newsock);
}
}
void modbus_tcp_init(void)
{
sys_thread_new("modbus_tcp_thread", modbus_tcp_thread, NULL, 1024, 2);
}
```
阅读全文