Stackoverflow!
在Arduino的udp通信期间,异常的奇怪字符被添加到数据包中。
嗨。堆栈溢出。在Arduino上使用Udp时遇到了无法解决的问题。我查找了很多信息来解决此问题。我还测试了各种情况。但是我解决不了。
从数据包的第24个字节开始出现问题。超过23个字符的字符将转换为奇怪的字符,并且不会输出后续句子。 udp通信的数据包传输是否限制为24个字节?如果是这样,我很高兴,但是我想知道如何解决它。请帮助我。
测试环境如下。两个Arduino通过LAN电缆连接。此外,以下源代码已上传到Arduino。串行监视器如下。
。 。 17:46:31.521->发送UDP消息
17:46:32.516->发送UDP消息
17:46:33.514->发送UDP消息
17:46:34.519->发送UDP消息
17:46:35.515->发送UDP消息
17:46:36.510->发送UDP消息
17:38:51.664-> 010010010101001010101001K;⸮<----------问题
17:38:52.662->收到的大小为31的数据包
17:38:52.662->从192.168.1.251,端口5678
17:38:52.662->内容:
17:38:52.662->01001001010100101010100j⸮<-------问题
17:38:53.663->收到的大小为31的数据包
17:38:53.663->从192.168.1.251,端口5678
17:38:53.663->内容:
17:38:53.663-> 010010010101001010101001;ے<------------问题
17:38:56.770->收到的大小为31的数据包
---------源代码-------------
///////////TX
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#define MSG_INTERVAL 1000
// network parameters
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0x05, 0x04}; // ethernet interface MAC address
IPAddress localIp(192, 168, 1, 251); // local ip address
IPAddress destIp(192, 168, 1, 15); // destination ip address
unsigned int port = 5678; // destination port
// EthernetUDP to send and receive messages.
EthernetUDP Udp;
// message string
char message[] = "0100100101010010101010010101010";
// timing
//unsigned long previousLedMillis;
//unsigned long ledInterval;
unsigned long previousSendMillis;
unsigned long sendInterval;
// setup the arduino and shields
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start ethernet and udp
Ethernet.begin(mac,localIp); // static ip version
// open UDP port
Udp.begin(port);
// show the local ip address (useful for dhcp)
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
// initialize send interval
sendInterval = MSG_INTERVAL;
}
// do tasks
void loop() {
// check if udp string has to be sent
if(millis() - previousSendMillis >= sendInterval) {
sendMessage();
}
}
// send udp string
void sendMessage() {
Serial.println("Sending UDP message");
// store current millis
previousSendMillis = millis();
// send udp message
Udp.beginPacket(destIp, port);
Udp.write(message);
Udp.endPacket();
/////RX
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 15);
IPAddress remIp(92, 168, 1, 176);
unsigned int localPort = 5678; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
void setup()
{
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
pinMode(5, OUTPUT);
Serial.begin(115200);
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
}
void loop()
{
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
//Udp.read(packetBuffer,1024);
Serial.println("Contents:");
Serial.println(packetBuffer);
//for (int i=0;i<packetSize;i++){
// Serial.print(packetBuffer[i]);
//}
//Serial.print('\n');
}
}
答案 0 :(得分:0)
是的,没错,长度24有点问题!
如果您在EthernetUDP.h文件中进行读取,则会看到:
#define UDP_TX_PACKET_MAX_SIZE 24
因此,如果要使用更多字符,请不要使用此const,请选择您的个人大小。
更改
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
到
char packetBuffer[50];
或使用另一个定义:
#define UDP_MAX_BUFFER 50 //for example
char packetBuffer[UDP_MAX_BUFFER];