mbed:在接收UDP广播消息时触发中断
每次在STM32板(NucleoF429ZI)的给定端口上收到广播消息时,我都试图触发一个中断函数。我使用的通信协议是UDP,mbed库是mbed:在接收UDP广播消息时触发中断,udp,interrupt,stm32,ethernet,mbed,Udp,Interrupt,Stm32,Ethernet,Mbed,每次在STM32板(NucleoF429ZI)的给定端口上收到广播消息时,我都试图触发一个中断函数。我使用的通信协议是UDP,mbed库是UDPSocket,它继承自Socket 有人知道如何实现它吗 编辑: 多亏了PeterJ的评论,我找到了类Socket的一个有趣(但已弃用)成员函数,名为attach()。此方法在套接字状态更改时注册回调(recv/send/accept) 因为我在套接字上有一个传入的广播,所以在我的情况下没有状态变化(只接收数据,从不发送)。是否有一种方法可以使用此att
UDPSocket
,它继承自Socket
有人知道如何实现它吗
编辑:
多亏了PeterJ的评论,我找到了类Socket
的一个有趣(但已弃用)成员函数,名为attach()
。此方法在套接字状态更改时注册回调(recv/send/accept)
因为我在套接字上有一个传入的广播,所以在我的情况下没有状态变化(只接收数据,从不发送)。是否有一种方法可以使用此attach()
方法来检测收到的每条消息
// Open Ethernet connection
EthernetInterface eth;
eth.connect();
// Create an UDP socket for listening to the broadcast
UDPSocket broadcastSocket;
broadcastSocket.open(ð);
broadcastSocket.bind(BROADCAST_PORT);
// Function to call when a broadcast message is received
broadcastSocket.attach(&onUDPSocketEvent);
void onUDPSocketEvent(){
printf("UDP event detected\n");
}
attach
已被替换,但我认为它无法满足您的要求。一个好方法是旋转一个新线程,并使用该线程处理套接字
void onUDPSocketEvent(void* buffer, size_t size) {
printf("UDP event detected\n");
}
void udp_main() {
// Open Ethernet connection
EthernetInterface eth;
eth.connect();
// Create an UDP socket for listening to the broadcast
UDPSocket broadcastSocket;
broadcastSocket.open(ð);
broadcastSocket.bind(BROADCAST_PORT);
void* recvBuffer = malloc(1024);
while (1) {
// this blocks until next packet comes in
nsapi_size_or_error_t size = broadcastSocket.recvfrom(NULL, recvBuffer, 1024);
if (size < 0) {
printf("recvfrom failed with error code %d\n", size);
}
onUDPSocketEvent(recvBuffer, size);
}
}
int main() {
Thread t; // can pass in the stack size here if you run out of memory
t.start(&udp_main);
while (1) {
wait(osWaitForever);
}
}
void onUDPSocketEvent(void*缓冲区,大小){
printf(“检测到UDP事件\n”);
}
void udp_main(){
//开放式以太网连接
以太网接口;
eth.connect();
//创建用于收听广播的UDP套接字
UDPSocket广播套接字;
广播套接字。打开(ð);
broadcastSocket.bind(广播端口);
void*recvBuffer=malloc(1024);
而(1){
//这会一直阻塞,直到下一个数据包到达
nsapi_size_或_error_t size=broadcastSocket.recvfrom(NULL,recvBuffer,1024);
如果(尺寸<0){
printf(“recvfrom失败,错误代码为%d\n”,大小);
}
onUDPSocketEvent(recvBuffer,大小);
}
}
int main(){
线程t;//如果内存不足,可以在此处传入堆栈大小
t、 启动(&udp_main);
而(1){
等待(永远等待);
}
}
(请注意,回调函数不是在ISR中运行的——因此不是在中断上下文中运行的——但我假设您实际上并不希望这样)
编辑:我已经创建了一个演示如何在单独的线程上侦听UDP消息的程序。
attach
已被替换为,但我认为它不会满足您的需要。一个好方法是旋转一个新线程,并使用该线程处理套接字
void onUDPSocketEvent(void* buffer, size_t size) {
printf("UDP event detected\n");
}
void udp_main() {
// Open Ethernet connection
EthernetInterface eth;
eth.connect();
// Create an UDP socket for listening to the broadcast
UDPSocket broadcastSocket;
broadcastSocket.open(ð);
broadcastSocket.bind(BROADCAST_PORT);
void* recvBuffer = malloc(1024);
while (1) {
// this blocks until next packet comes in
nsapi_size_or_error_t size = broadcastSocket.recvfrom(NULL, recvBuffer, 1024);
if (size < 0) {
printf("recvfrom failed with error code %d\n", size);
}
onUDPSocketEvent(recvBuffer, size);
}
}
int main() {
Thread t; // can pass in the stack size here if you run out of memory
t.start(&udp_main);
while (1) {
wait(osWaitForever);
}
}
void onUDPSocketEvent(void*缓冲区,大小){
printf(“检测到UDP事件\n”);
}
void udp_main(){
//开放式以太网连接
以太网接口;
eth.connect();
//创建用于收听广播的UDP套接字
UDPSocket广播套接字;
广播套接字。打开(ð);
broadcastSocket.bind(广播端口);
void*recvBuffer=malloc(1024);
而(1){
//这会一直阻塞,直到下一个数据包到达
nsapi_size_或_error_t size=broadcastSocket.recvfrom(NULL,recvBuffer,1024);
如果(尺寸<0){
printf(“recvfrom失败,错误代码为%d\n”,大小);
}
onUDPSocketEvent(recvBuffer,大小);
}
}
int main(){
线程t;//如果内存不足,可以在此处传入堆栈大小
t、 启动(&udp_main);
而(1){
等待(永远等待);
}
}
(请注意,回调函数不是在ISR中运行的——因此不是在中断上下文中运行的——但我假设您实际上并不希望这样)
编辑:我创建了一个函数,它显示了如何在单独的线程上侦听UDP消息。您需要在库中找到回调函数名(或回调机制)。没有什么比“UDP中断”更好的了,因为STM32 uC没有内置硬件IP堆栈。谢谢@PeterJ,你的评论帮助我更进一步。我刚刚编辑了这个问题。查看库源代码。您需要在库中找到回调函数名(或回调机制)。没有什么比“UDP中断”更好的了,因为STM32 uC没有内置硬件IP堆栈。谢谢@PeterJ,你的评论帮助我更进一步。我刚刚编辑了这个问题。看看库的源代码。