使用MatchVec = std::向量匹配< >;
uint16_tstd::uint8_t crcData, std::uint16_t crcReg;
intPacketIndex = 0;
模板< typename T,无符号N >
constexprunsigned count (T const (&)[N])无例外
{
返回N;
}
MatchVecconst std::string &str, const std::string &pattern
{
MatchVec矢量;
匹配m;
Std::string::size_type索引,pos = 0;
而(index = str.find(pattern, pos)) != std::string::npos) {
m.first =指数;/ / startof Syncword
指数+ = pattern.length ();
m.second = str.substr(指数,264 + 16);/ / 264为有效载荷的大小和两个字节的CRC
vec.push_back (m);
pos =指数;
}
返回矢量;
}
uint16_tcallccrc (uint8_t crcData, uint16_t crcReg)
{
uint8_t我;
为(i = 0;我< 8;我+ +)
{
如果(((crcReg & 0x8000) >> 8) ^ (crcData & 0x80))
{
crcReg = (crcReg << 1) ^ CRC16_POLY;
}
其他的
{
crcReg = (crcReg << 1);
}
crcData< < = 1;
}
返回crcReg;
}
无符号int valueOf(const std::string &substring) {
字符retChar = ' \ 0 ';
Std::uint8_t total = 0;
int计数器= 1;
为(int i = byteSize;我> 0;我(){
如果(substring(张)= =' 1 ')总+ =计数器;
如果(substring(张)! =' ')计数器* = 2;
}
返回总;
}
无效const std::string &rx) {
无符号int ByteConvertRxBuffer [35] = {0};
uint8_trxBuffer [countof (ByteConvertRxBuffer)];
size_tireal = 0;/ /注意,最后两个字节用于CRC,这表示最后两个索引是CRC0的CRC值, CRC1.
uint16_t校验和;/ / CRC的2字节即16位。
保龄球crcOK = false;
uint8_t我;
校验和= CRC_INIT;
std:: cout < <“{”;
为(size_t我= 0;我< rx.length ();我+ = byteSize) {
字符咬(byteSize);
为(int j = 0;j < byteSize;[J] = rx[i+ J]; [J] = rx[i+ J];
std:: cout < <返回对象的值(咬);/ /打印每个包内每个字节-8位的值打交道,每个包分开。
ByteConvertRxBuffer [ireal + +] =返回对象的值(咬);
如果(i+byteSize != rx.length()) std::cout<", ";
}
std:: cout < <“}”< < endl;
为(int i = 0;我< countof (ByteConvertRxBuffer);我+ +)
{
rxBuffer[我]= ByteConvertRxBuffer[我];
}
为(i = 0;我< sizeof (rxBuffer);我+ +)
{
checksum = calcCRC(rxBuffer[i], checksum);
}
PacketIndex + +;
如果(校验和= = 0)
{
crcOK = true;
std:: cout < <“您的包号”< < PacketIndex < <”:“<<“有效的”< < endl;
}
其他的
{
crcOK = false;
std:: cout < <"你的包号"< < PacketIndex < <”:“<<“无效”< < endl;
}
}
intMain () /*int Main (int argc, char** argv){}*/
{
std:: string PacketData =“011101111001001010101010101010101010101010101100100110000101101010001110111100010000000000000011001010010000010111000011011001100100101110101100100101000011110110110001100110000111100110001100110000010111101111010000101101011100110011000000101101111110100010111100010100110000111111010110000101101111110011001111001011000011001001001100000001000100101101000110000010000111111110111111110”;
std:: string SyncWord =“10010011000010110101000111011110”;/ / syncword模式
size_t notfound = PacketData.find(SyncWord);
Assert (notfound != string::npos);//如果没有找到出口和不完整的。
汽车匹配= findPattern(PacketData, SyncWord);
为(auto &match: match) {
std:: cout < <“匹配位置:”< <匹配。第一个< < std:: endl;
std:: cout < <“字符串”< <匹配。第二个< < std:: endl;
deBin(匹配。二);
}
返回0;
}