是否有任何可用的 mbed 以太网接口库?
Is there any working mbed Ethernet Interface library?
我正在做一个关于 STM32F767ZI 的项目。我编译代码时遇到的错误是 "EthernetInterface library is not supported on your target"。我研究了 google 并找到了某人 F7_Ethernet 库,但是由于库中的错误,程序仍然无法编译。
代码如下:
//Example - Voice Controlled Door Access
#include "mbed.h"
//#include "EthernetInterface.h"
//#include "F7_Ethernet.h"
#include <stdio.h>
#include <string>
#include "rtos.h"
#include "Servo.h"
#if defined(TARGET_NUCLEO_F767ZI)
Servo myservo(D9);
#endif
#define PORT 80
void web_server(void const *args) {
TCPSocketServer server;
TCPSocketConnection client;
server.bind(PORT);
server.listen();
while(true){
printf("Waiting for connection...\r\n");
int32_t status = server.accept(client);
printf("Connection from: %s\r\n", client.get_address());
if (status>=0)
{
char buffer[1024] = {};
int n= client.receive(buffer, 1023);
printf("Received Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
//GET /q=open+sesame HTTP/1.1
char item[13];
for(int k=0; k<13; k++){
item[k]= buffer[k+5];
}
char Body[1024] = {};
if (strcmp(item,"q=open+sesame")==0){
sprintf(Body,"<html><title></title><body><h1>Door Open</h1></body></html>\n\r\n\r");
//move the servo to open the door,
myservo = 1;
// wait for 5 seconds, close the door
wait(5);
//close the door
myservo = 0;
}
else{
sprintf(Body,"<html><title></title><body><h1>Door Not Open</h1></body></html>\n\r\n\r");
//do nothing with the door
}
char Header[256] = {};
sprintf(Header,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent- Type: text/html\n\rConnection: Keep-Alive\n\r\n\r",strlen(Body));
client.send(Header,strlen(Header));
client.send(Body,strlen(Body));
client.close();
}
}
}
int main() {
EthernetInterface eth;
eth.init();
eth.connect();
printf("\r\nServer IP Address is %s\r\n", eth.getIPAddress());
//close the door
myservo = 0;
//wait for instructions
web_server("");
while(1){}
}
我假设您使用的是最新的 Mbed OS 版本 (5.11~)。我建议使用 NetworkInterface
(doc) 和 TCPSocket
。
初始化网络接口后,使用TCPSocket
打开连接connect()
、发送数据send()
和接收数据recv()
。它也可以 bind()
、accept()
和 listen()
。
您可以在此处找到示例程序:
https://os.mbed.com/docs/mbed-os/v5.14/apis/tcpsocket.html#server-socket
我正在做一个关于 STM32F767ZI 的项目。我编译代码时遇到的错误是 "EthernetInterface library is not supported on your target"。我研究了 google 并找到了某人 F7_Ethernet 库,但是由于库中的错误,程序仍然无法编译。
代码如下:
//Example - Voice Controlled Door Access
#include "mbed.h"
//#include "EthernetInterface.h"
//#include "F7_Ethernet.h"
#include <stdio.h>
#include <string>
#include "rtos.h"
#include "Servo.h"
#if defined(TARGET_NUCLEO_F767ZI)
Servo myservo(D9);
#endif
#define PORT 80
void web_server(void const *args) {
TCPSocketServer server;
TCPSocketConnection client;
server.bind(PORT);
server.listen();
while(true){
printf("Waiting for connection...\r\n");
int32_t status = server.accept(client);
printf("Connection from: %s\r\n", client.get_address());
if (status>=0)
{
char buffer[1024] = {};
int n= client.receive(buffer, 1023);
printf("Received Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
//GET /q=open+sesame HTTP/1.1
char item[13];
for(int k=0; k<13; k++){
item[k]= buffer[k+5];
}
char Body[1024] = {};
if (strcmp(item,"q=open+sesame")==0){
sprintf(Body,"<html><title></title><body><h1>Door Open</h1></body></html>\n\r\n\r");
//move the servo to open the door,
myservo = 1;
// wait for 5 seconds, close the door
wait(5);
//close the door
myservo = 0;
}
else{
sprintf(Body,"<html><title></title><body><h1>Door Not Open</h1></body></html>\n\r\n\r");
//do nothing with the door
}
char Header[256] = {};
sprintf(Header,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent- Type: text/html\n\rConnection: Keep-Alive\n\r\n\r",strlen(Body));
client.send(Header,strlen(Header));
client.send(Body,strlen(Body));
client.close();
}
}
}
int main() {
EthernetInterface eth;
eth.init();
eth.connect();
printf("\r\nServer IP Address is %s\r\n", eth.getIPAddress());
//close the door
myservo = 0;
//wait for instructions
web_server("");
while(1){}
}
我假设您使用的是最新的 Mbed OS 版本 (5.11~)。我建议使用 NetworkInterface
(doc) 和 TCPSocket
。
初始化网络接口后,使用TCPSocket
打开连接connect()
、发送数据send()
和接收数据recv()
。它也可以 bind()
、accept()
和 listen()
。
您可以在此处找到示例程序: https://os.mbed.com/docs/mbed-os/v5.14/apis/tcpsocket.html#server-socket