Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 通过UDP发送传感器数据_Arduino_Udp_Declaration_Send - Fatal编程技术网

Arduino 通过UDP发送传感器数据

Arduino 通过UDP发送传感器数据,arduino,udp,declaration,send,Arduino,Udp,Declaration,Send,我正在尝试通过UDP发送传感器数据。目前,我正在努力“打包”UDP数据包。它说“incomingData”在我尝试发送时没有声明。 我希望你能给我任何建议。 代码如下。 谢谢:) //版本1.012 //必要的图书馆 #包括 #包括 #包括 //Pin设置 #定义CTD 19 //网络设置 字节mac[]={0x90,0xA2,0xDA,0x10,0xEC,0xAB}//设置MAC地址以太网屏蔽(背面) 字节ip[]={XXX,XXX,X,X}//设置IP地址 字节网关[]={XXX,XXX,X

我正在尝试通过UDP发送传感器数据。目前,我正在努力“打包”UDP数据包。它说“incomingData”在我尝试发送时没有声明。 我希望你能给我任何建议。 代码如下。 谢谢:)

//版本1.012
//必要的图书馆
#包括
#包括
#包括
//Pin设置
#定义CTD 19
//网络设置
字节mac[]={0x90,0xA2,0xDA,0x10,0xEC,0xAB}//设置MAC地址以太网屏蔽(背面)
字节ip[]={XXX,XXX,X,X}//设置IP地址
字节网关[]={XXX,XXX,X,X}//设置网关
字节子网[]={255,255,255,1}//设置子网掩码
//要侦听的本地UDP端口
无符号int localPort=4000;
//接收方IP
ipAddressRecipientIP(127,0,0,1);
//收件人UDP端口
无符号int RecipientPort=4444;
//发送数据的缓冲区
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
//EthernetUDP实例
以太网Udp;
无效设置()
{
//启动以太网
以太网开始(mac,ip);
//启动UDP
开始(localPort);
//仅用于调试
Serial.begin(9600);
//CTD的串行波特率
1.开始(1200);
//版本1.012
串行打印(“1.012版”);
//CTD
引脚模式(CTD,输入);
}
void循环()
{
//如果CTD正在发送
if(Serial1.available())
{
//读取传入数据
int incomingData=Serial1.read();
//仅用于调试
串行打印(“数据:”);
串行打印LN(输入数据,BIN);
}
//发送UDP数据包
int packetSize=Udp.parsePacket();
if(包装尺寸){
//把这个包读入packetbuffer
读取(数据包缓冲区、Udp_发送_数据包最大大小);
//发送到IP地址和端口
Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
Udp.write(输入数据);
Udp.endPacket();
}
}

在代码中,您已将
incomingData
声明为
int
inside
void loop()
函数inside
if(Serial1.available())


但如果上述if循环失败,则不会声明
incomingData
,并说
packetsize
大于零(数据包可用),然后
if(packetsize)
段将被执行。因此,
incomingData
不会被声明,但会被使用。这就是为什么您会得到您所说的错误。

incomingData
设置为全局并检查它。这是否有帮助0向下投票接受谢谢您的建议。这绝对有道理;)我仍然在努力传输UDP数据包。我使用“Wireshark”作为传入数据包的控件。UDP数据包的IP地址是我的固定PC配置的IP地址,对吗?我更改了脚本,但仍然没有收到任何包:/谢谢!这是我的密码:0否决票接受@Billa谢谢你的建议。这绝对有道理;)我仍然在努力传输UDP数据包。我正在使用“Wireshark”作为传入数据包的控件。我认为当您尝试发送数据时,
if(Serial1.available())
首先会执行if
(packetSize)
,因此,
incomingData
不会被声明,但您正在尝试在
if(packetSize)
内部使用它。因此您会得到错误。
//Version 1.012

//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, 1 };                //set Subnetmask


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

//Recipient IP
IPAddress RecipientIP(127, 0, 0, 1);

//Recipient UDP port
unsigned int RecipientPort = 4444;

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

//EthernetUDP instance
EthernetUDP Udp;



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.012
Serial.print("Version 1.012");

  //CTD
  pinMode(CTD, INPUT);
}

void loop()
{

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

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

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

    // 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();
  }
}