所以我的问题在于我不知道如何将文本文件(其中包含1000个用金星2编写的逐行命令)正确输入到我尝试创建的while循环中用于重复向电机发送位置命令。这是一个文本文件,其中的坐标是用Venus 2命令编写的(鲜为人知的语言;非常讨厌使用)。我不是c ++计算机编程的狂热爱好者,但我确实知道大多数基础和中级c ++理念。如果您有任何建议或意见,请告诉我您的想法!!
另外,这里是文件外观的片段
0.0229 1nm 0.0317 2nm
0.0276 1 nm 0.0311 2 nm
0.0323 1 nm 0.0302 2 nm
0.0347 1 nm 0.0310 2 nm
0.0364 1 nm 0.0310 2 nm
0.0422 1 nm 0.0343 2 nm
0.0466 1 nm 0.0324 2 nm
位置,motorID,运动类型,位置,motorID,运动类型
(NI Pollux II电机x2)
//text file is opened
int i = 0;
double c1[1000];
char line;
cout << "Press any key to begin jitter.\n";
cin.get();
cout << "Running the jitter data...\n";
/* here there should be a conversion from double to char */
while (!jitterFile.eof()) {
jitterFile >> c1[i];
/* or here there should be a conversion */
if (serial.Open(18, 19200)){
static char szMessage[] = /* here is where the converted data should go */;
int nBytesSent = serial.SendData(szMessage, strlen(szMessage));
assert(nBytesSent == strlen(szMessage));
}
else{
cout << "error occurred with runJitter()...\n";
}
cout << c1[i] << endl;
++i;
}
//Sleep(1000);
jitterFile.close();
return 0;
}
答案 0 :(得分:0)
首先,考虑是否需要将其转换为double。我看到你正在构建一个字符串消息以便在串口上发送。
此示例应向您展示如何执行您要执行的操作。如果您需要这些位置供以后使用,那么我建议不要使用双数组。相反,读取每一行,将其分解为其字符串组件,然后构建您的字符串消息以发送到串行端口。如果你确实需要这些位置作为双打,那么将它们推到deque的后面&lt;双&gt;供以后使用。
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <deque>
#include <cstdlib>
using namespace std;
size_t sendToSerial(const char* msg, size_t len) {
return len;
}
int main(int argc, const char** argv) {
ifstream jitterFile("jitter.txt");
int i = 0;
std::deque< double > c1Deque;
//double c1[1000];
//char line;
cout << "Press any key to begin jitter.\n";
cin.get();
cout << "Running the jitter data...\n";
/* here there should be a conversion from double to char */
string line;
string pos1;
string motor1;
string motion1;
string pos2;
string motor2;
string motion2;
while (getline(jitterFile, line)) {
std::istringstream sstrm(line);
sstrm >> pos1 >> motor1 >> motion1 >> pos2 >> motor2 >> motion2;
/* or here there should be a conversion */
c1Deque.push_back(atof(pos1.c_str()));
if (true/*serial.Open(18, 19200)*/){
std::string message = pos1;
message += ",";
message += pos2;
int nBytesSent = sendToSerial/*serial.SendData*/(message.c_str(), message.length());
//assert(nBytesSent == strlen(szMessage));
}
else{
cout << "error occurred with runJitter()...\n";
}
cout << pos1 << endl;
++i;
}
//Sleep(1000);
std::cout << "There are " << c1Deque.size() << " entries in the deque" << std::endl;
jitterFile.close();
return 0;
}