#include "TorqueModeStructData.h" #include #include #include #include #include "Utils.h" TorqueModeStructData::TorqueModeStructData() : m_prefixLength(2) // 前导长度,此处为表示数量的字节数2 { } TorqueModeStructData::TorqueModeStructData(TorqueModeParam_t *data) : m_prefixLength(2) // 前导长度,此处为表示数量的字节数2 { setData(*data); // m_data.type = data->type; // m_data.datas.clear(); // //m_data.datas.insert(data->datas.begin(), data->datas.end()); // std::map // std::copy(data->datas.begin(), data->datas.end(), std::back_inserter(m_data.datas)); } Data* TorqueModeStructData::clone() { Data* data = new TorqueModeStructData(&m_data); return data; } void TorqueModeStructData::copy(Data* data) { TorqueModeStructData *dt = dynamic_cast(data); m_data.datas.clear(); //m_data.datas.insert(dt->getData().datas.begin(), dt->getData().datas.end()); // std::map std::copy(dt->getData().datas.begin(), dt->getData().datas.end(), std::back_inserter(m_data.datas)); } void TorqueModeStructData::copy(Data& data) { TorqueModeStructData *dt = dynamic_cast(&data); m_data.datas.clear(); //m_data.datas.insert(dt->getData().datas.begin(), dt->getData().datas.end()); // std::map std::copy(dt->getData().datas.begin(), dt->getData().datas.end(), std::back_inserter(m_data.datas)); } int TorqueModeStructData::length() { return (m_prefixLength + (10 + 4 + 2) * m_data.datas.size()); } std::string TorqueModeStructData::toStr() { std::stringstream ss; std::list::iterator iter = m_data.datas.begin(); while(iter != m_data.datas.end()) { ss << "name:" << (*iter).getName() << ", value:" << (*iter).getValue()<< ", valueAmpere:" << (*iter).getValueAmpere() << std::endl; iter++; } return ss.str(); } // 1. 0-3 共 4 字节:数量 // 2. 4-13 共 10 字节:扳手名称 // 3. 14-17 共 4 字节:扭矩 // 重复 2 和 3 std::string TorqueModeStructData::toByteStr() { std::string s; unsigned short int nCount = m_data.datas.size(); s.append(Utils::shortToByteStr(nCount)); std::list::iterator iter = m_data.datas.begin(); while(iter != m_data.datas.end()) { char name[11]; int value; unsigned short valueAmpere; memset(name, 0, sizeof(name)); memcpy(name, (*iter).getName().c_str(), (*iter).getName().length() <= 10 ? (*iter).getName().length() : 10); name[10] = '\0'; value = (*iter).getValue(); valueAmpere = (*iter).getValueAmpere(); s.append(name, 10); s.append(Utils::intToByteStr(value)); s.append(Utils::shortToByteStr(valueAmpere)); iter++; } return s; } std::string TorqueModeStructData::toHexStr() { std::string s = toByteStr(); return Utils::strToHexStr(s); } bool TorqueModeStructData::fromByteStr(std::string str) { if(str.length() < m_prefixLength) { return false; } std::string s; s = str.substr(0, 2); unsigned short int nCount = Utils::byteStrToShort(s); if(str.length() < (m_prefixLength + (10 + 4 + 2) * nCount)) { return false; } for(int i=0; i