0

So my problem here is I don't know how to get the text file (which is full of 1000 line-by-line commands written in Venus 2) to be correctly entered into the while loop I tried to create for repeatedly sending the position commands to the motors. There's a text file which has coordinates written in Venus 2 commands (little known language; very annoying to work with). I'm not an aficionado at c++ computer programming but I do know most basic and intermediate level c++ ideals. If you have any suggestions or comments, let me know what you think!!

also, here's a snippet of what the file looks like

0.0229 1 nm 0.0317 2 nm
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

position, motorID, motiontype, position, motorID, motiontype
(NI Pollux II motor 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;
}
πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
  • Do you need the array of doubles after reading the file? – Jason Jan 09 '15 at 20:52
  • `while (!jitterFile.eof())` First have a thorough read here please: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Also `char line;` looks pretty suspect, did you mean something semantically more consistent like `std::string line;` – πάντα ῥεῖ Jan 09 '15 at 20:52

1 Answers1

0

First, consider whether you need to convert it to a double. I see you're building a string message to send on the serial port.

This example should show you how to do what you're looking to do. If you need the positions for later use, then I'd recommend not using a double array. Instead, read each line, break it into its string components, and then build up your string message to send to the serial port. If you do need the positions as doubles, push them to the back of a deque< double > for use later on.

#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;
}
Jason
  • 1,076
  • 5
  • 10