在Qt创建器中创建modbus请求包

时间:2017-04-18 01:44:19

标签: c++ c++11

您好我所有使用qt创建者平台来使用Modbus容器类。我正在解析一个包含我的请求数据包的CSV文件,例如slave ID,功能代码,起始地址,地址计数和crc。我曾尝试使用qt Modbus类,但我似乎可以在Modbus功能中构建正确的数据包传递并发送到串口?

int main()
{
    struct mframe {
        unsigned short address;
        unsigned short func_code;
        unsigned short byte_count;
        unsigned int startaddress;
        unsigned short crc;
    };
    string ConfigPath = "myname.csv";
    // char getaddress(char addr);
    // bool setaddress(char addr);
    mframe frame[1000];
    int point_count = 0;
    vector<vector<string> > matrix;
    string line;
    ifstream file(ConfigPath);
    if (!file.is_open()) {
        perror("error while opening file");
    }
    getline(file, line, '\n'); // deletes first row of column headers
    while (getline(file, line, '\n')) // find the endline charcater
    {
        vector<string> vec;
        string linevec;
        istringstream ss(line); // allows for parsing each line

        while (getline(ss, linevec, ',')) {
            vec.push_back(
                linevec); // push the parsed data for that line into a vector
        }

        matrix.emplace_back(vec); // push each parsed line into another vector
        frame[point_count].address = stoi(vec[0]);
        frame[point_count].func_code = stoi(vec[1]);
        frame[point_count].byte_count = stoi(vec[2]);
        frame[point_count].startaddress = stoi(vec[3]);
        frame[point_count].crc = stoi(vec[4]);
        point_count++;
    }
}

0 个答案:

没有答案