当前位置: 首页 > news >正文

家居网站建设定位分析论文大型网站技术架构

家居网站建设定位分析论文,大型网站技术架构,哪个网站可以做淘宝代码,百度电脑版网址作者: Herman Ye Galbot Auromix 测试环境: Ubuntu20.04 更新日期: 2023/08/30 注1: Auromix 是一个机器人爱好者开源组织。 注2: 本文在更新日期经过测试,确认有效。 笔者出于学习交流目的, 给…

作者: Herman Ye @Galbot @Auromix
测试环境: Ubuntu20.04
更新日期: 2023/08/30
1 @Auromix 是一个机器人爱好者开源组织。
2 本文在更新日期经过测试,确认有效。

笔者出于学习交流目的,
给出以下ModbusCRC16校验常用的四种函数以及完整示例代码:

1.计算CRC

注意: 此处在末尾进行了高低位交换,可根据需求删减代码交换高低位顺序。

unsigned short calculateModbusCRC16(const vector<uint8_t> &data) {int length = data.size();unsigned short CRC = 0xFFFF; // initial valuefor (int i = 0; i < length; i++) {CRC = CRC ^ data[i]; // XOR byte into least sig. byte of crcfor (int j = 0; j < 8; j++) {if (CRC & 1) {CRC >>= 1;CRC ^= 0xA001;} else {CRC >>= 1;}}}unsigned short swappedCRC = ((CRC >> 8) & 0xFF) | ((CRC & 0xFF) << 8);return swappedCRC;
}

2.添加CRC校验位

注意: 此处进行了高低位交换,可根据需求删减代码交换高低位顺序。


void addModbusCRC16(vector<uint8_t> &data) {unsigned short crc = calculateModbusCRC16(data);// Append CRC bytes to the data vectordata.push_back((crc >> 8) & 0xFF);   // MSBdata.push_back(crc & 0xFF);          // LSB}

3.删除CRC校验位

void removeModbusCRC16(vector<uint8_t> &dataWithCRC) {int length = dataWithCRC.size();// Error checkif (length < 2) {cerr << "Invalid data length" << endl;return;}// Delete CRC at the enddataWithCRC.resize(length - 2);
}

4.比较CRC校验位


bool compareModbusCRC16(const vector<uint8_t> &dataWithCRC) {int length = dataWithCRC.size();// Error checkif (length < 2) {cerr << "Invalid data length" << endl;return false;}// Get data without CRCvector<uint8_t> dataWithoutCRC(dataWithCRC.begin(), dataWithCRC.end() - 2);// Calculateunsigned short calculatedCRC = calculateModbusCRC16(dataWithoutCRC);// Get original CRCunsigned short originalCRC = (dataWithCRC[length - 2] << 8) | dataWithCRC[length - 1];return originalCRC == calculatedCRC;
}

5.完整示例代码

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;unsigned short calculateModbusCRC16(const vector<uint8_t> &data) {int length = data.size();unsigned short CRC = 0xFFFF; // initial valuefor (int i = 0; i < length; i++) {CRC = CRC ^ data[i]; // XOR byte into least sig. byte of crcfor (int j = 0; j < 8; j++) {if (CRC & 1) {CRC >>= 1;CRC ^= 0xA001;} else {CRC >>= 1;}}}unsigned short swappedCRC = ((CRC >> 8) & 0xFF) | ((CRC & 0xFF) << 8);return swappedCRC;
}void addModbusCRC16(vector<uint8_t> &data) {unsigned short crc = calculateModbusCRC16(data);// Append CRC bytes to the data vectordata.push_back((crc >> 8) & 0xFF);   // MSBdata.push_back(crc & 0xFF);          // LSB}void removeModbusCRC16(vector<uint8_t> &dataWithCRC) {int length = dataWithCRC.size();// Error checkif (length < 2) {cerr << "Invalid data length" << endl;return;}// Delete CRC at the enddataWithCRC.resize(length - 2);
}bool compareModbusCRC16(const vector<uint8_t> &dataWithCRC) {int length = dataWithCRC.size();// Error checkif (length < 2) {cerr << "Invalid data length" << endl;return false;}// Get data without CRCvector<uint8_t> dataWithoutCRC(dataWithCRC.begin(), dataWithCRC.end() - 2);// Calculateunsigned short calculatedCRC = calculateModbusCRC16(dataWithoutCRC);// Get original CRCunsigned short originalCRC = (dataWithCRC[length - 2] << 8) | dataWithCRC[length - 1];// Logcout<< "ModbusCRC16 original: "<<hex<< originalCRC<< endl;cout<< "ModbusCRC16 calculated: "<<hex<< calculatedCRC<< endl;return originalCRC == calculatedCRC;
}int main() {// Example data 1vector<uint8_t> deviceData1 = {0x01, 0x10, 0x00, 0x02, 0x00, 0x06, 0x0C, 0x41, 0x20,0x00, 0x00, 0x42, 0xC8, 0x00, 0x00, 0x42, 0x48, 0x00, 0x00,0x84, 0xC1}; // Example CRC: 0x84, 0xC1// Print original datacout << "Original data 1: ";for (uint8_t byte : deviceData1) {cout << hex << uppercase << setw(2) << setfill('0') << (int)byte << " ";}cout << endl;bool comparedResult=compareModbusCRC16(deviceData1);if (comparedResult)cout<<"Compared result: "<<"TRUE"<<endl;elsecout<<"Compared result: "<<"FALSE"<<endl;// Example data 2cout<<endl;vector<uint8_t> deviceData2 = {0x01, 0x06, 0x00, 0x00, 0x01, 0x02, 0x02};// Example CRC: 0xDA, 0xC7cout << "Original data 2: ";for (uint8_t byte : deviceData2) {cout << hex << uppercase << setw(2) << setfill('0') << (int)byte << " ";}cout << endl;// Add CRC and print modified dataaddModbusCRC16(deviceData2);cout << "Add CRC to original data 2: ";for (uint8_t byte : deviceData2) {cout << hex << uppercase << setw(2) << setfill('0') << (int)byte << " ";}cout << endl;// Remove CRC from dataremoveModbusCRC16(deviceData2);cout << "Remove CRC from modified data 2: ";for (uint8_t byte : deviceData2) {cout << hex << uppercase << setw(2) << setfill('0') << (int)byte << " ";}cout << endl;	return 0;
}
http://www.yayakq.cn/news/377197/

相关文章:

  • 响应式网站 图片居中网站标题怎么做链接
  • 动力网站重庆网站平台如何推广
  • 优化网站除了百度站长购物网站开发设计类图
  • 微商网站如何做推广海南注册家族公司条件
  • 搭理彩票网站开发鞍山人才网怎么查档案
  • php网站服务器怎么来网络广告的概念
  • 建个大型网站要多少钱网站后台上传软件
  • 电影片头在线制作网站网页界面设计教材
  • 网站的宗旨北京网站建设案例
  • 模板网站对排名的影响企业官网怎么查
  • 网站开发结论wordpress2017
  • 网站用户体验要素设计制作商城网站
  • 华为快速建站做网站如何自动采集图片
  • 我做网站啦 圆通导航网站建设 九艾
  • 专业门户网站开发企业网站续费
  • 手机网站类型天津机械网站建设模板
  • 潍坊个人做网站稻壳ppt免费模板
  • 小说网站自动采集建设企业网站怎么样
  • 咖啡公司网站建设策划书ui设计是什么类
  • 新闻客户端网站开发wordpress 飞龙博客 许愿墙
  • 做网站app怎么赚钱怎么用php做网站方案
  • 家居网站建设哪家好网站层级
  • wordpress建好站了打不开首页无锡网站建设企业
  • 企业建站电话多少有哪些企业网站平台
  • 电商网站开题报告网站内部优化
  • 顺德网站建设公司价位wordpress access
  • 苏州网站关键词推广怎么找网站是由什么建的
  • 网站开发的进度控制计划表怎么查看一个网站是谁做的
  • jsp做的零食网站下载温州教育网站建设
  • 自己做付费网站网站优化软件排行榜