半导体:Gem/Secs基本协议库的开发(3)

2023-12-15 21:36

本文主要是介绍半导体:Gem/Secs基本协议库的开发(3),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

接着上一篇《半导体:Gem/Secs基本协议库的开发(2)》继续,模拟工具延到下一篇,别催!

【codes】

//hsmsmessageheader.h
/*****************************************         HsmsMessageHeader****************************************/#ifndef HSMSMESSAGEHEADER_H
#define HSMSMESSAGEHEADER_H#include "JcHsms.h"class JCHSMS_EXPORT HsmsMessageHeader
{
public:HsmsMessageHeader();  // originateHsmsMessageHeader(uint16_t sessionID, uint8_t stream, uint8_t function,uint8_t ptype, uint8_t stype,  uint32_t systembytes);HsmsMessageHeader(QByteArray source); // interpretQByteArray toByteArray();QByteArray toByteArray(uint16_t sessionID,uint8_t stream,uint8_t function,uint8_t ptype,uint8_t stype,uint32_t systembytes);uint16_t GetSessionID();uint8_t  Getstream();uint8_t  Getfunction();uint8_t  GetpType();uint8_t  GetsType();uint32_t GetSystemBytes();MessageType GetMessageType();static uint16_t GetSessionID(const QByteArray& source);static uint8_t  Getstream(const QByteArray& source);static uint8_t  Getfunction(const QByteArray& source);static uint8_t  GetpType(const QByteArray& source);static uint8_t  GetsType(const QByteArray& source);static uint32_t GetSystemBytes(const QByteArray& source);static MessageType GetMessageType(const QByteArray& source);void SetSessionID(uint16_t id);void SetStreamFunction(uint8_t stream, uint8_t function);void SetWBit(WBit bit);void SetPSType(uint8_t ptype, uint8_t stype);void SetSystemBytes(uint32_t systemByte);uint32_t uniqueSystemBytes();public:static uint32_t m_systembytes;  /// 4 bytes SystemBytes.static uint32_t SystemBytes_counter;
private:QByteArray m_source;            /// 10 bytes.fixed.uint16_t m_sessionID;           /// 2 bytesuint8_t m_stream;               /// 1 byte streamuint8_t m_function;             /// 1 byte functionuint8_t m_stype;                /// 1 byte stypeuint8_t m_ptype;                /// 1 byte ptype
};#endif // HSMSMESSAGEHEADER_H
// hsmsmessageheader.cpp/*******************************************************************************   HsmsMessageHeader******************************************************************************/#include "hsmsmessageheader.h"uint32_t HsmsMessageHeader::m_systembytes = 0;
uint32_t HsmsMessageHeader::SystemBytes_counter = 0;/*!* \brief The HsmsMessageHeader struct*  10 bytes message header* segment ==>|session id |  stream | function | pType | SType |     system bytes             |*            |hDev |lDev |------------------------------------| source num | transaction num |* bytes   ==>|    2      |    1    |   1      |   1   |  1    |            4                 |*/
HsmsMessageHeader::HsmsMessageHeader()
{m_source.resize(10);}HsmsMessageHeader::HsmsMessageHeader(QByteArray source)
{assert(m_source.length() != HSMS_MESSAGEHEADER_LEN);m_source.resize(10);m_source = source;m_sessionID = GetSessionID(source);m_stream = Getstream(source);m_function = Getfunction(source);m_stype = GetsType(source);m_ptype = GetpType(source);m_systembytes = GetSystemBytes(source);
}HsmsMessageHeader::HsmsMessageHeader(uint16_t sessionID, uint8_t stream, uint8_t function,uint8_t ptype, uint8_t stype,  uint32_t systembytes)
{m_sessionID = sessionID;m_stream = stream;m_function = function;m_stype = stype;m_ptype = ptype;m_systembytes = systembytes;m_source.resize(10);toByteArray();}QByteArray HsmsMessageHeader::toByteArray()
{SetSessionID(m_sessionID);SetStreamFunction(m_stream,m_function);SetPSType(m_ptype,m_stype);SetSystemBytes(m_systembytes);return m_source;
}QByteArray HsmsMessageHeader::toByteArray(uint16_t sessionID, uint8_t stream,uint8_t  function, uint8_t ptype,uint8_t  stype   , uint32_t systembytes)
{SetSessionID(sessionID);SetStreamFunction(stream,function);SetPSType(ptype,stype);SetSystemBytes(systembytes);return m_source;
}uint16_t HsmsMessageHeader::GetSessionID()
{return m_sessionID;
}uint8_t HsmsMessageHeader::Getstream()
{return m_stream;
}uint8_t HsmsMessageHeader::Getfunction()
{return m_function;
}uint8_t HsmsMessageHeader::GetpType()
{return m_ptype;
}uint8_t HsmsMessageHeader::GetsType()
{return m_stype;
}uint32_t HsmsMessageHeader::GetSystemBytes()
{return m_systembytes;
}MessageType HsmsMessageHeader::GetMessageType()
{if(m_source.size() < 10){return ErrorMessageType;}if( GetpType() != 0){return ErrorMessageType;}if( Getstream() & ( !GetsType() ) ){return DataMessage;}if( GetsType() >= 0 && GetsType() < 10){return static_cast<MessageType>(GetsType());}else{return ErrorMessageType;}
}uint16_t HsmsMessageHeader::GetSessionID(const QByteArray &source)
{uint16_t id = static_cast<uint16_t>(source.at(0));id = (id << 8) + static_cast<uint16_t>(source.at(1));return id;
}uint8_t HsmsMessageHeader::Getstream(const QByteArray &source)
{return static_cast<uint8_t>(source.at(2) & 0b01111111);
}uint8_t HsmsMessageHeader::Getfunction(const QByteArray &source)
{return static_cast<uint8_t>(source.at(3));
}uint8_t HsmsMessageHeader::GetpType(const QByteArray &source)
{return static_cast<uint8_t>(source.at(4));
}uint8_t HsmsMessageHeader::GetsType(const QByteArray &source)
{return static_cast<uint8_t>(source.at(5));
}uint32_t HsmsMessageHeader::GetSystemBytes(const QByteArray &source)
{uint32_t lenA[4] = {0};uint32_t len = 0;lenA[0] = static_cast<uint32_t>(source.at(6));lenA[1] = static_cast<uint32_t>(source.at(7));lenA[2] = static_cast<uint32_t>(source.at(8));lenA[3] = static_cast<uint32_t>(source.at(9));#if ENDIAN == LITTLE_ENDIANfor(int i = 0; i < 4;i++) len += (lenA[i] << ((3-i)*8 ));
#elsefor(int i = 0; i < 4;i++)  len += (lenA[i] << (i*8 ));
#endifreturn len;
}MessageType HsmsMessageHeader::GetMessageType(const QByteArray &source)
{if(source.size() < 10){return ErrorMessageType;}if( GetpType(source) != 0){return ErrorMessageType;}if( Getstream(source) & ( !GetsType(source) ) ){return DataMessage;}if( GetsType(source) >= 0 && GetsType(source) < 10){return static_cast<MessageType>(GetsType(source));}else{return ErrorMessageType;}
}void HsmsMessageHeader::SetSessionID(uint16_t id)
{
#if  ENDIAN == LITTLE_ENDIANm_source[0] = static_cast<char>( (id >> 8) & 0xff); //header byte 0m_source[1] = static_cast<char>( (id >> 0) & 0xff); //header byte 1
#elsem_source[1] = static_cast<char>( (id >> 8) & 0xff); //header byte 1m_source[0] = static_cast<char>( (id >> 0) & 0xff); //header byte 0
#endifm_sessionID = id;
}void HsmsMessageHeader::SetStreamFunction(uint8_t stream, uint8_t function)
{m_source[2] = static_cast<char>(stream);   //header byte 2m_source[3] = static_cast<char>(function); //header byte 3m_stream = stream;m_function = function;
}void HsmsMessageHeader::SetWBit(WBit bit)
{uint8_t f = Getfunction();uint8_t s = Getstream();if(bit == NeedReply){s = s | 0b10000000;}else{s = s & 0b01111111;}SetStreamFunction(s, f);
}void HsmsMessageHeader::SetPSType(uint8_t ptype, uint8_t stype)
{m_source[4] = static_cast<char>(ptype);  //header byte 4m_source[5] = static_cast<char>(stype);  //header byte 5m_ptype = ptype;m_stype = stype;
}void HsmsMessageHeader::SetSystemBytes(uint32_t systemByte)
{m_source[6] = static_cast<uchar>( (systemByte >> 24) & 0xff); //header byte 6m_source[7] = static_cast<uchar>( (systemByte >> 16) & 0xff); //header byte 7m_source[8] = static_cast<uchar>( (systemByte >> 8 ) & 0xff); //header byte 8m_source[9] = static_cast<uchar>( (systemByte >> 0 ) & 0xff); //header byte 9m_systembytes = systemByte;
}uint32_t HsmsMessageHeader::uniqueSystemBytes()
{SystemBytes_counter ++;uint32_t timeDate = QDateTime::currentDateTime().toTime_t();  // 获取当前时间,将当前时间转为时间戳m_systembytes = timeDate<<4;m_systembytes += SystemBytes_counter%16;return m_systembytes;
}
//  secs2item.h/*****************************************         Secs2Item***************************************** ItemHeader      | Item Body     |* format_lenbytes | Len | context |* format|len bytes| Len | context |****************************************/#ifndef SECS2ITEM_H
#define SECS2ITEM_H#include "JcHsms_global.h"class JCHSMS_EXPORT Secs2Item
{
public:Secs2Item() {}Secs2Item(QString name);Secs2Item(ItemFormatCode format,QVariant val);Secs2Item(QByteArray source); // interpretQByteArray toByteArray() const;int format() const;bool isEmpty() const;static int  format(const QByteArray &array);static bool isValidFormat(ItemFormatCode format);static int  itemHeaderSize(const QByteArray &array);static int  bytes(const QByteArray& array);const int bytes() const;QByteArray itemHeader() const;QByteArray itemBody() const;QByteArray itemDataAll() const;int buildItemHeader();void AddBinary(const QByteArray& x);void AddASCII(QString str);void AddASCII(const char* str,int len);void AddJIS8(const char * str); // unicodevoid AddBoolean(const bool x);void AddBoolean(const bool x[], int N);void AddInt8(const int8_t x);void AddInt8(const int8_t x[], int N);void AddInt8

这篇关于半导体:Gem/Secs基本协议库的开发(3)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/497971

相关文章

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

Java Instrumentation从概念到基本用法详解

《JavaInstrumentation从概念到基本用法详解》JavaInstrumentation是java.lang.instrument包提供的API,允许开发者在类被JVM加载时对其进行修改... 目录一、什么是 Java Instrumentation主要用途二、核心概念1. Java Agent

Java中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例解析

《Java中的分布式系统开发基于Zookeeper与Dubbo的应用案例解析》本文将通过实际案例,带你走进基于Zookeeper与Dubbo的分布式系统开发,本文通过实例代码给大家介绍的非常详... 目录Java 中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例一、分布式系统中的挑战二

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

Kotlin 协程之Channel的概念和基本使用详解

《Kotlin协程之Channel的概念和基本使用详解》文章介绍协程在复杂场景中使用Channel进行数据传递与控制,涵盖创建参数、缓冲策略、操作方式及异常处理,适用于持续数据流、多协程协作等,需注... 目录前言launch / async 适合的场景Channel 的概念和基本使用概念Channel 的

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版