遇到我需要将双数组转换为字符的问题

Running into an issue where I need to convert double array into char

本文关键字:转换 字符 问题 数组 遇到      更新时间:2023-10-16

所以我的问题是我不知道如何让文本文件(里面装满了 1000 个用 Venus 2 编写的逐行命令)正确输入到我试图创建的 while 循环中,用于重复向电机发送位置命令。有一个文本文件,其中包含用Venus 2命令编写的坐标(鲜为人知的语言;使用起来非常烦人)。我不是 c++ 计算机编程的爱好者,但我确实知道大多数基本和中级 c++ 的理想。 如果您有任何建议或意见,请告诉我您的想法!!

另外,这是文件外观的片段

0.0229 1 纳米 0.0317 2 纳米
0.0276 1 纳米 0.0311 2 纳米
0.0323 1 纳米 0.0302 2 纳米
0.0347 1 纳米 0.0310 2 纳米
0.0364 1 纳米 0.0310 2 纳米
0.0422 1 纳米 0.0343 2 纳米
0.0466 1 纳米 0.0324 2 纳米

位置, 马达 ID, 运动类型,

位置, 电机 ID, 运动类型
(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;
}

首先,考虑是否需要将其转换为双精度。 我看到您正在构建要在串行端口上发送的字符串消息。

此示例应向您展示如何执行要执行的操作。 如果您需要这些位置供以后使用,那么我建议不要使用双精度数组。 相反,读取每一行,将其分解为字符串组件,然后构建字符串消息以发送到串行端口。 如果您确实需要双倍的位置,请将它们推到双倍<双倍>的后面,以便以后使用。

#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;
}