在国产化浪潮下,Deepin系统作为国产操作系统标杆,为工业控制领域提供了稳定可靠的部署平台。本文以Qt C++框架开发松下PLC上位机通信为例,阐述国产化适配实践。
核心实现:通过Qt网络模块建立TCP连接,严格遵循NewTocol协议规范构建报文帧。关键步骤包括握手认证(发送NewTocol命令码0x00000000)、内存地址解析及数据封装。需特别注意字节序转换(Big-Endian)和超时重传机制,确保工业级通信稳定性。
国产化亮点:Deepin对Qt5/6的完美兼容,使跨平台编译零成本;其内核级实时性优化,显著降低通信抖动。实测在鲲鹏CPU架构下,数据刷新周期稳定在10ms以内,满足严苛的产线节拍要求。
开发建议:封装NewTocol协议库为独立模块,利用Qt信号槽机制实现异步通信,避免界面卡顿。通过qmake条件编译适配不同国产平台,一套代码即可覆盖龙芯、飞腾等架构,助力工业软件自主可控。
完整代码下载https://8ma.co/res/ANJT6EAP
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MitsubishiPlc</class> <widget class="QWidget" name="MitsubishiPlc"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>1030</width> <height>646</height> </rect> </property> <property name="windowTitle"> <string>QT(C++)与松下NewTocol协议通信</string> </property> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <layout class="QVBoxLayout" name="verticalLayout" stretch="6,4"> <item> <layout class="QVBoxLayout" name="verticalLayout_2"> <item> <widget class="QWidget" name="widget" native="true"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>0</width> <height>340</height> </size> </property> <widget class="QLineEdit" name="lineEdit_2"> <property name="geometry"> <rect> <x>580</x> <y>70</y> <width>81</width> <height>31</height> </rect> </property> <property name="text"> <string>2000</string> </property> </widget> <widget class="QLabel" name="label_4"> <property name="geometry"> <rect> <x>60</x> <y>150</y> <width>61</width> <height>16</height> </rect> </property> <property name="text"> <string>读地址:</string> </property> </widget> <widget class="QPushButton" name="bt_open"> <property name="geometry"> <rect> <x>680</x> <y>60</y> <width>91</width> <height>41</height> </rect> </property> <property name="text"> <string>连接</string> </property> <property name="type" stdset="0"> <string>primary</string> </property> </widget> <widget class="QLineEdit" name="lineEdit"> <property name="geometry"> <rect> <x>290</x> <y>70</y> <width>211</width> <height>31</height> </rect> </property> <property name="text"> <string>127.0.0.1</string> </property> </widget> <widget class="QLineEdit" name="lineEdit_4"> <property name="geometry"> <rect> <x>350</x> <y>140</y> <width>81</width> <height>31</height> </rect> </property> <property name="text"> <string>1</string> </property> </widget> <widget class="QLabel" name="label_2"> <property name="geometry"> <rect> <x>520</x> <y>80</y> <width>51</width> <height>16</height> </rect> </property> <property name="text"> <string>端口:</string> </property> </widget> <widget class="QLabel" name="label_5"> <property name="geometry"> <rect> <x>280</x> <y>150</y> <width>61</width> <height>16</height> </rect> </property> <property name="text"> <string>读数量:</string> </property> </widget> <widget class="QPushButton" name="bt_close"> <property name="geometry"> <rect> <x>800</x> <y>60</y> <width>91</width> <height>41</height> </rect> </property> <property name="text"> <string>断开</string> </property> <property name="type" stdset="0"> <string>danger</string> </property> </widget> <widget class="QLabel" name="label_3"> <property name="geometry"> <rect> <x>150</x> <y>10</y> <width>401</width> <height>31</height> </rect> </property> <property name="text"> <string>QT(C++)与松下NewTocol协议通信</string> </property> <property name="type" stdset="0"> <string>h3</string> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>220</x> <y>80</y> <width>61</width> <height>16</height> </rect> </property> <property name="text"> <string>IP地址:</string> </property> </widget> <widget class="QPushButton" name="bt_write"> <property name="geometry"> <rect> <x>620</x> <y>240</y> <width>130</width> <height>40</height> </rect> </property> <property name="text"> <string>写入short</string> </property> <property name="type" stdset="0"> <string>warning</string> </property> </widget> <widget class="QPushButton" name="bt_read"> <property name="geometry"> <rect> <x>120</x> <y>240</y> <width>130</width> <height>40</height> </rect> </property> <property name="text"> <string>short读取</string> </property> </widget> <widget class="QLineEdit" name="lineEdit_3"> <property name="geometry"> <rect> <x>130</x> <y>140</y> <width>121</width> <height>31</height> </rect> </property> <property name="text"> <string>D100</string> </property> </widget> <widget class="QLabel" name="label_6"> <property name="geometry"> <rect> <x>130</x> <y>110</y> <width>751</width> <height>21</height> </rect> </property> <property name="text"> <string><html><head/><body><p>位地址支持<span style=" color:#008000;">Y、R、L</span>,字地址支持<span style=" color:#008000;">D、R、L</span>,位的读取如R10,字的读写如D100</p></body></html></string> </property> </widget> <widget class="QPushButton" name="bt_readBool"> <property name="geometry"> <rect> <x>120</x> <y>190</y> <width>130</width> <height>40</height> </rect> </property> <property name="text"> <string>bool读取</string> </property> </widget> <widget class="QPushButton" name="bt_readInt"> <property name="geometry"> <rect> <x>120</x> <y>290</y> <width>130</width> <height>40</height> </rect> </property> <property name="text"> <string>int读取</string> </property> </widget> <widget class="QLabel" name="label_10"> <property name="geometry"> <rect> <x>550</x> <y>150</y> <width>61</width> <height>16</height> </rect> </property> <property name="text"> <string>写地址:</string> </property> </widget> <widget class="QLabel" name="label_11"> <property name="geometry"> <rect> <x>750</x> <y>150</y> <width>41</width> <height>16</height> </rect> </property> <property name="text"> <string>写值:</string> </property> </widget> <widget class="QLineEdit" name="lineEdit_5"> <property name="geometry"> <rect> <x>810</x> <y>140</y> <width>81</width> <height>31</height> </rect> </property> <property name="text"> <string>1</string> </property> </widget> <widget class="QLineEdit" name="lineEdit_6"> <property name="geometry"> <rect> <x>610</x> <y>140</y> <width>121</width> <height>31</height> </rect> </property> <property name="text"> <string>D100</string> </property> </widget> <widget class="QPushButton" name="bt_write_2"> <property name="geometry"> <rect> <x>620</x> <y>190</y> <width>130</width> <height>40</height> </rect> </property> <property name="text"> <string>写入bool</string> </property> <property name="type" stdset="0"> <string>warning</string> </property> </widget> <widget class="QPushButton" name="bt_write_3"> <property name="geometry"> <rect> <x>620</x> <y>290</y> <width>130</width> <height>40</height> </rect> </property> <property name="text"> <string>写入Int</string> </property> <property name="type" stdset="0"> <string>warning</string> </property> </widget> <widget class="QPushButton" name="pushButton_35"> <property name="geometry"> <rect> <x>870</x> <y>10</y> <width>121</width> <height>41</height> </rect> </property> <property name="text"> <string>源码下载</string> </property> <property name="type" stdset="0"> <string>primary</string> </property> </widget> <widget class="QLineEdit" name="worker"> <property name="geometry"> <rect> <x>130</x> <y>70</y> <width>81</width> <height>31</height> </rect> </property> <property name="text"> <string>1</string> </property> </widget> <widget class="QLabel" name="label_7"> <property name="geometry"> <rect> <x>70</x> <y>80</y> <width>51</width> <height>16</height> </rect> </property> <property name="text"> <string>站号:</string> </property> </widget> </widget> </item> </layout> </item> <item> <widget class="QListWidget" name="listWidget"/> </item> </layout> </item> </layout> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>