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.

94 lines
2.2 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 "FallbackStructData.h"
#include <sstream>
#include <iomanip>
#include <ctime>
#include "Utils.h"
FallbackStructData::FallbackStructData()
{
}
FallbackStructData::FallbackStructData(FallbackParam_t *data)
{
setData(*data);
}
Data* FallbackStructData::clone()
{
Data* data = new FallbackStructData(&m_data);
return data;
}
void FallbackStructData::copy(Data* data)
{
FallbackStructData *dt = dynamic_cast<FallbackStructData *>(data);
std::memcpy(&m_data, &dt->getData(), sizeof(FallbackParam_t));
}
void FallbackStructData::copy(Data& data)
{
FallbackStructData *dt = dynamic_cast<FallbackStructData *>(&data);
std::memcpy(&m_data, &dt->getData(), sizeof(FallbackParam_t));
}
int FallbackStructData::length()
{
return 10;
}
std::string FallbackStructData::toStr()
{
std::stringstream ss;
ss << "angle: " << m_data.angle << ", ampere: " << (double)(m_data.ampere / 10) << std::endl;
return ss.str();
}
// 1. 第0-1个字节/共2字节 角度
// 2. 第2字节/共1字节 电流
std::string FallbackStructData::toByteStr()
{
std::string s;
s.append(Utils::ushortToStr(m_data.angle));
s.append(Utils::uintToStr(m_data.ampere));
s.append(Utils::uintToStr(m_data.ampere2));
return s;
}
/*[2023-05-26 10:33:20] Welcome to TMS-KEY 1.0
[2023-05-26 10:33:25] Device Opened!
[2023-05-26 10:33:25] 打开串口成功
[2023-05-26 10:33:34] 发送消息:设置参数
[2023-05-26 10:33:34] Read Data!
[2023-05-26 10:33:34] 接收数据 - cmd: 设置回退参数
[2023-05-26 10:33:34] PORT:COM4 >> Recv Data: CMD=设置回退参数
[2023-05-26 10:33:34] PORT:COM4 >> SET PARAM FALLBACK SUCCESS*/
std::string FallbackStructData::toHexStr()
{
return Utils::strToHexStr(toByteStr());
}
bool FallbackStructData::fromByteStr(std::string str)
{
if(str.length() < 10)
{
return false;
}
std::string s;
s = str.substr(0, 2);
m_data.angle = Utils::strToUshort(s);
s = str.substr(2,4 );
m_data.ampere = Utils::strToUint(s);
s = str.substr(5,4 );
m_data.ampere2 = Utils::strToUint(s);
return true;
}
void FallbackStructData::setData(FallbackParam_t& data)
{
memcpy(&m_data, &data, sizeof(FallbackParam_t));
}