Qt Modbus CRC ccsds校验 异或校验 数据显示 分割显示

18 篇文章 1 订阅

一、qt Modbus crc


quint16 MasterModbus::getCrc(QByteArray arr)
{
    quint16 res = 0xffff;
    int index=0;
    while(index<arr.size())
    {
        res ^= (quint8)arr[index];
        for(int i = 0; i < 8; i ++)
        {
            if(res & 0x01)
            {
                res = (res >> 1) ^ 0xa001;
            }
            else
            {
                res = res >> 1;
            }
        }
        index++;
    }
    res = ((res & 0xff) << 8) | (res >> 8);
    return res;
}

quint16 MasterModbus::getCrc(quint8 *bytes, int len)
{
    quint16 res = 0xffff;
    while(len --)
    {
        res ^= *bytes ++;
        for(int i = 0; i < 8; i ++)
        {
            if(res & 0x01)
            {
                res = (res >> 1) ^ 0xa001;
            }
            else
            {
                res = res >> 1;
            }
        }
    }
    res = ((res & 0xff) << 8) | (res >> 8);
    return res;
}

二、CCSDS校验

生成多项式如下

g(x) = x^16 + x^12 + x^5 + x^1

uint16_t Widget::getCrc(QByteArray bytes)
{
    uint16_t wCRCin = 0xFFFF;
    uint16_t wCPoly = 0x1021;
    uchar wChar = 0;
    int index = 0;
    while (index<bytes.size())
    {
        wChar = bytes[index];
        wCRCin ^= (wChar << 8);

        for (int i = 0; i < 8; i++)
        {
            if (wCRCin & 0x8000)
            {
                wCRCin = (wCRCin << 1) ^ wCPoly;
            }
            else
            {
                wCRCin = wCRCin << 1;
            }
        }
        index++;
    }
    return (wCRCin);
}

三、异或校验

        quint16 BccCRC(quint8 *data,quint16 len){
            quint16 bcc=0;
            for(; len>0;len--){
                bcc ^= *data;
                data++;
            }
            return  ((bcc & 0xff) << 8) | (bcc >> 8);
        }
    
        quint16 BccCRC(QByteArray arr){
            quint16 bcc = 0;
            int index = 0;
            int len = 0;
            len = arr.length ();
            while (len--) {
                bcc ^= (quint8)arr.at (index);
                index++;
            }
            return  ((bcc & 0xff) << 8) | (bcc >> 8);
        }

Qt用法举例[异或校验]

	QByteArray dataCode;
	quint16 crcRes = 0;
	dataCode.append (ui->lEdtData->text ());
    crcRes = crc.BccCRC (dataCode.toHex ());

自己实践的结果


Data:待校验数据,crc:校验结果

在线生成器生成的结果


在这里插入图片描述

四、数据显示


Ⅰ、十六进制显示


#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QByteArray arr("1234");
    bool isTransformSuccessful = false;
    qDebug()<<"isTransformSuccessful"<<isTransformSuccessful<<arr.toInt (&isTransformSuccessful,16);
    return a.exec();
}


打印结果


isTransformSuccessful true 4660


Ⅱ、十进制显示


#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QByteArray arr("1234");
    bool isTransformSuccessful = false;
    qDebug()<<"isTransformSuccessful"<<isTransformSuccessful<<arr.toInt (&isTransformSuccessful,10);
    return a.exec();
}

打印结果

isTransformSuccessful true 1234

分割显示

低版本的qt可能不支持,我测试使用的版本为qt5.14.2版本

分割显示

#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QByteArray arr("1234");
    qDebug()<<arr.toHex (' ');
    qDebug()<<arr.toHex ('.');
    qDebug()<<arr.toHex (':');
    qDebug()<<arr.toHex ('-');
    return a.exec();
}


打印结果


“31 32 33 34”
“31.32.33.34”
“31:32:33:34”
“31-32-33-34”


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春意盎然的三月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值