You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.9 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "TorqueModeStructData.h"
#include <sstream>
#include <iomanip>
#include <ctime>
#include <QDebug>
#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<TorqueModeStructData *>(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<TorqueModeStructData *>(&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<TorqueModeParam>::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<TorqueModeParam>::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<nCount; i++)
{
std::string name = str.substr(m_prefixLength + (10 + 4 + 2) * i, 10);
std::string strValue = str.substr(m_prefixLength + (10 + 4 + 2) * i + 10, 4);
std::string strValueAmpere = str.substr(m_prefixLength + (10 + 4 + 2) * i + 14, 2);
int value = Utils::byteStrToInt(strValue);
unsigned short valueAmpere = Utils::byteStrToShort(strValueAmpere);
TorqueModeParam param(name, value, valueAmpere);
m_data.datas.push_back(param);
}
return true;
}
void TorqueModeStructData::setData(TorqueModeParam_t& data)
{
m_data.datas.clear();
std::copy(data.datas.begin(), data.datas.end(), std::back_inserter(m_data.datas));
}