C++ C~Arduino:调用函数后停止
我在调试这段代码时遇到了困难,我希望有人能指出我在哪里犯了错误。 我的串行输出是:C++ C~Arduino:调用函数后停止,c++,arduino,C++,Arduino,我在调试这段代码时遇到了困难,我希望有人能指出我在哪里犯了错误。 我的串行输出是: connected GetData() got data... next stage... 我已经用一个简单的网络客户端进行了测试,该客户端循环发送hello world,效果很好。每次尝试向客户端发送值后,此代码都会停止。我知道getData导致了这个问题,但我无法找出原因,或者如何最好地解决它。 为代码转储道歉,我想彻底检查一下 #include <LiquidCrystal.h> #incl
connected
GetData()
got data...
next stage...
我已经用一个简单的网络客户端进行了测试,该客户端循环发送hello world,效果很好。每次尝试向客户端发送值后,此代码都会停止。我知道getData导致了这个问题,但我无法找出原因,或者如何最好地解决它。
为代码转储道歉,我想彻底检查一下
#include <LiquidCrystal.h>
#include <dht11.h>
#include <SPI.h>
#include <Ethernet.h>
dht11 DHT11;
#define DHT11PIN 8
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,1,100);
IPAddress ip(192,168,1,50);
EthernetClient client;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int trigPin = 6;
const int echoPinOne = 7;
const int echoPinTwo = 8;
const int echoPinThree = 9;
const int echoPinFour = 10;
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
delay(1000);
//Network
if (Ethernet.begin(mac) == 0) {
Serial.println("DHCP FAIL");
Ethernet.begin(mac, ip);
}
delay(1000);
Serial.println("connecting...");
lcd.setCursor(0, 0);
lcd.print("connecting");
connect();
}
void connect(){
if (client.connect(server, 8080)) {
Serial.println("connected");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("connected");
client.println("hello server");
client.println();
delay(500);
lcd.clear();
}
else {
Serial.println("connection failed");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connection Failure");
}
delay(500);
}
void loop() {
//network
if(!client.available()){
Serial.println("lost connection!-restart");
lcd.println("Disconnection");
client.stop();
connect();
}
else {
int inp[5];
getData(inp);
Serial.println("next stage...");
char *output = (char *) &inp[0];
client.println(output);
client.println("first");
Serial.println("sent first...");
//here repeats the same process for the other sensors - omitted for clear reading
}
//end network
delay(500);
}
void getData(int *inputBuf){
Serial.println("GetData()");
long durationThree, inchesThree, cmThree = 0;
long durationOne, inchesOne, cmOne = 0;
long durationTwo, inchesTwo, cmTwo = 0;
long durationFour, inchesFour, cmFour = 0;
//SENSOR ONE
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPinOne, INPUT);
durationOne = pulseIn(echoPinOne, HIGH);
delay(100);
//SENSOR TWO
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPinTwo, INPUT);
durationTwo = pulseIn(echoPinTwo, HIGH);
delay(100);
//SENSOR THREE
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPinThree, INPUT);
durationThree = pulseIn(echoPinThree, HIGH);
delay(100);
//SENSOR FOUR
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPinFour, INPUT);
durationFour = pulseIn(echoPinFour, HIGH);
//DISTANCE CALCULATION
cmOne = microsecondsToCentimeters(durationOne);
cmTwo = microsecondsToCentimeters(durationTwo);
cmThree = microsecondsToCentimeters(durationThree);
cmFour = microsecondsToCentimeters(durationFour);
//Serial.println("sending...");
//char *out = (char *) &cmOne;
//Serial.println(out);
//client.println(out);
//Serial.println("sent!"); used this to test - same issue
Serial.println("got data...");
inputBuf[0] = cmOne;
inputBuf[1] = cmTwo;
inputBuf[2] = cmThree;
inputBuf[3] = cmFour;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
我不知道println是如何工作的,但是假设它需要一个以nul结尾的字节序列,所以您需要在这个声明之后以nul结尾
int inp[5]
加上这个
inp[4] = '\0';
或者你可以把它加在这里
inputBuf[0] = cmOne;
inputBuf[1] = cmTwo;
inputBuf[2] = cmThree;
inputBuf[3] = cmFour;
inputBuf[4] = 0; // <--- here
请格式化您的代码,它有太多的空行一个失去了轨道的哪一行属于哪个块,我找不到任何声明的inputBuf.Modified。对不起,我一天中大部分时间都在尝试错误,但遗漏了几个部分。将采取像字符,整型,字符串等类型我已经尝试了你的解决方案,但我得到了完全相同的result@Simon我没有注意到Serial.PrimtLn这是C++为什么你要标记C?而且,为什么你希望它在循环之后确实做了一些事情,你又调用了循环?这是包含C和C++的ARDUNO。安装后会反复调用循环,直到断电或关机。我也在arduino stack exchange页面上发布了这个问题,但它仍处于测试阶段&我确信这个问题是基于代码的,很可能是从该函数获取数据,getData我可以确认这是一个代码问题,因为提取从一个传感器获取数据的代码,然后直接将值发送出去工作正常。