中断时发送 UDP 数据包
Sending UDP packet upon interupt
我正在尝试在使用mbed平台触发中断时发送UDP数据包。
但是,当我尝试从中断函数key_pressed
调用udp_send
时,我收到sys_arch_protect错误。
这可能是因为 UDPsocket 的某些部分没有传递给中断函数吗?
为了简洁起见,我省略了大部分代码
提前感谢,格雷格
/*--INCLUDES----------------------------------------------------------------------------*/
#include "mbed.h"
#include "EthernetInterface.h"
/*--CONSTANTS---------------------------------------------------------------------------*/
const int BROADCAST_PORT = 58083;
char pin_status[1] = {0};
InterruptIn push_button(SW3);
/*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
void udp_send(void);
void keyPressed(void);
void keyReleased(void);
/*--------------------------------------------------------------------------------------*/
void keyPressed(void)
{
printf("Key Pressedrn"); //debug
udp_send(); //calling the function to send UDP packet, this casuses errors
}
/*--------------------------------------------------------------------------------------*/
void keyReleased( void )
{
printf("Key Releasedrn"); //debug
}
/*--------------------------------------------------------------------------------------*/
void udp_send(void) //sends UDP broadcast packet
{
UDPSocket sock;
sock.init();
sock.set_broadcasting();
Endpoint broadcast;
broadcast.set_address("255.255.255.255", BROADCAST_PORT); //broadcast UDP to all
sock.sendTo(broadcast, pin_status, sizeof(pin_status)); //pin_status changed elsewhere
}
/*--------------------------------------------------------------------------------------*/
int main() {
EthernetInterface eth;
eth.init();
eth.connect();
printf("IP Address is %srn", eth.getIPAddress());
udp_send(); //test call to confirm UDP_send function is working (with Wireshark)
while( 1 )
{
push_button.rise(keyPressed); //debounce omitted, calls interupt
push_button.fall(keyReleased);
//other stuff
}
}
非常感谢
@Tony d,我想无论如何在中断例程中包含大函数调用都不是好习惯。
为了将来参考,附加了代码;
#include "mbed.h"
#include "EthernetInterface.h"
#include "rtos.h"
#include <string>
#include "PinDetect.h"
/*--CONSTANTS---------------------------------------------------------------------------*/
const int BROADCAST_PORT = 58083;
char pin_status[1] = {0};
int global_flag;
InterruptIn push_button(SW3);
/*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
void udp_send(void);
void keyPressed(void);
void keyReleased(void);
EthernetInterface eth;
Endpoint broadcast;
UDPSocket sock;
/*--------------------------------------------------------------------------------------*/
void keyPressed(void)
{
printf("Key Pressedrn"); //debug
udp_send();
}
/*--------------------------------------------------------------------------------------*/
void keyReleased( void )
{
printf("Key Releasedrn"); //debug
}
/*--------------------------------------------------------------------------------------*/
void udp_send(void)
{
global_flag = 1; //sends UDP broadcast packet
//pin_status changed elsewhere
}
/*--------------------------------------------------------------------------------------*/
int main() {
eth.init();
eth.connect();
sock.init();
sock.set_broadcasting();
broadcast.set_address("255.255.255.255", BROADCAST_PORT); //broadcast UDP to all
printf("IP Address is %srn", eth.getIPAddress());
udp_send(); //test call to confirm UDP_send function is working
while( 1 )
{
push_button.rise(keyPressed); //debounce omitted, calls interupt
push_button.fall(keyReleased);
if (global_flag)
{
global_flag = 0;
sock.sendTo(broadcast, pin_status, sizeof(pin_status));
}
}
}
相关文章:
- 发送固定大小的 UDP 数据包
- 使用C++将UDP数据包存储在Structure中
- 在高数据包速率下最大限度地减少丢弃的 UDP 数据包 (Windows 10)
- Qt 在可预测的秒数后跳过 UDP 数据包
- 高频接收UDP数据包:丢包?
- 为 posix recv 设置超时会导致 udp 数据包丢失吗?
- 了解 UDP 数据包大小限制的 TCP 数据包大小限制以及它在 boost::asio 编程级别的含义
- 获取进入UDP数据包的目标端口
- 操纵Windows Explorer窗口时,UDP数据包会掉落
- C++ 使用 recvmmsg 丢弃 UDP 数据包
- 如何在QT中接收适当的UDP数据包
- 使用ASIO捕获大量UDP数据包
- 如何正确接收多播UDP数据包
- 服务器未收到UDP数据包,花费数小时试图找出问题所在
- 当接收缓冲区几乎满时,阻止UDP数据包被部分截断
- 如何检查UDP数据包是否在发送器上发送
- 使用 QNX 实时操作系统实时接收 UDP 数据包
- 高效地发送UDP数据包流
- 为什么我在使用 winsock 时没有收到 UDP 数据包?
- SDL_net UDP 数据包数据