用引用调用函数后值错误

时间:2019-02-09 09:55:30

标签: arduino

我尝试使用固件版本来构建文件名,但是在调用带有引用的函数后会出现一些奇怪的字符。

globals中的结构。h

struct Versions {
  char* remoteVersion;
  char* currentVersion;
  char* newVersion;
  byte versionLength;
  int response;
};
wifiFwUpdater.h中的

void getFirmwareVersion(Versions &v) {
  if (connect(hostConfig.host, hostConfig.port)) {
    get(hostConfig.host, hostConfig.port, hostConfig.pathVersionWiFi);
    parseResponseForVersion(v);
  }
}

void wifiUpdater() {
  Versions versions;
  versions.versionLength = 7;

  getFirmwareVersion(versions);
  Serial.print(F("Remote Firmware version: "));
  Serial.println(versions.remoteVersion);
  Serial.print(F("Response: "));
  Serial.println(versions.response);

  if (versions.response != 1) {
  ...

在客户端。h

void parseResponseForVersion(Versions &v) {
  char buf[v.versionLength];

  while (true) {
    if (client.available()) {
      if (!client.find("HTTP/1.1")) {
        v.response = 1;
        break;
      }

      int statusCode = client.parseInt();
      if (statusCode == 200 && client.find("\"version\" : \"")) {
        int l = client.readBytesUntil('"', buf, v.versionLength - 1);
        buf[l] = '\0';

        Serial.print("Content-1: ");
        Serial.println(buf);

        v.remoteVersion = buf;

        Serial.print("Content-2: ");
        Serial.println(v.remoteVersion);

        v.response = 1;
        client.stop();
        break;
      } else {
        client.stop();
        v.response = statusCode;
        break;
      }
    }

    if (!client.connected()) {
      client.stop();
      v.response = -1;
      break;
    }
  }
}

输出

Content-1: 19.5.4
Content-2: 19.5.4
Remote Firmware version: 
Response: 1

我看不到推理错误在哪里。 从char数组到char指针的简单分配不是允许的吗?

0 个答案:

没有答案