init: 北航大学BH-DAS项目首次入库

This commit is contained in:
XingCheng3
2026-06-08 17:06:10 +08:00
commit 10f06d99cd
207 changed files with 40597 additions and 0 deletions

43
DK_DC_Tool/.gitignore vendored Normal file
View File

@@ -0,0 +1,43 @@
*.pyc
*.map
*.dblite
*.elf
*.bin
*.hex
*.axf
*.exe
*.pdb
*.idb
*.ilk
*.old
build
bin
obj
Debug
documentation/html
*~
*.o
*.obj
*.out
*.bak
*.dep
*.lib
*.i
*.d
.DS_Stor*
.config 3
.config 4
.config 5
Midea-X1
*.uimg
GPATH
GRTAGS
GTAGS
.vscode
JLinkLog.txt
JLinkSettings.ini
DebugConfig/
RTE/
settings/
*.uvguix*
cconfig.h

53
DK_DC_Tool/App.config Normal file
View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<!--站点日志配置部分-->
<log4net>
<root>
<!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
<!--比如定义级别为INFO则INFO级别向下的级别比如DEBUG日志将不会被记录-->
<!--如果没有定义LEVEL的值则缺省为DEBUG-->
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<remoteAddress value="127.0.0.1"/>
<remotePort value="7171"/>
<param name="Encoding" value="utf-8"/>
<layout type="log4net.Layout.PatternLayout" value="&lt;log level=&quot;%level&quot; app=&quot;mlw&quot; thread=&quot;%thread&quot; time=&quot;%date&quot; logger=&quot;%logger&quot;&gt;&lt;![CDATA[%message%exception]]&gt;&lt;/log&gt;"/>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<!--日志文件名开头-->
<file value="Log\\Log4net.txt"/>
<!--多线程时采用最小锁定-->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置-->
<datePattern value="(yyyyMMdd)"/>
<!--是否追加到文件,默认为true通常无需设置-->
<appendToFile value="true"/>
<!--变换的形式为日期,这种情况下每天只有一个日志-->
<!--此时MaxSizeRollBackups和maximumFileSize的节点设置没有意义-->
<!--<rollingStyle value="Date"/>-->
<!--变换的形式为日志大小-->
<!--这种情况下MaxSizeRollBackups和maximumFileSize的节点设置才有意义-->
<RollingStyle value="Size"/>
<!--每天记录的日志文件个数与maximumFileSize配合使用-->
<MaxSizeRollBackups value="10"/>
<!--每个日志文件的最大大小-->
<!--可用的单位:KB|MB|GB-->
<!--不要使用小数,否则会一直写入当前日志-->
<maximumFileSize value="100MB"/>
<!--日志格式-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%t]%p: %m%n"/>
</layout>
</appender>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>

View File

@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3C6FEF50-2014-4157-A12B-93FB4AD20FE7}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>DK_DC_Tool</RootNamespace>
<AssemblyName>DK_DC_Tool</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Design" />
<Reference Include="System.Web" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Tighten.cs" />
<Compile Include="ToolPacket.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyLog4Net\01_MyLog4Net.csproj">
<Project>{1dc48c83-1842-4de8-acaf-26e1e7f7c58f}</Project>
<Name>01_MyLog4Net</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

25
DK_DC_Tool/DK_DC_Tool.sln Normal file
View File

@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31919.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DK_DC_Tool", "DK_DC_Tool.csproj", "{3C6FEF50-2014-4157-A12B-93FB4AD20FE7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3C6FEF50-2014-4157-A12B-93FB4AD20FE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C6FEF50-2014-4157-A12B-93FB4AD20FE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C6FEF50-2014-4157-A12B-93FB4AD20FE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C6FEF50-2014-4157-A12B-93FB4AD20FE7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1894CA72-2F06-434D-A73A-03E306A66BA2}
EndGlobalSection
EndGlobal

483
DK_DC_Tool/Form1.Designer.cs generated Normal file
View File

@@ -0,0 +1,483 @@
namespace DK_DC_Tool
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label6 = new System.Windows.Forms.Label();
this.cbxDevType = new System.Windows.Forms.ComboBox();
this.btnOpen = new System.Windows.Forms.Button();
this.gbxSerial = new System.Windows.Forms.GroupBox();
this.tbxSerialProtocol = new System.Windows.Forms.TextBox();
this.label10 = new System.Windows.Forms.Label();
this.tbxSerialAddr = new System.Windows.Forms.TextBox();
this.label9 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.cbxSerial = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.cbxSerialCheckSum = new System.Windows.Forms.ComboBox();
this.cbxSerialBaud = new System.Windows.Forms.ComboBox();
this.label5 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.cbxSerialStopBit = new System.Windows.Forms.ComboBox();
this.label4 = new System.Windows.Forms.Label();
this.cbxSerialDataBit = new System.Windows.Forms.ComboBox();
this.label8 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.tbxTcpDestIP = new System.Windows.Forms.TextBox();
this.tbxTcpDestPort = new System.Windows.Forms.TextBox();
this.gbxTcp = new System.Windows.Forms.GroupBox();
this.gbxProtocol = new System.Windows.Forms.GroupBox();
this.btnProtocolSend = new System.Windows.Forms.Button();
this.tbxProtocolData = new System.Windows.Forms.TextBox();
this.label13 = new System.Windows.Forms.Label();
this.tbxProtocolMID = new System.Windows.Forms.TextBox();
this.label12 = new System.Windows.Forms.Label();
this.cbxProtocolOp = new System.Windows.Forms.ComboBox();
this.label11 = new System.Windows.Forms.Label();
this.tbxProtocolDebug = new System.Windows.Forms.TextBox();
this.btnClear = new System.Windows.Forms.Button();
this.gbxSerial.SuspendLayout();
this.gbxTcp.SuspendLayout();
this.gbxProtocol.SuspendLayout();
this.SuspendLayout();
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(30, 21);
this.label6.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(62, 18);
this.label6.TabIndex = 10;
this.label6.Text = "类型:";
//
// cbxDevType
//
this.cbxDevType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbxDevType.FormattingEnabled = true;
this.cbxDevType.Location = new System.Drawing.Point(118, 16);
this.cbxDevType.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.cbxDevType.Name = "cbxDevType";
this.cbxDevType.Size = new System.Drawing.Size(130, 26);
this.cbxDevType.TabIndex = 11;
this.cbxDevType.SelectedIndexChanged += new System.EventHandler(this.cbxDevType_SelectedIndexChanged);
//
// btnOpen
//
this.btnOpen.Location = new System.Drawing.Point(418, 12);
this.btnOpen.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnOpen.Name = "btnOpen";
this.btnOpen.Size = new System.Drawing.Size(142, 42);
this.btnOpen.TabIndex = 13;
this.btnOpen.Text = "打开";
this.btnOpen.UseVisualStyleBackColor = true;
this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
//
// gbxSerial
//
this.gbxSerial.Controls.Add(this.tbxSerialProtocol);
this.gbxSerial.Controls.Add(this.label10);
this.gbxSerial.Controls.Add(this.tbxSerialAddr);
this.gbxSerial.Controls.Add(this.label9);
this.gbxSerial.Controls.Add(this.label1);
this.gbxSerial.Controls.Add(this.cbxSerial);
this.gbxSerial.Controls.Add(this.label2);
this.gbxSerial.Controls.Add(this.cbxSerialCheckSum);
this.gbxSerial.Controls.Add(this.cbxSerialBaud);
this.gbxSerial.Controls.Add(this.label5);
this.gbxSerial.Controls.Add(this.label3);
this.gbxSerial.Controls.Add(this.cbxSerialStopBit);
this.gbxSerial.Controls.Add(this.label4);
this.gbxSerial.Controls.Add(this.cbxSerialDataBit);
this.gbxSerial.Location = new System.Drawing.Point(638, 96);
this.gbxSerial.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.gbxSerial.Name = "gbxSerial";
this.gbxSerial.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.gbxSerial.Size = new System.Drawing.Size(548, 218);
this.gbxSerial.TabIndex = 16;
this.gbxSerial.TabStop = false;
//
// tbxSerialProtocol
//
this.tbxSerialProtocol.Location = new System.Drawing.Point(400, 120);
this.tbxSerialProtocol.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.tbxSerialProtocol.Name = "tbxSerialProtocol";
this.tbxSerialProtocol.Size = new System.Drawing.Size(130, 28);
this.tbxSerialProtocol.TabIndex = 19;
//
// label10
//
this.label10.AutoSize = true;
this.label10.Location = new System.Drawing.Point(294, 124);
this.label10.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(98, 18);
this.label10.TabIndex = 18;
this.label10.Text = "协议类型:";
//
// tbxSerialAddr
//
this.tbxSerialAddr.Location = new System.Drawing.Point(400, 70);
this.tbxSerialAddr.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.tbxSerialAddr.Name = "tbxSerialAddr";
this.tbxSerialAddr.Size = new System.Drawing.Size(130, 28);
this.tbxSerialAddr.TabIndex = 17;
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(294, 76);
this.label9.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(62, 18);
this.label9.TabIndex = 10;
this.label9.Text = "地址:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 26);
this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 18);
this.label1.TabIndex = 0;
this.label1.Text = "串口号:";
//
// cbxSerial
//
this.cbxSerial.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbxSerial.FormattingEnabled = true;
this.cbxSerial.Location = new System.Drawing.Point(100, 21);
this.cbxSerial.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.cbxSerial.Name = "cbxSerial";
this.cbxSerial.Size = new System.Drawing.Size(130, 26);
this.cbxSerial.TabIndex = 1;
this.cbxSerial.DropDown += new System.EventHandler(this.cbxSerial_DropDown);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 76);
this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 18);
this.label2.TabIndex = 2;
this.label2.Text = "波特率:";
//
// cbxSerialCheckSum
//
this.cbxSerialCheckSum.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbxSerialCheckSum.FormattingEnabled = true;
this.cbxSerialCheckSum.Location = new System.Drawing.Point(400, 21);
this.cbxSerialCheckSum.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.cbxSerialCheckSum.Name = "cbxSerialCheckSum";
this.cbxSerialCheckSum.Size = new System.Drawing.Size(130, 26);
this.cbxSerialCheckSum.TabIndex = 9;
//
// cbxSerialBaud
//
this.cbxSerialBaud.FormattingEnabled = true;
this.cbxSerialBaud.Location = new System.Drawing.Point(100, 72);
this.cbxSerialBaud.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.cbxSerialBaud.Name = "cbxSerialBaud";
this.cbxSerialBaud.Size = new System.Drawing.Size(130, 26);
this.cbxSerialBaud.TabIndex = 3;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(294, 26);
this.label5.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(98, 18);
this.label5.TabIndex = 8;
this.label5.Text = "校验格式:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 124);
this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 18);
this.label3.TabIndex = 4;
this.label3.Text = "数据位:";
//
// cbxSerialStopBit
//
this.cbxSerialStopBit.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbxSerialStopBit.FormattingEnabled = true;
this.cbxSerialStopBit.Location = new System.Drawing.Point(100, 171);
this.cbxSerialStopBit.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.cbxSerialStopBit.Name = "cbxSerialStopBit";
this.cbxSerialStopBit.Size = new System.Drawing.Size(130, 26);
this.cbxSerialStopBit.TabIndex = 7;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(12, 176);
this.label4.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 18);
this.label4.TabIndex = 5;
this.label4.Text = "停止位:";
//
// cbxSerialDataBit
//
this.cbxSerialDataBit.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbxSerialDataBit.FormattingEnabled = true;
this.cbxSerialDataBit.Location = new System.Drawing.Point(100, 120);
this.cbxSerialDataBit.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.cbxSerialDataBit.Name = "cbxSerialDataBit";
this.cbxSerialDataBit.Size = new System.Drawing.Size(130, 26);
this.cbxSerialDataBit.TabIndex = 6;
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(9, 153);
this.label8.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(80, 18);
this.label8.TabIndex = 2;
this.label8.Text = "端口号:";
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(9, 51);
this.label7.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(80, 18);
this.label7.TabIndex = 0;
this.label7.Text = "IP地址";
//
// tbxTcpDestIP
//
this.tbxTcpDestIP.Location = new System.Drawing.Point(98, 46);
this.tbxTcpDestIP.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.tbxTcpDestIP.Name = "tbxTcpDestIP";
this.tbxTcpDestIP.Size = new System.Drawing.Size(148, 28);
this.tbxTcpDestIP.TabIndex = 3;
//
// tbxTcpDestPort
//
this.tbxTcpDestPort.Location = new System.Drawing.Point(98, 148);
this.tbxTcpDestPort.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.tbxTcpDestPort.Name = "tbxTcpDestPort";
this.tbxTcpDestPort.Size = new System.Drawing.Size(148, 28);
this.tbxTcpDestPort.TabIndex = 4;
//
// gbxTcp
//
this.gbxTcp.Controls.Add(this.tbxTcpDestPort);
this.gbxTcp.Controls.Add(this.tbxTcpDestIP);
this.gbxTcp.Controls.Add(this.label7);
this.gbxTcp.Controls.Add(this.label8);
this.gbxTcp.Location = new System.Drawing.Point(18, 76);
this.gbxTcp.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.gbxTcp.Name = "gbxTcp";
this.gbxTcp.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.gbxTcp.Size = new System.Drawing.Size(548, 218);
this.gbxTcp.TabIndex = 15;
this.gbxTcp.TabStop = false;
//
// gbxProtocol
//
this.gbxProtocol.Controls.Add(this.btnProtocolSend);
this.gbxProtocol.Controls.Add(this.tbxProtocolData);
this.gbxProtocol.Controls.Add(this.label13);
this.gbxProtocol.Controls.Add(this.tbxProtocolMID);
this.gbxProtocol.Controls.Add(this.label12);
this.gbxProtocol.Controls.Add(this.cbxProtocolOp);
this.gbxProtocol.Controls.Add(this.label11);
this.gbxProtocol.Location = new System.Drawing.Point(18, 303);
this.gbxProtocol.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.gbxProtocol.Name = "gbxProtocol";
this.gbxProtocol.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.gbxProtocol.Size = new System.Drawing.Size(548, 488);
this.gbxProtocol.TabIndex = 17;
this.gbxProtocol.TabStop = false;
//
// btnProtocolSend
//
this.btnProtocolSend.Location = new System.Drawing.Point(82, 62);
this.btnProtocolSend.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnProtocolSend.Name = "btnProtocolSend";
this.btnProtocolSend.Size = new System.Drawing.Size(450, 50);
this.btnProtocolSend.TabIndex = 23;
this.btnProtocolSend.Text = "发送";
this.btnProtocolSend.UseVisualStyleBackColor = true;
this.btnProtocolSend.Click += new System.EventHandler(this.btnProtocolSend_Click);
//
// tbxProtocolData
//
this.tbxProtocolData.Location = new System.Drawing.Point(15, 118);
this.tbxProtocolData.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.tbxProtocolData.Multiline = true;
this.tbxProtocolData.Name = "tbxProtocolData";
this.tbxProtocolData.Size = new System.Drawing.Size(516, 358);
this.tbxProtocolData.TabIndex = 22;
//
// label13
//
this.label13.AutoSize = true;
this.label13.Location = new System.Drawing.Point(12, 72);
this.label13.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(62, 18);
this.label13.TabIndex = 21;
this.label13.Text = "数据:";
//
// tbxProtocolMID
//
this.tbxProtocolMID.Location = new System.Drawing.Point(400, 21);
this.tbxProtocolMID.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.tbxProtocolMID.Name = "tbxProtocolMID";
this.tbxProtocolMID.Size = new System.Drawing.Size(130, 28);
this.tbxProtocolMID.TabIndex = 20;
this.tbxProtocolMID.Text = "0202";
//
// label12
//
this.label12.AutoSize = true;
this.label12.Location = new System.Drawing.Point(339, 26);
this.label12.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(53, 18);
this.label12.TabIndex = 11;
this.label12.Text = "MID";
//
// cbxProtocolOp
//
this.cbxProtocolOp.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbxProtocolOp.FormattingEnabled = true;
this.cbxProtocolOp.Location = new System.Drawing.Point(118, 21);
this.cbxProtocolOp.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.cbxProtocolOp.Name = "cbxProtocolOp";
this.cbxProtocolOp.Size = new System.Drawing.Size(130, 26);
this.cbxProtocolOp.TabIndex = 10;
//
// label11
//
this.label11.AutoSize = true;
this.label11.Location = new System.Drawing.Point(12, 26);
this.label11.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(98, 18);
this.label11.TabIndex = 9;
this.label11.Text = "操作方式:";
//
// tbxProtocolDebug
//
this.tbxProtocolDebug.Location = new System.Drawing.Point(590, 12);
this.tbxProtocolDebug.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.tbxProtocolDebug.Multiline = true;
this.tbxProtocolDebug.Name = "tbxProtocolDebug";
this.tbxProtocolDebug.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.tbxProtocolDebug.Size = new System.Drawing.Size(1069, 697);
this.tbxProtocolDebug.TabIndex = 23;
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(590, 735);
this.btnClear.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(282, 56);
this.btnClear.TabIndex = 25;
this.btnClear.Text = "清除";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1702, 808);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.tbxProtocolDebug);
this.Controls.Add(this.gbxProtocol);
this.Controls.Add(this.gbxSerial);
this.Controls.Add(this.gbxTcp);
this.Controls.Add(this.btnOpen);
this.Controls.Add(this.cbxDevType);
this.Controls.Add(this.label6);
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "Form1";
this.Text = "DK_DC 报文调试助手";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
this.gbxSerial.ResumeLayout(false);
this.gbxSerial.PerformLayout();
this.gbxTcp.ResumeLayout(false);
this.gbxTcp.PerformLayout();
this.gbxProtocol.ResumeLayout(false);
this.gbxProtocol.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label6;
private System.Windows.Forms.ComboBox cbxDevType;
private System.Windows.Forms.Button btnOpen;
private System.Windows.Forms.GroupBox gbxSerial;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox cbxSerial;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cbxSerialCheckSum;
private System.Windows.Forms.ComboBox cbxSerialBaud;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox cbxSerialStopBit;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.ComboBox cbxSerialDataBit;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox tbxTcpDestIP;
private System.Windows.Forms.TextBox tbxTcpDestPort;
private System.Windows.Forms.GroupBox gbxTcp;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.TextBox tbxSerialAddr;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.TextBox tbxSerialProtocol;
private System.Windows.Forms.GroupBox gbxProtocol;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.ComboBox cbxProtocolOp;
private System.Windows.Forms.TextBox tbxProtocolMID;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.TextBox tbxProtocolData;
private System.Windows.Forms.Button btnProtocolSend;
private System.Windows.Forms.TextBox tbxProtocolDebug;
private System.Windows.Forms.Button btnClear;
}
}

812
DK_DC_Tool/Form1.cs Normal file
View File

@@ -0,0 +1,812 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DK_DC_Tool
{
public enum RETURN_ERR_STATE
{
RETURN_EOK = 0,
RETURN_ERR = -1,
RETURN_ESTOP = -2,
RETURN_ECONNECT = -3,
RETURN_ETIMEOUT = -4,
RETURN_EDECOMPOSE = -5
}
public partial class Form1 : Form
{
readonly string[] dev_type_tab = new string[] { "串口", "TCP" };
readonly string[] serial_baud_tab = new string[] { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" };
readonly string[] serial_databit_tab = new string[] { "5", "6", "7", "8" };
readonly string[] serial_stopbit_tab = new string[] { "1", "1.5", "2" };
readonly string[] serial_checksum_tab = new string[] { "无", "奇校验", "偶校验", "置1", "置0" };
readonly string[] protocol_op_tab = new string[] { "R", "W" };
enum DevType { DEV_TYPE_SERIAL = 0, DEV_TYPE_TCP };
int _dev_type;
byte _serial_addr = 0;
byte _serial_protocol = 0;
SerialPort _serial = null;
TcpClient _client = null;
Thread _parse_tid = null;
bool _isconnected = false;
bool _is_run = false;
bool _is_show_packet = true;
bool _is_reconnect = true;
bool _first_connect = true;
UInt32 _reconnect_cnt = 0;
Mutex _hw_mtx = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cbxDevType.Items.AddRange(dev_type_tab);
cbxDevType.SelectedIndex = 0;
_dev_type = (int)DevType.DEV_TYPE_SERIAL;
gbxSerial.Visible = true;
gbxSerial.BringToFront();
gbxTcp.Visible = false;
string[] ports = SerialPort.GetPortNames();
cbxSerial.Items.AddRange(ports);
cbxSerial.SelectedIndex = cbxSerial.Items.Count > 0 ? 0 : -1;
cbxSerialBaud.Items.AddRange(serial_baud_tab);
cbxSerialBaud.SelectedIndex = 7;
cbxSerialDataBit.Items.AddRange(serial_databit_tab);
cbxSerialDataBit.SelectedIndex = 3;
cbxSerialStopBit.Items.AddRange(serial_stopbit_tab);
cbxSerialStopBit.SelectedIndex = 0;
cbxSerialCheckSum.Items.AddRange(serial_checksum_tab);
cbxSerialCheckSum.SelectedIndex = 0;
tbxSerialAddr.Text = "1";
tbxSerialProtocol.Text = "1";
//tbxTcpDestIP.Text = "192.168.2.32";
tbxTcpDestIP.Text = "192.168.3.31";
tbxTcpDestPort.Text = "5000";
gbxProtocol.Enabled = false;
cbxProtocolOp.Items.AddRange(protocol_op_tab);
cbxProtocolOp.SelectedIndex = 0;
_hw_mtx = new Mutex();
_parse_tid = new Thread(new ThreadStart(parse_entry));
_parse_tid.Name = "data_parse";
_parse_tid.IsBackground = true;
_parse_tid.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (_parse_tid != null)
_parse_tid.Abort();
}
finally
{
_parse_tid = null;
}
try
{
if (_serial != null)
_serial.Close();
}
finally
{
_serial = null;
}
try
{
if (_client != null)
_client.Close();
}
finally
{
_client = null;
}
Environment.Exit(0);
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (btnOpen.Text == "打开")
{
if (cbxDevType.SelectedIndex == 0)
_dev_type = (int)DevType.DEV_TYPE_SERIAL;
else
_dev_type = (int)DevType.DEV_TYPE_TCP;
if (_dev_type == (int)DevType.DEV_TYPE_SERIAL)
{
try
{
_serial_addr = byte.Parse(tbxSerialAddr.Text);
_serial_protocol = byte.Parse(tbxSerialProtocol.Text);
}
catch
{
MessageBox.Show("串口地址和协议类型配置出错");
return;
}
}
//_log.Info("\r\n\r\n\r\n");
//_log.Info("启动");
btnOpen.Text = "关闭";
cbxDevType.Enabled = false;
gbxTcp.Enabled = false;
gbxSerial.Enabled = false;
gbxProtocol.Enabled = true;
_is_run = true;
if (_dev_type == (int)DevType.DEV_TYPE_SERIAL)
_is_reconnect = false;
else
_is_reconnect = true;
_first_connect = true;
_reconnect_cnt = 0;
}
else
{
//_log.Info("\r\n\r\n\r\n");
//_log.Info("结束");
btnOpen.Text = "打开";
cbxDevType.Enabled = true;
gbxTcp.Enabled = true;
gbxSerial.Enabled = true;
gbxProtocol.Enabled = false;
_is_run = false;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
tbxProtocolDebug.Clear();
}
private void cbxDevType_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxDevType.SelectedIndex == 0)
{
gbxSerial.Visible = true;
gbxSerial.BringToFront();
gbxTcp.Visible = false;
}
else
{
gbxTcp.Visible = true;
gbxTcp.BringToFront();
gbxSerial.Visible = false;
}
}
private void cbxSerial_DropDown(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
cbxSerial.Items.Clear();
cbxSerial.Items.AddRange(ports);
cbxSerial.SelectedIndex = cbxSerial.Items.Count > 0 ? 0 : -1;
}
//代理
private delegate void change_tbx_delegate(string str);
private void tbxProtocolDebug_show(string str)
{
if (tbxProtocolDebug.InvokeRequired)
{
change_tbx_delegate cd = new change_tbx_delegate(tbxProtocolDebug_show);
tbxProtocolDebug.Invoke(cd, new object[] { str });
}
else
{
tbxProtocolDebug.Text += str;
tbxProtocolDebug.Text += "\r\n\r\n";
tbxProtocolDebug.Select(tbxProtocolDebug.TextLength, 0);
tbxProtocolDebug.ScrollToCaret();
}
}
private string bytes_to_hex_string(byte[] buf, int offset, int len)
{
string str = "";
for (int i = 0; i < len; i++)
{
str += buf[offset + i].ToString("X2") + " ";
}
return str;
}
private string bytes_to_string(byte[] buf, int offset, int len)
{
string str = "";
for (int i = 0; i < len; i++)
{
if ((buf[offset + i] < 32) || (buf[offset + i] > 126))
str += " .";
else
str += " " + ((char)buf[offset + i]).ToString();
str += " ";
}
return str;
}
private bool serial_open(ref SerialPort port, ComboBox serialNo, ComboBox serialBaud, ComboBox serialDataBit, ComboBox serialStopBit, ComboBox serialCheckSum)
{
if (!_is_run)
return false;
try
{
if (port != null)
port.Close();
port = null;
port = new SerialPort();
port.PortName = serialNo.Text;
port.BaudRate = int.Parse(serial_baud_tab[serialBaud.SelectedIndex]);
port.DataBits = int.Parse(serial_databit_tab[serialDataBit.SelectedIndex]);
switch (serialStopBit.SelectedIndex)
{
case 0:
port.StopBits = StopBits.One;
break;
case 1:
port.StopBits = StopBits.OnePointFive;
break;
case 2:
port.StopBits = StopBits.Two;
break;
default:
port.StopBits = StopBits.One;
break;
}
switch (serialCheckSum.SelectedIndex)
{
case 0:
port.Parity = Parity.None;
break;
case 1:
port.Parity = Parity.Odd;
break;
case 2:
port.Parity = Parity.Even;
break;
case 3:
port.Parity = Parity.Mark;
break;
case 4:
port.Parity = Parity.Space;
break;
default:
port.Parity = Parity.None;
break;
}
port.Handshake = Handshake.None;
port.Open();
}
catch
{
port = null;
return false;
}
return true;
}
private int serial_receive(SerialPort port, byte[] buffer, int offset, int max_len)
{
if (!_is_run)
return (int)RETURN_ERR_STATE.RETURN_ESTOP;
if (!_isconnected)
return (int)RETURN_ERR_STATE.RETURN_ECONNECT;
if (port == null)
return (int)RETURN_ERR_STATE.RETURN_ERR;
if (port.IsOpen == false)
return (int)RETURN_ERR_STATE.RETURN_ERR;
int len = 0;
int rc = 0;
while (max_len > 0)
{
Thread.Sleep(35);
if (!_is_run)
{
rc = (int)RETURN_ERR_STATE.RETURN_ESTOP;
break;
}
if (!_isconnected)
{
rc = (int)RETURN_ERR_STATE.RETURN_ECONNECT;
break;
}
try
{
if (port.BytesToRead < 0)
{
rc = (int)RETURN_ERR_STATE.RETURN_ERR;
break;
}
if (port.BytesToRead == 0)
break;
rc = port.Read(buffer, offset + len, max_len);
if (rc <= 0)
{
rc = (int)RETURN_ERR_STATE.RETURN_ERR;
break;
}
len += rc;
max_len -= rc;
}
catch
{
rc = (int)RETURN_ERR_STATE.RETURN_ERR;
break;
}
}
if (rc >= 0)
rc = len;
return rc;
}
private int serial_send(SerialPort port, byte[] buffer)
{
if (!_is_run)
return (int)RETURN_ERR_STATE.RETURN_ESTOP;
if (!_isconnected)
return (int)RETURN_ERR_STATE.RETURN_ECONNECT;
if (port == null)
return (int)RETURN_ERR_STATE.RETURN_ERR;
if (port.IsOpen == false)
return (int)RETURN_ERR_STATE.RETURN_ERR;
try
{
port.Write(buffer, 0, buffer.Length);
}
catch
{
return (int)RETURN_ERR_STATE.RETURN_ERR;
}
return buffer.Length;
}
private bool tcp_connect(ref TcpClient client, string ip, string port)
{
if (!_is_run)
return false;
try
{
if (client != null)
client.Close();
client = null;
client = new TcpClient();
if (!client.ConnectAsync(ip, int.Parse(port)).Wait(2000))
{
client.Close();
client = null;
return false;
}
}
catch
{
client = null;
return false;
}
return true;
}
private int tcp_receive(TcpClient client, byte[] buffer, int offset, int max_len)
{
if (!_is_run)
return (int)RETURN_ERR_STATE.RETURN_ESTOP;
if (!_isconnected)
return (int)RETURN_ERR_STATE.RETURN_ECONNECT;
if (client == null)
return (int)RETURN_ERR_STATE.RETURN_ERR;
if (client.Connected == false)
return (int)RETURN_ERR_STATE.RETURN_ERR;
NetworkStream networkStream = null;
try
{
networkStream = client.GetStream();
}
catch
{
return (int)RETURN_ERR_STATE.RETURN_ERR;
}
int len = 0;
int rc = 0;
while (max_len > 0)
{
Thread.Sleep(20);
if (!_is_run)
{
rc = (int)RETURN_ERR_STATE.RETURN_ESTOP;
break;
}
if (!_isconnected)
{
rc = (int)RETURN_ERR_STATE.RETURN_ECONNECT;
break;
}
try
{
if (networkStream.DataAvailable == false)
break;
rc = networkStream.Read(buffer, offset + len, max_len);
if (rc <= 0)
{
rc = (int)RETURN_ERR_STATE.RETURN_ERR;
break;
}
len += rc;
max_len -= rc;
}
catch
{
rc = (int)RETURN_ERR_STATE.RETURN_ERR;
break;
}
}
if (rc >= 0)
rc = len;
return rc;
}
private int tcp_send(TcpClient client, byte[] buffer)
{
if (!_is_run)
return (int)RETURN_ERR_STATE.RETURN_ESTOP;
if (!_isconnected)
return (int)RETURN_ERR_STATE.RETURN_ECONNECT;
if (client == null)
return (int)RETURN_ERR_STATE.RETURN_ERR;
if (client.Connected == false)
return (int)RETURN_ERR_STATE.RETURN_ERR;
try
{
NetworkStream networkStream = _client.GetStream();
networkStream.Write(buffer, 0, buffer.Length);
}
catch
{
return (int)RETURN_ERR_STATE.RETURN_ERR;
}
return buffer.Length;
}
private int hw_receive(byte[] buffer, int offset, int max_len)
{
int rc = 0;
if (_dev_type == (int)DevType.DEV_TYPE_SERIAL)
rc = serial_receive(_serial, buffer, offset, max_len);
else
rc = tcp_receive(_client, buffer, offset, max_len);
string str = null;
if(rc < 0)
{
if(rc != (int)RETURN_ERR_STATE.RETURN_ESTOP)
{
string err_str = "接收数据异常";
// _log.Warn(err_str);
str = "\r\n" + DateTime.Now.ToString() + '.' + DateTime.Now.Millisecond.ToString("d3") + "\r\n";
str += err_str + "\r\n";
}
}
else if (rc > 0)
{
//buffer = new byte[] { 0x02 ,0x00, 0x00, 0x00, 0xd9, 0x54, 0x30, 0x32, 0x30, 0x32, 0x30, 0x30, 0x30, 0x31, 0x30, 0x3d, 0x30, 0x2e, 0x32, 0x39, 0x37, 0x2c, 0x33, 0x32, 0x30, 0x38, 0x2e, 0x35, 0x36, 0x33, 0x2c, 0x31, 0x2e, 0x36, 0x35, 0x31, 0x3b, 0x30, 0x30, 0x30, 0x31, 0x31, 0x3d, 0x31, 0x3b, 0x30, 0x30, 0x30, 0x31, 0x32, 0x3d, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x31, 0x30, 0x3d, 0x30, 0x2e, 0x30, 0x31, 0x33, 0x2c, 0x2d, 0x38, 0x39, 0x2e, 0x39, 0x35, 0x34, 0x2c, 0x30, 0x2e, 0x31, 0x37, 0x34, 0x3b, 0x30, 0x31, 0x30, 0x31, 0x31, 0x3d, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x32, 0x30, 0x3d, 0x30, 0x2e, 0x30, 0x30, 0x38, 0x2c, 0x38, 0x39, 0x2e, 0x39, 0x35, 0x34, 0x2c, 0x30, 0x2e, 0x31, 0x30, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x32, 0x31, 0x3d, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x33, 0x30, 0x3d, 0x30, 0x2e, 0x30, 0x30, 0x36, 0x2c, 0x32, 0x37, 0x39, 0x39, 0x2e, 0x34, 0x37, 0x32, 0x2c, 0x30, 0x2e, 0x34, 0x30, 0x39, 0x3b, 0x30, 0x31, 0x30, 0x33, 0x31, 0x3d, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x34, 0x30, 0x3d, 0x30, 0x2e, 0x30, 0x35, 0x39, 0x2c, 0x33, 0x30, 0x37, 0x2e, 0x36, 0x37, 0x38, 0x2c, 0x30, 0x2e, 0x32, 0x35, 0x36, 0x3b, 0x30, 0x31, 0x30, 0x34, 0x31, 0x3d, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x35, 0x30, 0x3d, 0x30, 0x2e, 0x32, 0x39, 0x37, 0x2c, 0x31, 0x30, 0x2e, 0x33, 0x31, 0x33, 0x2c, 0x30, 0x2e, 0x34, 0x39, 0x32, 0x3b, 0x30, 0x31, 0x30, 0x35, 0x31, 0x3d, 0x30, 0x3b, 0x03 };
//rc = buffer.Length;
//_log.Debug("接收到数据");
string hex_str = "recv: " + bytes_to_hex_string(buffer, offset, rc);
string raw_str = " " + bytes_to_string(buffer, offset, rc);
//_log.Debug(hex_str);
//_log.Debug(raw_str);
if (_is_show_packet)
{
str = "\r\n" + DateTime.Now.ToString() + '.' + DateTime.Now.Millisecond.ToString("d3") + "\r\n";
str += hex_str + "\r\n" + raw_str + "\r\n";
}
}
if (str != null)
tbxProtocolDebug_show(str);
return rc;
}
private int hw_send(ToolPacket packet)
{
int rc = 0;
ToolPacket send_packet = null;
if (_dev_type == (int)DevType.DEV_TYPE_SERIAL)
{
ToolSerialPacket serial_packet = new ToolSerialPacket();
serial_packet.addr = _serial_addr;
serial_packet.protocol = _serial_protocol;
serial_packet.op = packet.op;
serial_packet.mid = packet.mid;
serial_packet.data_domain_str = packet.data_domain_str;
send_packet = serial_packet;
}
else
{
send_packet = packet;
}
byte[] buffer = send_packet.compose_protocol();
_hw_mtx.WaitOne();
if (_dev_type == (int)DevType.DEV_TYPE_SERIAL)
rc = serial_send(_serial, buffer);
else
rc = tcp_send(_client, buffer);
_hw_mtx.ReleaseMutex();
string str = null;
if (rc < 0)
{
if(rc != (int)RETURN_ERR_STATE.RETURN_ESTOP)
{
string err_str = "发送失败";
//_log.Warn(err_str);
str = "\r\n" + DateTime.Now.ToString() + '.' + DateTime.Now.Millisecond.ToString("d3") + "\r\n";
str += err_str + "\r\n";
}
}
else
{
//_log.Debug("发送数据");
string hex_str = "send: " + bytes_to_hex_string(buffer, 0, buffer.Length);
string raw_str = " " + bytes_to_string(buffer, 0, buffer.Length);
//_log.Debug(hex_str);
//_log.Debug(raw_str);
if (_is_show_packet)
{
str = "\r\n" + DateTime.Now.ToString() + '.' + DateTime.Now.Millisecond.ToString("d3") + "\r\n";
str += hex_str + "\r\n" + raw_str + "\r\n";
}
}
if(str != null)
tbxProtocolDebug_show(str);
return rc;
}
byte[] normal_buf = new byte[1024];
private void parse_entry()
{
Control.CheckForIllegalCrossThreadCalls = false;
while (true)
{
// 未使能运行时,关闭连接,等待使能
if(!_is_run)
{
try
{
if (_serial != null)
_serial.Close();
}
finally
{
_serial = null;
}
try
{
if (_client != null)
_client.Close();
}
finally
{
_client = null;
}
_isconnected = false;
_first_connect = true;
Thread.Sleep(50);
continue;
}
// 未连接时,连接硬件接口,失败 2s 周期重连
if(!_isconnected)
{
// 不使能重连
if(!_is_reconnect && !_first_connect)
{
try
{
if (_serial != null)
_serial.Close();
}
finally
{
_serial = null;
}
try
{
if (_client != null)
_client.Close();
}
finally
{
_client = null;
}
Thread.Sleep(50);
continue;
}
_first_connect = false;
_reconnect_cnt++;
if (_dev_type == (int)DevType.DEV_TYPE_SERIAL)
{
if (serial_open(ref _serial, cbxSerial, cbxSerialBaud, cbxSerialDataBit, cbxSerialStopBit, cbxSerialCheckSum))
{
tbxProtocolDebug_show("\r\n\r\n" + DateTime.Now.ToString() + '.' + DateTime.Now.Millisecond.ToString("d3") + "\r\n打开串口成功!\r\n");
//_log.Info("打开串口成功!");
_isconnected = true;
Thread.Sleep(50);
continue;
}
else
{
tbxProtocolDebug_show("\r\n\r\n" + DateTime.Now.ToString() + '.' + DateTime.Now.Millisecond.ToString("d3") + "\r\n打开串口失败!\r\n");
//_log.Warn("打开串口失败!");
}
}
else
{
if (tcp_connect(ref _client, tbxTcpDestIP.Text, tbxTcpDestPort.Text))
{
tbxProtocolDebug_show("\r\n\r\n" + DateTime.Now.ToString() + '.' + DateTime.Now.Millisecond.ToString("d3") + "\r\n网络连接成功!\r\n");
//_log.Info("网络连接成功!");
_isconnected = true;
Thread.Sleep(50);
continue;
}
else
{
tbxProtocolDebug_show("\r\n\r\n" + DateTime.Now.ToString() + '.' + DateTime.Now.Millisecond.ToString("d3") + "\r\n网络连接失败!\r\n");
//_log.Warn("网络连接失败!");
}
}
Thread.Sleep(2000);
continue;
}
int recv_len = hw_receive(normal_buf, 0, normal_buf.Length);
if (recv_len < 0)
{
Thread.Sleep(50);
_isconnected = false;
continue;
}
}
}
private void btnProtocolSend_Click(object sender, EventArgs e)
{
ToolPacket packet = new ToolPacket();
if (cbxProtocolOp.SelectedIndex == 0)
packet.op = 'R';
else
packet.op = 'W';
packet.mid = tbxProtocolMID.Text;
packet.data_domain_str = tbxProtocolData.Text;
hw_send(packet);
}
}
}

120
DK_DC_Tool/Form1.resx Normal file
View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

22
DK_DC_Tool/Program.cs Normal file
View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DK_DC_Tool
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("DK_DC_Tool")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DK_DC_Tool")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("3c6fef50-2014-4157-a12b-93fb4ad20fe7")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,63 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:4.0.30319.42000
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace DK_DC_Tool.Properties {
using System;
/// <summary>
/// 一个强类型的资源类,用于查找本地化的字符串等。
/// </summary>
// 此类是由 StronglyTypedResourceBuilder
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
// (以 /str 作为命令选项),或重新生成 VS 项目。
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// 返回此类使用的缓存的 ResourceManager 实例。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DK_DC_Tool.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// 重写当前线程的 CurrentUICulture 属性,对
/// 使用此强类型资源类的所有资源查找执行重写。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
}
}

View File

@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@@ -0,0 +1,26 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:4.0.30319.42000
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace DK_DC_Tool.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.9.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
}
}

View File

@@ -0,0 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
<Settings />
</SettingsFile>

482
DK_DC_Tool/Tighten.cs Normal file
View File

@@ -0,0 +1,482 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DK_DC_Tool
{
public class Tighten
{
public static Dictionary<string, string> TightenNG_Code = new Dictionary<string, string>()
{
// 00 = 无定意01 = 最终扭矩过大02 = 最终扭矩过大03 = 最终角度过大04 = 最终角度过小n1 = 第 n 步扭矩过大n2 = 第 n 步时间超限1 < n < 590 = 总时间超限;
{ "00", "无定意" },
{ "01", "最终扭矩过大" },
{ "02", "最终扭矩过大" },
{ "03", "最终角度过大" },
{ "04", "最终角度过小" },
{ "11", "第 1 步扭矩过大" },
{ "21", "第 2 步扭矩过大" },
{ "31", "第 3 步扭矩过大" },
{ "41", "第 4 步扭矩过大" },
{ "51", "第 5 步扭矩过大" },
{ "12", "第 1 步时间超限" },
{ "22", "第 2 步时间超限" },
{ "32", "第 3 步时间超限" },
{ "42", "第 4 步时间超限" },
{ "52", "第 5 步时间超限" },
{ "90", "总时间超限" }
};
TcpClient _client = null;
Thread _parse_tid = null;
Thread _tightenHeartBeat = null;
bool _isconnected = false;
bool _is_run = false;
bool _is_reconnect = true;
bool _first_connect = true;
string ip = "127.0.0.1";
int port = 5000;
int _reconnect_cnt = 0;
string currentOpName = "127.0.0.1";
public static Action<String,String> E_ScanContent = null;
public static Action<String, String, String> E_LogInsert = null;
public static Action<String> E_DeviceHeartBeat = null;
public void Start(string ip ,int port, string opName)
{
this.currentOpName = opName;
this.ip = ip;
this.port = port;
_parse_tid = new Thread(new ThreadStart(parse_entry));
_parse_tid.Name = "data_parse";
_parse_tid.IsBackground = true;
_parse_tid.Start();
_tightenHeartBeat = new Thread(new ThreadStart(hw_send_HeartBeat));
_tightenHeartBeat.Name = "tightenHeartBeat";
_tightenHeartBeat.IsBackground = true;
_tightenHeartBeat.Start();
_is_run = true;
_is_reconnect = true;
_first_connect = true;
_reconnect_cnt = 0;
}
public void Stop()
{
_is_run = false;
_is_reconnect = false;
_first_connect = false;
}
//代理
private delegate void change_tbx_delegate(string str);
private string bytes_to_hex_string(byte[] buf, int offset, int len)
{
string str = "";
for (int i = 0; i < len; i++)
{
str += buf[offset + i].ToString("X2") + " ";
}
return str;
}
private string bytes_to_string(byte[] buf, int offset, int len)
{
string str = "";
for (int i = 0; i < len; i++)
{
if ((buf[offset + i] < 32) || (buf[offset + i] > 126))
str += " .";
else
str += " " + ((char)buf[offset + i]).ToString();
str += " ";
}
return str;
}
private bool tcp_connect(ref TcpClient client, string ip, string port)
{
if (!_is_run)
return false;
try
{
if (client != null)
client.Close();
client = null;
client = new TcpClient();
if (!client.ConnectAsync(ip, int.Parse(port)).Wait(2000))
{
client.Close();
client = null;
return false;
}
}
catch(Exception err)
{
E_LogInsert("Tighten", "Error", "165" + err.Message);
if (E_DeviceHeartBeat != null)
{
E_DeviceHeartBeat.Invoke("0");
}
client = null;
return false;
}
return true;
}
private int tcp_receive(TcpClient client, byte[] buffer, int offset, int max_len)
{
if (!_is_run)
return (int)RETURN_ERR_STATE.RETURN_ESTOP;
if (!_isconnected)
return (int)RETURN_ERR_STATE.RETURN_ECONNECT;
if (client == null)
return (int)RETURN_ERR_STATE.RETURN_ERR;
if (client.Connected == false)
return (int)RETURN_ERR_STATE.RETURN_ERR;
NetworkStream networkStream = null;
try
{
networkStream = client.GetStream();
}
catch
{
return (int)RETURN_ERR_STATE.RETURN_ERR;
}
int len = 0;
int rc = 0;
while (max_len > 0)
{
Thread.Sleep(20);
if (!_is_run)
{
rc = (int)RETURN_ERR_STATE.RETURN_ESTOP;
break;
}
if (!_isconnected)
{
rc = (int)RETURN_ERR_STATE.RETURN_ECONNECT;
break;
}
try
{
if (networkStream.DataAvailable == false)
break;
rc = networkStream.Read(buffer, offset + len, max_len);
if (rc <= 0)
{
rc = (int)RETURN_ERR_STATE.RETURN_ERR;
break;
}
len += rc;
max_len -= rc;
}
catch
{
rc = (int)RETURN_ERR_STATE.RETURN_ERR;
break;
}
}
if (rc >= 0)
rc = len;
return rc;
}
private int tcp_send(TcpClient client, byte[] buffer)
{
if (!_is_run)
return (int)RETURN_ERR_STATE.RETURN_ESTOP;
if (!_isconnected)
return (int)RETURN_ERR_STATE.RETURN_ECONNECT;
if (client == null)
return (int)RETURN_ERR_STATE.RETURN_ERR;
if (client.Connected == false)
return (int)RETURN_ERR_STATE.RETURN_ERR;
try
{
NetworkStream networkStream = _client.GetStream();
networkStream.Write(buffer, 0, buffer.Length);
}
catch(Exception err)
{
E_LogInsert("Tighten", "Error", "270" + err.Message);
return (int)RETURN_ERR_STATE.RETURN_ERR;
}
return buffer.Length;
}
private int hw_receive(byte[] buffer, int offset, int max_len)
{
int rc = tcp_receive(_client, buffer, offset, max_len);
string str = null;
if(rc < 0)
{
if(rc != (int)RETURN_ERR_STATE.RETURN_ESTOP)
{
string err_str = "拧紧枪接收数据异常!";
E_LogInsert("Tighten", "Error", err_str);
}
}
else if (rc > 0)
{
//buffer = new byte[] { 0x02 ,0x00, 0x00, 0x00, 0xd9, 0x54, 0x30, 0x32, 0x30, 0x32, 0x30, 0x30, 0x30, 0x31, 0x30, 0x3d, 0x30, 0x2e, 0x32, 0x39, 0x37, 0x2c, 0x33, 0x32, 0x30, 0x38, 0x2e, 0x35, 0x36, 0x33, 0x2c, 0x31, 0x2e, 0x36, 0x35, 0x31, 0x3b, 0x30, 0x30, 0x30, 0x31, 0x31, 0x3d, 0x31, 0x3b, 0x30, 0x30, 0x30, 0x31, 0x32, 0x3d, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x31, 0x30, 0x3d, 0x30, 0x2e, 0x30, 0x31, 0x33, 0x2c, 0x2d, 0x38, 0x39, 0x2e, 0x39, 0x35, 0x34, 0x2c, 0x30, 0x2e, 0x31, 0x37, 0x34, 0x3b, 0x30, 0x31, 0x30, 0x31, 0x31, 0x3d, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x32, 0x30, 0x3d, 0x30, 0x2e, 0x30, 0x30, 0x38, 0x2c, 0x38, 0x39, 0x2e, 0x39, 0x35, 0x34, 0x2c, 0x30, 0x2e, 0x31, 0x30, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x32, 0x31, 0x3d, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x33, 0x30, 0x3d, 0x30, 0x2e, 0x30, 0x30, 0x36, 0x2c, 0x32, 0x37, 0x39, 0x39, 0x2e, 0x34, 0x37, 0x32, 0x2c, 0x30, 0x2e, 0x34, 0x30, 0x39, 0x3b, 0x30, 0x31, 0x30, 0x33, 0x31, 0x3d, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x34, 0x30, 0x3d, 0x30, 0x2e, 0x30, 0x35, 0x39, 0x2c, 0x33, 0x30, 0x37, 0x2e, 0x36, 0x37, 0x38, 0x2c, 0x30, 0x2e, 0x32, 0x35, 0x36, 0x3b, 0x30, 0x31, 0x30, 0x34, 0x31, 0x3d, 0x30, 0x3b, 0x30, 0x31, 0x30, 0x35, 0x30, 0x3d, 0x30, 0x2e, 0x32, 0x39, 0x37, 0x2c, 0x31, 0x30, 0x2e, 0x33, 0x31, 0x33, 0x2c, 0x30, 0x2e, 0x34, 0x39, 0x32, 0x3b, 0x30, 0x31, 0x30, 0x35, 0x31, 0x3d, 0x30, 0x3b, 0x03 };
//rc = buffer.Length;
var hex_str = bytes_to_hex_string(buffer, offset, rc);
var raw_str = bytes_to_string(buffer, offset, rc);
if (E_ScanContent != null)
{
E_ScanContent.Invoke(hex_str, raw_str);
}
}
//if (str != null)
// tbxProtocolDebug_show(str);
return rc;
}
private int hw_send(ToolPacket packet)
{
int rc = 0;
ToolPacket send_packet = null;
send_packet = packet;
byte[] buffer = send_packet.compose_protocol();
rc = tcp_send(_client, buffer);
string str = null;
if (rc < 0)
{
try
{
E_LogInsert("Tighten", "Error", ((RETURN_ERR_STATE)rc).ToString());
}
catch (Exception err)
{
E_LogInsert("Tighten", "Error", rc.ToString() + "_" + err.Message);
}
}
else
{
string send_str = "拧紧枪发送数据!";
E_LogInsert("Tighten", "Info", send_str);
//string hex_str = "send: " + bytes_to_hex_string(buffer, 0, buffer.Length);
string raw_str = " " + bytes_to_string(buffer, 0, buffer.Length);
E_LogInsert("Tighten", "Info", raw_str);
}
//if(str != null)
// tbxProtocolDebug_show(str);
return rc;
}
private void hw_send_HeartBeat()
{
ToolPacket packet = new ToolPacket();
packet.op = 'R';
packet.mid = "0204";
packet.data_domain_str = "";
new Thread(new ThreadStart(delegate () {
while (true)
{
try
{
int rc = 0;
byte[] buffer = packet.compose_protocol();
rc = tcp_send(_client, buffer);
if (rc < 0)
{
if (E_DeviceHeartBeat != null)
{
E_DeviceHeartBeat.Invoke("0");
}
E_LogInsert("Tighten_HeartBeat", "Error", RETURN_ERR_STATE.RETURN_ESTOP.ToString());
}
else
{
if (E_DeviceHeartBeat != null)
{
E_DeviceHeartBeat.Invoke("1");
}
}
}
catch (Exception err)
{
E_LogInsert("Tighten_HeartBeat", "Error", err.Message);
if (E_DeviceHeartBeat != null)
{
E_DeviceHeartBeat.Invoke("0");
}
}
Thread.Sleep(5000);
}
}))
{ IsBackground = true }.Start();
}
byte[] normal_buf = new byte[1024];
private void parse_entry()
{
Control.CheckForIllegalCrossThreadCalls = false;
while (true)
{
// 未使能运行时,关闭连接,等待使能
if(!_is_run)
{
try
{
if (_client != null)
_client.Close();
}
finally
{
_client = null;
}
_isconnected = false;
_first_connect = true;
Thread.Sleep(50);
continue;
}
// 未连接时,连接硬件接口,失败 2s 周期重连
if(!_isconnected)
{
// 不使能重连
if(!_is_reconnect && !_first_connect)
{
try
{
if (_client != null)
_client.Close();
}
finally
{
_client = null;
}
Thread.Sleep(50);
continue;
}
_first_connect = false;
_reconnect_cnt++;
if (tcp_connect(ref _client, this.ip, this.port.ToString()))
{
string send_str = "拧紧枪网络连接成功";
E_LogInsert("Tighten", "Info", send_str);
_isconnected = true;
if (E_DeviceHeartBeat != null)
{
E_DeviceHeartBeat.Invoke("1");
}
/*
MID 操作类型 描述 备注
0001 R 建立通信连接
0002 R 断开连接
0103 W Pset选择
0201 R 运行状态 Ready/RUN/OK/NG/Err
0202 R 最终拧紧结果
0203 R 实时曲线数据
0301 W 电机运行正转/反转使能 CW/CCW
*/
var packet = new ToolPacket();
packet.op = 'R';
packet.mid = "0202";
packet.data_domain_str = "";
hw_send(packet);
Thread.Sleep(50);
continue;
}
else
{
string send_str = "拧紧枪网络连接失败";
E_LogInsert("Tighten", "Error", send_str);
if (E_DeviceHeartBeat != null)
{
E_DeviceHeartBeat.Invoke("0");
}
}
Thread.Sleep(2000);
continue;
}
int recv_len = hw_receive(normal_buf, 0, normal_buf.Length);
if (recv_len < 0)
{
Thread.Sleep(50);
_isconnected = false;
if (E_DeviceHeartBeat != null)
{
E_DeviceHeartBeat.Invoke("0");
}
continue;
}
}
}
}
public class TightenParam
{
public string hex { get; set; }
public string raw { get; set; }
}
}

290
DK_DC_Tool/ToolPacket.cs Normal file
View File

@@ -0,0 +1,290 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DK_DC_Tool
{
public class ToolDataDomain
{
public string pid;
public List<string> data;
}
public class ToolPacket:ICloneable
{
const byte head = 0x02;
const byte tail = 0x03;
const byte min_length = 11;
public int type; //0: TCP 报文格式 1: 串口报文格式
public UInt32 data_packet_length;
public char op;
public string mid;
public string data_domain_str;
public List<ToolDataDomain> data_domains;
public object Clone()
{
ToolPacket packet = (ToolPacket)this.MemberwiseClone();
return packet;
}
static List<ToolDataDomain> get_data_domains(string str)
{
List<ToolDataDomain> tds = new List<ToolDataDomain>();
string[] rs = str.Split(new char[1] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in rs)
{
s.Trim();
string[] ds = s.Split(new char[1] { '=' }, StringSplitOptions.RemoveEmptyEntries);
if (ds.Length != 2)
continue;
ToolDataDomain data_domain = new ToolDataDomain();
data_domain.pid = ds[0].Trim();
string[] cs = ds[1].Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string cs2 in cs)
{
cs2.Trim();
}
data_domain.data = new List<string>(cs);
tds.Add(data_domain);
}
return tds;
}
public void update_data_domain_str()
{
this.data_domain_str = "";
foreach (ToolDataDomain domain in this.data_domains)
{
this.data_domain_str += domain.pid + "=";
for (int i = 0; i < domain.data.Count; i++)
{
this.data_domain_str += domain.data[i];
if (i != domain.data.Count - 1)
this.data_domain_str += ",";
}
this.data_domain_str += ";";
}
}
public virtual byte[] compose_protocol()
{
List<byte> data_list = new List<byte>();
byte[] tmp = new byte[100];
data_list.Add(head);
int data_len = 5 + this.data_domain_str.Length;
Array.Clear(tmp, 0, tmp.Length);
tmp[0] = (byte)(data_len >> 24);
tmp[1] = (byte)(data_len >> 16);
tmp[2] = (byte)(data_len >> 8);
tmp[3] = (byte)(data_len);
data_list.AddRange(tmp.Take(4));
data_list.Add((byte)this.op);
byte[] mid_buf = Encoding.UTF8.GetBytes(this.mid);
Array.Clear(tmp, 0, tmp.Length);
Array.Copy(mid_buf, tmp, (mid_buf.Length > 4) ? 4 : mid_buf.Length);
data_list.AddRange(tmp.Take(4));
data_list.AddRange(Encoding.UTF8.GetBytes(this.data_domain_str).ToList());
data_list.Add(tail);
this.type = 0;
this.data_domains = get_data_domains(this.data_domain_str);
this.data_packet_length = (UInt32)data_list.Count;
return data_list.ToArray();
}
public static int decompose_protocol(byte[] buf, ToolPacket packet)
{
int rc = 0;
do
{
if (buf.Length - min_length < 0)
break;
if (buf[0] != head)
break;
UInt32 _data_packet_len = (((UInt32)buf[1] << 24) +
((UInt32)buf[2] << 16) +
((UInt32)buf[3] << 8) +
(UInt32)buf[4]);
if ((buf.Length - (min_length + _data_packet_len - 5)) < 0)
break;
if (buf[5 + _data_packet_len] != tail)
break;
if (_data_packet_len < 5)
break;
try
{
packet.type = 0;
packet.data_packet_length = _data_packet_len;
packet.op = (char)buf[5];
packet.mid = Encoding.UTF8.GetString(buf, 6, 4);
packet.data_domain_str = Encoding.UTF8.GetString(buf, 10, (int)_data_packet_len - 5);
packet.data_domains = get_data_domains(packet.data_domain_str);
}
catch
{
break;
}
rc = 6 + (int)_data_packet_len;
} while (false);
return rc;
}
}
internal class ToolSerialPacket : ToolPacket
{
const byte head = 0x55;
const byte min_length = 7;
public byte addr;
public byte protocol;
/* Table of CRC values for high-order byte */
static Byte[] table_crc_hi = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
};
/* Table of CRC values for low-order byte */
static Byte[] table_crc_lo = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
};
static UInt16 crc16(Byte[] buffer, int buffer_length)
{
Byte crc_hi = 0xFF; /* high CRC byte initialized */
Byte crc_lo = 0xFF; /* low CRC byte initialized */
UInt32 i; /* will index into CRC lookup */
UInt32 j = 0;
/* pass through message buffer */
while (buffer_length-- > 0)
{
i = (UInt32)(crc_hi ^ buffer[j++]); /* calculate the CRC */
crc_hi = (Byte)(crc_lo ^ table_crc_hi[i]);
crc_lo = table_crc_lo[i];
}
return (UInt16)(crc_hi << 8 | crc_lo);
}
public override byte[] compose_protocol()
{
byte[] packet_data = base.compose_protocol();
this.type = 1;
List<byte> data_list = new List<byte>();
data_list.Add(head);
data_list.Add(this.addr);
data_list.Add(this.protocol);
int data_len = packet_data.Length;
data_list.Add((byte)(data_len >> 8));
data_list.Add((byte)data_len);
data_list.AddRange(packet_data.ToList());
UInt16 crc_calculated = crc16(data_list.ToArray(), data_list.Count);
data_list.Add((byte)(crc_calculated >> 8));
data_list.Add((byte)crc_calculated);
return data_list.ToArray();
}
public static int decompose_protocol(byte[] buf, ToolSerialPacket packet)
{
int rc = 0;
do
{
if (buf.Length - min_length < 0)
break;
if (buf[0] != head)
break;
UInt32 _data_packet_len = ((UInt32)buf[3] << 8) + (UInt32)buf[4];
if ((buf.Length - (min_length + _data_packet_len)) < 0)
break;
UInt16 crc_recv = (UInt16)((buf[5 + _data_packet_len] << 8) + buf[6 + _data_packet_len]);
UInt16 crc_calc = crc16(buf, 5 + (int)_data_packet_len);
if (crc_recv != crc_calc)
break;
if (ToolPacket.decompose_protocol(buf.Skip(5).Take((int)_data_packet_len).ToArray(), packet) <= 0)
break;
packet.type = 1;
packet.addr = buf[1];
packet.protocol = buf[2];
rc = min_length + (int)_data_packet_len;
} while (false);
return rc;
}
}
}

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.14" targetFramework="net472" />
</packages>