从Arduino发送UDP

问题描述 投票:0回答:1

我正在努力处理UDP数据包的传输。我正在使用“ Wireshark”作为传入数据包的控件。 UDP数据包的IP地址是我的固定PC的配置IP对吗?我更改了脚本,但仍然没有收到任何软件包:/

谢谢!

这是我的代码:

//Version 1.05

//necessary libraries
#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>

//Pin settings
#define CTD 19

//Network Settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0xEC, 0xAB };  //set MAC Address Ethernet Shield (Backside)
byte ip[]  = { XXX, XXX, X, X };                      //set IP-Address
byte gateway[] = { XXX, XXX, X, X };                  //set Gateway
byte subnet[]  = { 255, 255, 255, 0 };                //set Subnetmask


//local UDP port to listen on
unsigned int localPort = 5568;

//Recipient IP
IPAddress RecipientIP(XXX, XXX, X, X);

//Recipient UDP port
unsigned int RecipientPort = 8888;

//Buffer for sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

//EthernetUDP instance
EthernetUDP Udp;

//CTD data
int incomingData = 0;


void setup()
{
   //Start Ethernet
  Ethernet.begin(mac, ip);

  //Start UDP
  Udp.begin(localPort);

  //for debug only
  Serial.begin(9600);

  //Serial baud rate for CTD
  Serial1.begin(1200);

  //Version 1.05
Serial.print("Version 1.05");

  //CTD
  pinMode(CTD, INPUT);
}

void loop()
{

//If CTD is sending
if (Serial1.available())
{
  //read incoming data
  incomingData = Serial1.read();

  //for debug only
  Serial.print("Data: ");
  Serial.println(incomingData, BIN);
}

//Send UDP packets
int packetSize = Udp.parsePacket();
  if (packetSize) 
  {

    //Debug only
    Serial.print("Packet");
    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);

    // send to the IP address and port
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(incomingData);
    Udp.endPacket();
  }
}
arduino udp ethernet
1个回答
0
投票

安装Netcat。然后在收件人计算机上使用以下语法:sudo nc -l 8888启动arduino,您应该在shell上看到输出。

© www.soinside.com 2019 - 2024. All rights reserved.