需要一种解决方案来同步从iPerf和GPS接收的数据

时间:2017-02-07 19:05:19

标签: linux gps iperf gpsd

最近,我正在进行车对X通信并使用两个车载单元(OBU)。可以使用gpsd,gpsd客户端读取和监控两个模块上的GPS数据,还可以使用iPerf测量网络性能。要做到这一点,一个设备充当服务器,而另一个设备充当客户端。现在,我想用两台不同的笔记本电脑连接这两台设备,测量网络性能,并读取OBU外的GPS数据。此外,我需要在GPS数据和iPerf输出之间进行同步,以便我可以检测TPV(时间,位置,速度)明智的吞吐量,延迟等。现在我需要同步GPS和iPerf数据,以便我可以将它们存储为日志 - 稍后提交并处理数据。是否有人可以帮助我了解如何做到这一点。我已经尝试过一种解决方案--GPS& iperf数据,并存储到日志。我有第二个解决方案,即使用-f选项将GPS数据作为iperf有效载荷发送。我想尝试第二个但不完全确定如何做到这一点。真的很感激任何人的帮助。

1 个答案:

答案 0 :(得分:0)

您没有提到您熟悉的脚本语言或编程语言。有很多方法可以做到这一点,从简单的python或bash脚本到更复杂的C ++程序。

我不想写一整套演示软件,因为你不知道具体到底是什么,但是我能够接受这部分sample c++ gpsd client并对其进行修改,以便它也运行iperf并输出为csv(然后你可以管道到记录器,或者你想要做它)。

#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>
#include <memory>

#include <libgpsmm.h>

std::string exec(const char *cmd) {
  std::array<char, 128> buffer;
  std::string result;
  std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);

  if (!pipe) throw std::runtime_error("popen() failed!");

  while (!feof(pipe.get())) {
    if (fgets(buffer.data(), 128, pipe.get()) != NULL) result += buffer.data();
  }
  return result;
}

int main(int argc, char **argv)
{
  if (argc < 2) {
    std::cout << "Please execute command with a remote iperf server.  Exiting." <<
    "\n";
    return 1;
  }

  gpsmm gps_rec("localhost", DEFAULT_GPSD_PORT);

  if (gps_rec.stream(WATCH_ENABLE | WATCH_JSON) == NULL) {
    std::cerr << "No GPSD running.\n";
    return 1;
  }

  for (;;) {
    // run iperf and get output in a string (note: needs error checking!)
    // the cut portion is there to remove the first column from the csv output
    // which is time, as we'll take the gps time instead.
    std::stringstream iperfCmd;
    iperfCmd << "iperf -c " << argv[1] << " -ey C | cut -d , -f 2-9";
    const std::string& tmp  = iperfCmd.str();
    const char *iperfCmdArr = tmp.c_str();
    std::string iperfOutput = exec(iperfCmdArr);

    struct gps_data_t *newdata;

    if (!gps_rec.waiting(50000000)) continue;

    if ((newdata = gps_rec.read()) == NULL) {
      std::cerr << "Read error.\n";
      return 1;
    } else {
      while (((newdata = gps_rec.read()) == NULL) ||
             (newdata->fix.mode < 1)) {
        // Do nothing; don't want to output wrong initial time
      }
      timestamp_t ts   = newdata->fix.time;
      double latitude  = newdata->fix.latitude;
      double longitude = newdata->fix.longitude;

      // convert GPSD's timestamp_t into time_t
      time_t seconds;
      seconds = (time_t)ts;
      auto tm = *std::localtime(&seconds);

      std::ostringstream oss;
      oss << std::put_time(&tm, "%d-%m-%Y %H:%M:%S");
      auto time_str = oss.str();

      // set decimal precision
      std::cout.precision(6);
      std::cout.setf(std::ios::fixed, std::ios::floatfield);
      std::cout << time_str << "," <<
        latitude << "," <<
        longitude << "," << iperfOutput;

      // uncomment below to break out of for loop (will only output 1 iteration)
      // return 0;
    }
  }
  return 0;
}

您可以修改iperfCmd变量以更改iperf命令。例如,如果要运行双向测试,可以将其更改为“-rey C”。看看你需要的iperf --help,但你必须保留CSV输出的“-y C”标志。如果你有gpsd(有时是必要的libgps-dev库),你可以这样编译代码:

g++ -Wall -std=c++14 -pedantic $(pkg-config --cflags --libs libgps) gpsd-iperf-example.cpp -o gpsd-iperf-example

然后运行(按CTRL-C停止):

gpsd-iperf-example servername

假设gpsd工作正常,并且您使用的是iperf v2,则输出应如下所示:

07-05-2017 19:05:10,45.3XXXXX,-75.8XXXXX,192.168.2.36,40466,192.168.2.28,5001,4,0.0-10.0,286785536,228615722
07-05-2017 19:05:11,45.3XXXXX,-75.8XXXXX,192.168.2.36,40468,192.168.2.28,5001,4,0.0-10.0,328859648,261792317
07-05-2017 19:05:12,45.3XXXXX,-75.8XXXXX,192.168.2.36,40470,192.168.2.28,5001,4,0.0-10.0,293601280,234307789

您可以进一步解析或简单地调整c ++程序。不确定这是否是您正在寻找的,但同样,有很多方法可以做,只需查看代码片段并根据您的需求进行调整。