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

此篇是1-4 《半导体》的会和处啦,我们有了协议库,也有了通讯库,这不得快乐的玩一把~

一、先创建一个从站,也就是我们的Equipment端

QT -= guiCONFIG += c++11 console
CONFIG -= app_bundle
CONFIG += no_debug_release         # 不会生成debug 和 release 文件目录DESTDIR = $${PWD}/../../deploy/bin
OBJECTS_DIR = $${PWD}/../../build/sample/Equipment/tmp/obj
MOC_DIR = $${PWD}/../../build/sample/Equipment/tmp/obj
UI_DIR = $${PWD}/../../build/sample/Equipment/tmp/obj# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetwin32:CONFIG(release, debug|release){win32: LIBS += -L$$PWD/../../deploy/lib/Release -lJC_Commucationwin32: LIBS += -L$$PWD/../../deploy/lib/Release -lJcHsms
}
else:win32:CONFIG(debug, debug|release){win32: LIBS += -L$$PWD/../../deploy/lib/Debug -lJC_Commucationwin32: LIBS += -L$$PWD/../../deploy/lib/Debug -lJcHsms
}INCLUDEPATH += $$PWD/../../deploy/include
DEPENDPATH += $$PWD/../../deploy/include
#include <QCoreApplication>
#include <QDebug>
#include <iostream>
#include <QByteArray>
#include <string>
#include <QTimer>
using namespace std;#include "../../SemiGeneralStandardLibrary/JcGemSecsLibrary/Commucation/commucation.h"
#include "../../SemiGeneralStandardLibrary/JcGemSecsLibrary/Driver/JcHsms/hsmsincludes.h"/*** @brief OnStateChanged 连接状态改变回调事件* @param pComm* @param nState        0: 连接  1:断开连接* @param cSocket*/
void OnStateChanged(ICommucation* pComm, __int32 nState, void *cSocket)
{SOCKET* c = (SOCKET*) cSocket;std::string str = nState == 0 ? std::string("  connected to ") : std::string("  disconnected from ");std::cout << "[OnStateChanged Event] : " << c <<  str << (void*)pComm << std::endl;
}/// 无符号字节数组转16进制字符串
std::string bytesToHexString(const char* bytes,const int length)
{if (bytes == NULL) return "";std::string buff;const int len = length;for (int j = 0; j < len; j++) {int high = bytes[j]/16, low = bytes[j]%16;buff += (high<10) ? ('0' + high) : ('a' + high - 10);buff += (low<10) ? ('0' + low) : ('a' + low - 10);buff += " ";}return buff;
}/*!* \brief onMessageRecived  接收到消息的回调事件* \param pComm* \param recvedMsg* \param cSocket*/
void onMessageRecived(ICommucation* pComm,  char* message,int iRecvSize, void * cSocket)
{JcHsms ho(0,QString("JC Gem/Secs Test"),QString("1.0.1"));HsmsMessage hmsg = ho.interpretMessage(QByteArray(message,iRecvSize));HsmsMessage rsp = hmsg.dispatch();QByteArray responseByteArray = rsp.toByteArray();QString smlString = rsp.SmlString();string rHexString = bytesToHexString(message,iRecvSize);// qDebug().noquote() << "recv message ==> " << QString::fromStdString(rHexString);// qDebug().noquote() << "send message ==> " << responseByteArray.toHex(' ');qDebug().noquote() << QString("RECV S%1F%2 SystemBytes=%3").arg(QString::number((int)hmsg.GetHeader().Getstream()),QString::number((int)hmsg.GetHeader().Getfunction()),QString::number(hmsg.GetHeader().GetSystemBytes()));qDebug().noquote() << hmsg.SmlString();qDebug().noquote() << QString("SEND S%1F%2 SystemBytes=%3").arg(QString::number((int)rsp.GetHeader().Getstream()),QString::number((int)rsp.GetHeader().Getfunction()),QString::number( rsp.GetHeader().GetSystemBytes()));qDebug().noquote() << rsp.SmlString();int slen = responseByteArray.length();if(slen){int rslen = pComm->SendData(*((SOCKET*) cSocket),responseByteArray.data(),responseByteArray.length());if(rslen <= 0) {qDebug() << "Send Reply Message failed.";}}}/*!* \brief OnAsyncMsgTimeout  消息超时* \param pComm* \param nTransfer          消息ID* \param pClientData*/
void OnAsyncMsgTimeout(ICommucation* pComm, __int32 nTransfer, void *pClientData)
{}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);const char* comm_dll_version = JC_CommDllVersion();qDebug() << comm_dll_version;/// [1] 建立通讯连接(以单个通讯连接对象为例)CommucationParam setting;EthernetCommucationParam eParam = {45000,10000,5000,10000,5000,  /* timeout */0                             /* PASSIVE */,5555,                         /*  port  */1                             /* DEVID */,"Device Host","127.0.0.1"};SerialCommucationParam sParam = {2,9600,'N',8,1};setting.eParam = eParam;setting.sParam = sParam;/// 创建通讯对象ICommucation* o = NULL;o = JC_CreatCommObject(TcpServer,setting);/// 为通讯连接对象注册事件回调JC_SetEventCallBack(o,onMessageRecived,OnStateChanged,OnAsyncMsgTimeout);/// 启动监听JC_RunListenThread(o);/// 测试修改 Selected Equipment Status Data(SSD),线程安全float x[] = {12.3025,55.12,56.478,63.54};QTimer timer;timer.setInterval(300);QObject::connect(&timer,&QTimer::timeout,[&x](){static int i = 0;HsmsDataManager::Instance().UpdateSsdMap(1022,HsmsDataManager::ESD{F4,QVariant(x[++i%4])});});timer.start();QObject::connect(qApp,&QCoreApplication::aboutToQuit,[&o](){/// 释放通讯连接对象,结束通讯连接JC_ReleaseCommObject(o);});return a.exec();
}

二、创建一个主站,也就是我们的Host端

QT       += core gui networkgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11DESTDIR = $${PWD}/../../deploy/bin
OBJECTS_DIR = $${PWD}/../../build/sample/Host/tmp/obj
MOC_DIR = $${PWD}/../../build/sample/Host/tmp/obj
UI_DIR = $${PWD}/../../build/sample/Host/tmp/obj# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mcwidget.cppHEADERS += \mcwidget.h# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetwin32:CONFIG(release, debug|release){win32: LIBS += -L$$PWD/../../deploy/lib/Release -lJC_Commucationwin32: LIBS += -L$$PWD/../../deploy/lib/Release -lJcHsms
}
else:win32:CONFIG(debug, debug|release){win32: LIBS += -L$$PWD/../../deploy/lib/Debug -lJC_Commucationwin32: LIBS += -L$$PWD/../../deploy/lib/Debug -lJcHsms
}INCLUDEPATH += $$PWD/../../deploy/include
DEPENDPATH += $$PWD/../../deploy/include
// mcwidget.h
#ifndef MCWIDGET_H
#define MCWIDGET_H#include <QWidget>
#include <QTcpServer>
#include <QLabel>
#include <QHBoxLayout>
#include <QTimer>
#include <iostream>
#include <QDebug>
#include <string>
#include <QByteArray>
#include <QList>
#include "../../SemiGeneralStandardLibrary/JcGemSecsLibrary/Commucation/commucation.h"
#include  "../../SemiGeneralStandardLibrary/JcGemSecsLibrary/Driver/JcHsms/hsmsincludes.h"class TransHelper: public QObject
{Q_OBJECT
public:TransHelper(){qRegisterMetaType<HsmsMessage>("qRegisterMetaType");//qRegisterMetaType<HsmsMessage>("qRegisterMetaType&");}void RecivedMsgObject(HsmsMessage msg){emit RecivedMsgObjectSig(msg);}
signals:void RecivedMsgObjectSig(HsmsMessage);
};class MyLabel : public QWidget
{Q_OBJECT
public:MyLabel(QString labName,QString labval,QWidget* parent = nullptr):m_labname(labName),m_labVal(labval),QWidget(parent){m_nameLab = new QLabel(m_labname);m_valLab = new QLabel(m_labVal);m_nameLab->setFixedWidth(200);m_valLab->setFixedWidth(100);QHBoxLayout* ly = new QHBoxLayout;ly->addWidget(m_nameLab);ly->addWidget(m_valLab);ly->setContentsMargins(0,0,0,0);this->setContentsMargins(0,0,0,0);setLayout(ly);}void setValue(QString val){m_labVal = val;m_valLab->setText(m_labVal);}
private:QString m_labname;QString m_labVal;QLabel* m_nameLab;QLabel* m_valLab;
};class McWidget : public QWidget
{Q_OBJECTpublic:McWidget(QWidget *parent = nullptr);~McWidget();void initUi();static TransHelper transhelper;private:ICommucation* o = NULL;QTimer linktesttimer;QTimer s1f3Rqtimer;QList<MyLabel*> llabs;
};
#endif // MCWIDGET_H
// mcwidget.cpp#include "mcwidget.h"
#include <QDebug>
#include <functional>
#include <QVBoxLayout>TransHelper McWidget::transhelper;/*** @brief OnStateChanged 连接状态改变回调事件* @param pComm* @param nState        0: 连接  1:断开连接* @param cSocket*/
void OnStateChanged(ICommucation* pComm, __int32 nState, void *cSocket)
{SOCKET* c= (SOCKET*) cSocket;std::string str = nState == 0 ? std::string("  connected to ") : std::string("  disconnected from ");std::cout << "[OnStateChanged ] : " << c <<  str << (void*)pComm << str << std::endl;
}/*!* \brief onMessageRecived  接收到消息的回调事件* \param pComm* \param recvedMsg* \param cSocket*/
void onMessageRecived(ICommucation* pComm,char* recvedMsg, int iRecvsize,void * cSocket)
{SOCKET* c= (SOCKET*) cSocket;// std::cout << "[onMessageRecived ] : " << (void*)pComm << " <-- "  << c  << "  : "//           << recvedMsg  <<  "  len=" << iRecvsize << std::endl;/// 通过gemsecs的协议进行解析和应答JcHsms ho(0,QString("JC Gem/Secs Test"),QString("1.0.1"));HsmsMessage hmsg = ho.interpretMessage(QByteArray(recvedMsg,iRecvsize));if(hmsg.GetHeader().Getstream() == 0x1&& hmsg.GetHeader().Getfunction() == 0x4){ // S1F4McWidget::transhelper.RecivedMsgObject(hmsg);}
}/*!* \brief OnAsyncMsgTimeout  消息超时* \param pComm* \param nTransfer          消息ID* \param pClientData*/
void OnAsyncMsgTimeout(ICommucation* pComm, __int32 nTransfer, void *pClientData)
{qDebug() << QStringLiteral("同步发送请求消息超时");
}McWidget::McWidget(QWidget *parent): QWidget(parent)
{initUi();CommucationParam setting;EthernetCommucationParam eParam = {45000,10000,5000,10000,5000, /* timeout */0                            /* PASSIVE */,5555,                        /*  port  */1                            /* DEVID */,"Device Host","127.0.0.1"};SerialCommucationParam sParam = {2,9600,'N',8,1};setting.eParam = eParam;setting.sParam = sParam;o = JC_CreatCommObject(TcpClient,setting);/// 注册回调事件JC_SetEventCallBack(o,onMessageRecived,OnStateChanged,OnAsyncMsgTimeout);/// 启动监听JC_RunListenThread(o);/// 发送 select.req 请求JcHsms ho(0,QString("JC Gem/Secs Test"),QString("1.0.1"));HsmsMessage srmsg = HsmsMessageDispatcher::selectReq(ho.unique_sessionID);QByteArray srbytes =  srmsg.toByteArray();std::string rbuf;bool ok = o->SendSyncMessage(srbytes.toStdString(),true,rbuf,10);std::cout << "recv reply buf :" << rbuf << std::endl;qDebug("send status:%s\n",ok ? "success" : "failed");std::function<void()> flinktest = [=](){HsmsMessage lktestMgr = HsmsMessageDispatcher::linktestReq();QByteArray lktestBytes = lktestMgr.toByteArray();#if 0   /// 同步发送/接收消息std::string rbuf;bool ok = o->SendSyncMessage(lktestBytes.toStdString(),true,rbuf,10);std::cout << "recv reply buf :" << rbuf << std::endl;qDebug("send status:%s\n",ok ? "success" : "failed");
#else/// 异步发送接收消息(消息接收回调事件)o->SendData(0,lktestBytes.constData(),lktestBytes.length());
#endif};/// 立即执行一次if(ok) flinktest();/// 定时触发linktesttimer.setInterval(10000);// 10sQObject::connect(&linktesttimer,&QTimer::timeout,flinktest);linktesttimer.start();/// 定时发送S1F3 请求最新SSDstd::function<void()> fs1f3Rq = [=,&ho](){HsmsMessage s1f3ReqtMgr = HsmsMessageDispatcher::S1F3(ho.unique_sessionID);QByteArray s1f3ReqBytes = s1f3ReqtMgr.toByteArray();#if 0   /// 同步发送/接收消息std::string rbuf;bool ok = o->SendSyncMessage(lktestBytes.toStdString(),true,rbuf,10);std::cout << "recv reply buf :" << rbuf << std::endl;qDebug("send status:%s\n",ok ? "success" : "failed");
#else/// 异步发送接收消息(消息接收回调事件)o->SendData(0,s1f3ReqBytes.constData(),s1f3ReqBytes.length());
#endif};s1f3Rqtimer.setInterval(30);QObject::connect(&s1f3Rqtimer,&QTimer::timeout,fs1f3Rq);s1f3Rqtimer.start();/// S1F4 Recivedconnect(&transhelper,&TransHelper::RecivedMsgObjectSig,[=](HsmsMessage hm){Secs2Item item = hm.GetItem();QVector<Secs2Item> v = item.GetItems();if(v.isEmpty() || v.length() != 23 ) return;llabs[0]->setValue(QString::number( v[0].toInt32().first()));for(int i =1;i<=3;++i){ // boolllabs[i]->setValue(QString::number(v[i].toBoolean().first()));}for(int i =4;i<=20;++i){ // int32llabs[i]->setValue(QString::number(v[i].toInt32().first()));}for(int i = 21;i <= 22;++i){ // floatllabs[i]->setValue(QString::number(v[i].toFloat().first()));}});}McWidget::~McWidget()
{}void McWidget::initUi()
{llabs.clear();QVBoxLayout* vly = new QVBoxLayout;for(int i = 1001;i <= 1023; ++i){MyLabel* labptr = new MyLabel(QString::number(i),QString("0"));llabs.append(labptr);labptr->setFixedHeight(30);labptr->setFixedWidth(300);vly->addWidget(labptr);}setLayout(vly);
}
// main.cpp#include "mcwidget.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);McWidget w;w.show();return a.exec();
}

三、演示结果

在这里插入图片描述
perfect!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/226954.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python 直观理解基尼系数

基尼系数最开始就是衡量人群财富收入是否均衡&#xff0c;大家收入平平&#xff0c;那就是很平均&#xff0c;如果大家收入不平等&#xff0c;那基尼系数就很高。 还是给老干部们讲的言简意赅。 什么是基尼系数 我们接下来直接直观地看吧&#xff0c;程序说话 # -*- coding:…

Chart.js 实现实时动态折线图 并限制最大长度

<!DOCTYPE html> <html><head><title>模拟</title><script src"https://lib.sinaapp.com/js/jquery/3.1.0/jquery-3.1.0.min.js"></script><script src"https://cdn.staticfile.org/Chart.js/3.9.1/chart.js"…

12345、ABCDE项目符号列表文字视频怎么制作?重点内容介绍PR标题模板项目工程文件

Premiere模板&#xff0c;包含10个要点标题12345、ABCDE项目符号列表文字模板PR项目工程文件。可以根据自己的需要定制颜色。在视频的开头、中间和结尾使用。包括视频教程。 适用软件&#xff1a;Premiere Pro 2019 | 分辨率&#xff1a;19201080 (HD) | 文件大小&#xff1a;9…

基于Java SSM框架实现疫情居家办公OA系统项目【项目源码+论文说明】

基于java的SSM框架实现疫情居家办公OA系统演示 摘要 21世纪的今天&#xff0c;随着社会的不断发展与进步&#xff0c;人们对于信息科学化的认识&#xff0c;已由低层次向高层次发展&#xff0c;由原来的感性认识向理性认识提高&#xff0c;管理工作的重要性已逐渐被人们所认识…

加油站“变身”快充站,探讨充电新模式——安科瑞 顾烊宇

摘要&#xff1a;新能源汽车规模化发展的同时&#xff0c;充电不便利的痛点愈发明显。在未来的新能源汽车行业发展当中&#xff0c;充电的矛盾要远远大于造车的矛盾&#xff0c;解决好充电的问题成为电动汽车行业发展的一个突出问题。解决充电补能问题&#xff0c;重要的方式之…

分库分表以后,如何实现扩容?

在实际开发中&#xff0c;数据库的扩容和不同的分库分表规则直接相关&#xff0c;今天我们从系统设计的角度&#xff0c;抽象了一个项目开发中出现的业务场景&#xff0c;从数据库设计、路由规则&#xff0c;以及数据迁移方案的角度进行讨论。 从业务场景出发进行讨论 假设这…

msvcrtd.dll下载安装方法,解决msvcrtd.dll找不到的问题

在这篇文章中&#xff0c;我们将详细讨论msvcrtd.dll文件的下载安装方法&#xff0c;并分析出现找不到msvcrtd.dll的情况及解决方法。如果你遇到了与msvcrtd.dll相关的问题&#xff0c;本文将为你提供全面且详细的解决方案。 一.什么是msvcrtd.dll文件 首先&#xff0c;让我们…

透明PP专用UV胶水粘接PP材料高效率的提升生产效率

使用透明PP专用UV胶水粘接PP材料是提高生产效率的方法。以下方法&#xff0c;可以助您在生产中实现高效的PP材料粘接&#xff1a; ​1.选用合适的透明PP专用UV胶水 选择经过专门设计用于透明PP的UV胶水。这种胶水具有透明性&#xff0c;能保证粘接后的清晰度和外观。 2.自动…

vue中预览pdf的方法

使用vue-pdf 备注&#xff1a;这里只介绍了一页的pdf <div class"animation-box-pdf"><pdf :src"http://xxxx" /> </div>import Pdf from vue-pdf // src可以是文件地址url&#xff0c;也可以是文件流blob&#xff08;将blob转成url&a…

W25N01GV 芯片应用

项目中处于成本考虑&#xff0c;要把Nor Flash换成低成本的Nand Flash。 这里总结下芯片应用。 总体概述&#xff1a; 1&#xff09;W25N01&#xff08;NandFlash&#xff09;和W25Q&#xff08;Nor Flash&#xff09;的操作大不一样。 NandFlash擦除以块&#xff08;128KB&…

外包干了3年,技术退步明显。。。

前言 简单说下我的情况吧&#xff01;普通本科的科班生&#xff0c;19年的时候通过校招进了一家小自研&#xff0c;工资还凑合&#xff0c;在里面带了一年多&#xff0c;公司没了&#xff0c;疫情期间找工作很麻烦&#xff0c;后面就开始自己近3年的外包生涯&#xff0c;这三年…

如果你找不到东西,请先确保你在正确的地方寻找

之前我们在几篇文章中描述了如何进行”思想”调试&#xff0c;今天的文章我将不会这样做。 因为下面的编程错误大部分人都会遇到&#xff0c;如果你看一眼下面的代码&#xff0c;你不会发现有什么问题&#xff0c;这仅仅是因为你的的大脑只给你希望看到的&#xff0c;而不是那…

多线程 (上) - 学习笔记

前置知识 什么是线程和进程? 进程: 是程序的一次执行,一个在内存中运行的应用程序。每个进程都有自己独立的一块内存空间&#xff0c;一个进程可以有多个线程&#xff0c;比如在Windows系统中&#xff0c;一个运行的xx.exe就是一个进程。 线程: 进程中的一个执行流&#xff0…

seaborn库图形进行数据分析(基于tips数据集)

Seaborn 是一个基于 matplotlib 的数据可视化库&#xff0c;可以用来绘制各种统计图表&#xff0c;包括散点图、条形图、折线图、箱线图等。Seaborn 提供了一些用于美化图表的默认样式和颜色主题&#xff0c;使得生成的图表更具有吸引力。下面是一些 Seaborn 库的常用功能和用法…

TrustGeo代码理解(六)utils.py

代码链接:https://github.com/ICDM-UESTC/TrustGeo 一、导入常用库和模块 from __future__ import print_function from distutils.version import LooseVersion from matplotlib.scale import LogisticTransform import numpy as np import torch import warnings import t…

《Linux C编程实战》笔记:文件读写

Linux c下文件读写可用creat&#xff0c;open&#xff0c;close&#xff0c;read&#xff0c;write&#xff0c;lseek等函数。对于跨平台的程序还是用C标准库的fopen等。 open #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open …

Linux——进程创建与进程终止

&#x1f4d8;北尘_&#xff1a;个人主页 &#x1f30e;个人专栏:《Linux操作系统》《经典算法试题 》《C》 《数据结构与算法》 ☀️走在路上&#xff0c;不忘来时的初心 文章目录 一、进程创建1、fork函数初识2、fork函数返回值3、写时拷贝4、fork常规用法5、fork调用失败的…

【产品经理】产品增效项目落地,项目反哺产品成长

产品和项目是相辅相成的关系&#xff0c;产品的规范、成熟&#xff0c;为项目的快速落地提供支撑&#xff0c;项目的落地反哺产品&#xff0c;促进产品的成长成熟。 软件工程的初期是&#xff0c;我们需要什么&#xff0c;就立项项目&#xff0c;通过项目实现需要。 随着项目的…

什么是前端国际化(internationalization)和本地化(localization)?

聚沙成塔每天进步一点点 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 欢迎来到前端入门之旅&#xff01;感兴趣的可以订阅本专栏哦&#xff01;这个专栏是为那些对Web开发感兴趣、刚刚踏入前端领域的朋友们量身打造的。无论你是完全的新手还是有一些基础的开发…

Python+Yolov8+onnx-deepsort方法物体人流量识别统计

程序示例精选 PythonYolov8onnx-deepsort方法物体人流量识别统计 如需安装运行环境或远程调试&#xff0c;见文章底部个人QQ名片&#xff0c;由专业技术人员远程协助&#xff01; 前言 这篇博客针对《PythonYolov8onnx-deepsort方法物体人流量识别统计》编写代码&#xff0c;…