解析接收或读取串行端口数据c ++

时间:2016-02-09 23:25:10

标签: c++ checksum

我需要一些指针第一次使用串口编程。我有一个串口数据,我如何解析这些数据,以便我可以处理单个元素进行存储和报告生成。

命令发送到串口 comand[]= FA0130AB 与crc一起执行 FA xx xx xx xx xx = command + CRC

串口接收数据 FA xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx

处理数据所需的方法 [portID| Length | Response|Price | Volume | Amount |Totalvol | Status |Check CRC]

其中 `

[portID]=xx
[Length] = xx 
[Response]= xx
[Price ]=xx xx xx  3byte with BCD format
[Volume]=xx xx xx xx  4 bytes with BCD format
[Amount]=xx xx xx xx  4 bytes with BCD format
[Totalvol]= xx xx xx xx xx  5 bytes with BCD format

`

我希望能够操作串口数据来执行以下操作 1)拆分串口数据 2)获取单个元素的价格量,金额和totalvol将它们从BCD转换为Hex到ACII或Decimal进行存储和报告

使用此功能拆分串行数据
现在我如何区分BCD的HEX值,以便我可以单独处理。 任何指针都将受到赞赏。

std::vector<std::string> explode(
    std::string delimiter, 
    std::string source, 
    int limit = std::numeric_limits<int>::max())
{
    // PHP returns FALSE in this case, but C++ doesn't handle variant types well, and a
    // reasonable facsimile of FALSE is tricky with std::vector<>, so we'll just throw.
    if (delimiter.empty())
        throw std::invalid_argument("Empty delimiter in explode()");
    // Sanitize the limit to something meaningful
    if (limit == 0)
        limit = 1;
    std::vector<std::string> result;
    // Handle the simple case of zero delimiter matches
    if (source.find(delimiter) == std::string::npos) {
        // PHP returns array() if limit is negative, array(source) if limit is positive
        if (limit > 0)
            result.push_back(source);
        return result;
    }
    std::vector<std::string>::size_type begin = 0;
    std::vector<std::string>::size_type end = 0;
    // Explode up to any positive limit, exclusive
    while (end != std::string::npos) {
        if (limit > 0 && result.size() == limit - 1) {
            // PHP populates the last element (ie. result[limit - 1]) with the remaining 
            // string when limit is positive and less than the number of total tokens.
            result.push_back(source.substr(begin));
            break;
        }
        /*
            Conventional C++ manual string tokenization.
        */
        std::vector<std::string>::size_type substr_len = std::string::npos;
        std::vector<std::string>::size_type next_tok = std::string::npos;
        end = source.find_first_of(delimiter, begin);
        if (end != std::string::npos) {
            substr_len = end - begin;
            next_tok = end + delimiter.size();
        }
        result.push_back(source.substr(begin, substr_len));
        begin = next_tok;
    }
    // Negative limits trim that amount from the right side of the result. It's
    // easier to do this after the explode loop because we don't need to retain
    // unexploded parts of the string like with a positive limit.
    if (limit < 0) {
        limit = abs(limit);
        if (limit < result.size())
            result.resize(result.size() - limit);
        else {
            // PHP returns array() if all items or more would be trimed
            result.clear();
        }
    }
    return result;
}

0 个答案:

没有答案