├── McProtocol_CS ├── McProtocolTester │ ├── app.config │ ├── Properties │ │ ├── Settings.settings │ │ ├── Settings.Designer.cs │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ └── Resources.resx │ ├── Program.cs │ ├── Form1.Designer.cs │ ├── McProtocolTester.csproj │ ├── Form1.cs │ └── Form1.resx ├── McProtocol │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── McProtocol.csproj │ └── McProtocol.cs └── McProtocol.sln ├── .gitattributes └── .gitignore /McProtocol_CS/McProtocolTester/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocolTester/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocolTester/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Windows.Forms; 5 | 6 | namespace McProtocolTester 7 | { 8 | static class Program 9 | { 10 | /// 11 | /// アプリケーションのメイン エントリ ポイントです。 12 | /// 13 | [STAThread] 14 | static void Main() 15 | { 16 | Application.EnableVisualStyles(); 17 | Application.SetCompatibleTextRenderingDefault(false); 18 | Application.Run(new Form1()); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocolTester/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // このコードはツールによって生成されました。 4 | // ランタイム バージョン:4.0.30319.18033 5 | // 6 | // このファイルへの変更は、以下の状況下で不正な動作の原因になったり、 7 | // コードが再生成されるときに損失したりします。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace McProtocolTester.Properties { 12 | 13 | 14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { 17 | 18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 19 | 20 | public static Settings Default { 21 | get { 22 | return defaultInstance; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocol/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // アセンブリに関する一般情報は以下の属性セットをとおして制御されます。 6 | // アセンブリに関連付けられている情報を変更するには、 7 | // これらの属性値を変更してください。 8 | [assembly: AssemblyTitle("McProtocol")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("McProtocol")] 13 | [assembly: AssemblyCopyright("Copyright © 2013 Norio Satoh")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // ComVisible を false に設定すると、その型はこのアセンブリ内で COM コンポーネントから 18 | // 参照不可能になります。COM からこのアセンブリ内の型にアクセスする場合は、 19 | // その型の ComVisible 属性を true に設定してください。 20 | [assembly: ComVisible(false)] 21 | 22 | // 次の GUID は、このプロジェクトが COM に公開される場合の、typelib の ID です 23 | [assembly: Guid("b5c71742-5714-4c79-b5fa-4288a3043306")] 24 | 25 | // アセンブリのバージョン情報は、以下の 4 つの値で構成されています: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を 33 | // 既定値にすることができます: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.1.0.0")] 36 | [assembly: AssemblyFileVersion("1.1.0.0")] 37 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocolTester/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // アセンブリに関する一般情報は以下の属性セットをとおして制御されます。 6 | // アセンブリに関連付けられている情報を変更するには、 7 | // これらの属性値を変更してください。 8 | [assembly: AssemblyTitle("McProtocolTester")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("McProtocolTester")] 13 | [assembly: AssemblyCopyright("Copyright © 2013 Norio Satoh")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // ComVisible を false に設定すると、その型はこのアセンブリ内で COM コンポーネントから 18 | // 参照不可能になります。COM からこのアセンブリ内の型にアクセスする場合は、 19 | // その型の ComVisible 属性を true に設定してください。 20 | [assembly: ComVisible(false)] 21 | 22 | // 次の GUID は、このプロジェクトが COM に公開される場合の、typelib の ID です 23 | [assembly: Guid("c947ba4d-4a9d-4cbe-8c72-4e18f13f5337")] 24 | 25 | // アセンブリのバージョン情報は、以下の 4 つの値で構成されています: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を 33 | // 既定値にすることができます: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocol/McProtocol.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {607EBD9E-4E24-441F-BF5A-0832A0146282} 8 | Library 9 | Properties 10 | Nc 11 | McProtocol 12 | v4.5 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 53 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocol.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "McProtocol", "McProtocol\McProtocol.csproj", "{607EBD9E-4E24-441F-BF5A-0832A0146282}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "McProtocolTester", "McProtocolTester\McProtocolTester.csproj", "{AD1032D7-A38C-4740-8A2E-67FA2733C743}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Debug|Mixed Platforms = Debug|Mixed Platforms 12 | Debug|x86 = Debug|x86 13 | Release|Any CPU = Release|Any CPU 14 | Release|Mixed Platforms = Release|Mixed Platforms 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {607EBD9E-4E24-441F-BF5A-0832A0146282}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {607EBD9E-4E24-441F-BF5A-0832A0146282}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {607EBD9E-4E24-441F-BF5A-0832A0146282}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU 21 | {607EBD9E-4E24-441F-BF5A-0832A0146282}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU 22 | {607EBD9E-4E24-441F-BF5A-0832A0146282}.Debug|x86.ActiveCfg = Debug|Any CPU 23 | {607EBD9E-4E24-441F-BF5A-0832A0146282}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {607EBD9E-4E24-441F-BF5A-0832A0146282}.Release|Any CPU.Build.0 = Release|Any CPU 25 | {607EBD9E-4E24-441F-BF5A-0832A0146282}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU 26 | {607EBD9E-4E24-441F-BF5A-0832A0146282}.Release|Mixed Platforms.Build.0 = Release|Any CPU 27 | {607EBD9E-4E24-441F-BF5A-0832A0146282}.Release|x86.ActiveCfg = Release|Any CPU 28 | {AD1032D7-A38C-4740-8A2E-67FA2733C743}.Debug|Any CPU.ActiveCfg = Debug|x86 29 | {AD1032D7-A38C-4740-8A2E-67FA2733C743}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 30 | {AD1032D7-A38C-4740-8A2E-67FA2733C743}.Debug|Mixed Platforms.Build.0 = Debug|x86 31 | {AD1032D7-A38C-4740-8A2E-67FA2733C743}.Debug|x86.ActiveCfg = Debug|x86 32 | {AD1032D7-A38C-4740-8A2E-67FA2733C743}.Debug|x86.Build.0 = Debug|x86 33 | {AD1032D7-A38C-4740-8A2E-67FA2733C743}.Release|Any CPU.ActiveCfg = Release|x86 34 | {AD1032D7-A38C-4740-8A2E-67FA2733C743}.Release|Mixed Platforms.ActiveCfg = Release|x86 35 | {AD1032D7-A38C-4740-8A2E-67FA2733C743}.Release|Mixed Platforms.Build.0 = Release|x86 36 | {AD1032D7-A38C-4740-8A2E-67FA2733C743}.Release|x86.ActiveCfg = Release|x86 37 | {AD1032D7-A38C-4740-8A2E-67FA2733C743}.Release|x86.Build.0 = Release|x86 38 | EndGlobalSection 39 | GlobalSection(SolutionProperties) = preSolution 40 | HideSolutionNode = FALSE 41 | EndGlobalSection 42 | EndGlobal 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | [Dd]ebug/ 46 | [Rr]elease/ 47 | *_i.c 48 | *_p.c 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.vspscc 63 | .builds 64 | *.dotCover 65 | 66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 67 | #packages/ 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | 80 | # ReSharper is a .NET coding add-in 81 | _ReSharper* 82 | 83 | # Installshield output folder 84 | [Ee]xpress 85 | 86 | # DocProject is a documentation generator add-in 87 | DocProject/buildhelp/ 88 | DocProject/Help/*.HxT 89 | DocProject/Help/*.HxC 90 | DocProject/Help/*.hhc 91 | DocProject/Help/*.hhk 92 | DocProject/Help/*.hhp 93 | DocProject/Help/Html2 94 | DocProject/Help/html 95 | 96 | # Click-Once directory 97 | publish 98 | 99 | # Others 100 | [Bb]in 101 | [Oo]bj 102 | sql 103 | TestResults 104 | *.Cache 105 | ClientBin 106 | stylecop.* 107 | ~$* 108 | *.dbmdl 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Backup & report files from converting an old project file to a newer 112 | # Visual Studio version. Backup files are not needed, because we have git ;-) 113 | _UpgradeReport_Files/ 114 | Backup*/ 115 | UpgradeLog*.XML 116 | 117 | 118 | 119 | ############ 120 | ## Windows 121 | ############ 122 | 123 | # Windows image file caches 124 | Thumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | 130 | ############# 131 | ## Python 132 | ############# 133 | 134 | *.py[co] 135 | 136 | # Packages 137 | *.egg 138 | *.egg-info 139 | dist 140 | build 141 | eggs 142 | parts 143 | bin 144 | var 145 | sdist 146 | develop-eggs 147 | .installed.cfg 148 | 149 | # Installer logs 150 | pip-log.txt 151 | 152 | # Unit test / coverage reports 153 | .coverage 154 | .tox 155 | 156 | #Translations 157 | *.mo 158 | 159 | #Mr Developer 160 | .mr.developer.cfg 161 | 162 | # Mac crap 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocolTester/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // このコードはツールによって生成されました。 4 | // ランタイム バージョン:4.0.30319.18033 5 | // 6 | // このファイルへの変更は、以下の状況下で不正な動作の原因になったり、 7 | // コードが再生成されるときに損失したりします。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace McProtocolTester.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// ローカライズされた文字列などを検索するための、厳密に型指定されたリソース クラスです。 17 | /// 18 | // このクラスは StronglyTypedResourceBuilder クラスが ResGen 19 | // または Visual Studio のようなツールを使用して自動生成されました。 20 | // メンバーを追加または削除するには、.ResX ファイルを編集して、/str オプションと共に 21 | // ResGen を実行し直すか、または VS プロジェクトをビルドし直します。 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// このクラスで使用されているキャッシュされた ResourceManager インスタンスを返します。 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("McProtocolTester.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// 厳密に型指定されたこのリソース クラスを使用して、すべての検索リソースに対し、 51 | /// 現在のスレッドの CurrentUICulture プロパティをオーバーライドします。 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocolTester/Form1.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace McProtocolTester 2 | { 3 | partial class Form1 4 | { 5 | /// 6 | /// 必要なデザイナー変数です。 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// 使用中のリソースをすべてクリーンアップします。 12 | /// 13 | /// マネージ リソースが破棄される場合 true、破棄されない場合は false です。 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows フォーム デザイナーで生成されたコード 24 | 25 | /// 26 | /// デザイナー サポートに必要なメソッドです。このメソッドの内容を 27 | /// コード エディターで変更しないでください。 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.listBox1 = new System.Windows.Forms.ListBox(); 32 | this.comboBox1 = new System.Windows.Forms.ComboBox(); 33 | this.SuspendLayout(); 34 | // 35 | // listBox1 36 | // 37 | this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 38 | | System.Windows.Forms.AnchorStyles.Left) 39 | | System.Windows.Forms.AnchorStyles.Right))); 40 | this.listBox1.FormattingEnabled = true; 41 | this.listBox1.ItemHeight = 12; 42 | this.listBox1.Location = new System.Drawing.Point(12, 12); 43 | this.listBox1.Name = "listBox1"; 44 | this.listBox1.Size = new System.Drawing.Size(293, 292); 45 | this.listBox1.TabIndex = 0; 46 | // 47 | // comboBox1 48 | // 49 | this.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 50 | | System.Windows.Forms.AnchorStyles.Right))); 51 | this.comboBox1.FormattingEnabled = true; 52 | this.comboBox1.Location = new System.Drawing.Point(12, 310); 53 | this.comboBox1.Name = "comboBox1"; 54 | this.comboBox1.Size = new System.Drawing.Size(293, 20); 55 | this.comboBox1.TabIndex = 0; 56 | this.comboBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.comboBox1_KeyDown); 57 | // 58 | // Form1 59 | // 60 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 61 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 62 | this.ClientSize = new System.Drawing.Size(317, 337); 63 | this.Controls.Add(this.comboBox1); 64 | this.Controls.Add(this.listBox1); 65 | this.Name = "Form1"; 66 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 67 | this.Text = "Form1"; 68 | this.ResumeLayout(false); 69 | 70 | } 71 | 72 | #endregion 73 | 74 | private System.Windows.Forms.ListBox listBox1; 75 | private System.Windows.Forms.ComboBox comboBox1; 76 | } 77 | } 78 | 79 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocolTester/McProtocolTester.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {AD1032D7-A38C-4740-8A2E-67FA2733C743} 9 | WinExe 10 | Properties 11 | McProtocolTester 12 | McProtocolTester 13 | v4.5 14 | 15 | 16 | 512 17 | 18 | 19 | x86 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | false 28 | 29 | 30 | x86 31 | pdbonly 32 | true 33 | bin\Release\ 34 | TRACE 35 | prompt 36 | 4 37 | false 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | Form 54 | 55 | 56 | Form1.cs 57 | 58 | 59 | 60 | 61 | Form1.cs 62 | 63 | 64 | ResXFileCodeGenerator 65 | Resources.Designer.cs 66 | Designer 67 | 68 | 69 | True 70 | Resources.resx 71 | True 72 | 73 | 74 | 75 | SettingsSingleFileGenerator 76 | Settings.Designer.cs 77 | 78 | 79 | True 80 | Settings.settings 81 | True 82 | 83 | 84 | 85 | 86 | {607ebd9e-4e24-441f-bf5a-0832a0146282} 87 | McProtocol 88 | 89 | 90 | 91 | 98 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocolTester/Form1.cs: -------------------------------------------------------------------------------- 1 | using System.Globalization; 2 | using System.Windows.Forms; 3 | using Nc.Mitsubishi; 4 | 5 | namespace McProtocolTester 6 | { 7 | public partial class Form1 : Form 8 | { 9 | private readonly Plc FApp; 10 | public Form1() 11 | { 12 | FApp = new McProtocolTcp("192.168.40.103", 0x1394); 13 | // FApp = new McProtocolUdp("192.168.40.103", 0x1395); 14 | FApp.Open(); 15 | InitializeComponent(); 16 | } 17 | 18 | private void comboBox1_KeyDown(object sender, KeyEventArgs e) 19 | { 20 | if (e.KeyCode == Keys.Enter) 21 | { 22 | ComboBox cb = comboBox1; 23 | string buff = cb.Text; 24 | if (0 < buff.IndexOf(',')) 25 | { // "D10,2"のパターン 26 | string[] s = buff.Split(','); 27 | if (1 < s.Length) 28 | { 29 | PlcDeviceType type; 30 | int addr; 31 | McProtocolApp.GetDeviceCode(s[0], out type, out addr); 32 | 33 | var val = new int[int.Parse(s[1])]; 34 | int rtCode = McProtocolApp.IsBitDevice(type) ? FApp.GetBitDevice(s[0], val.Length, val) : 35 | FApp.ReadDeviceBlock(s[0], val.Length, val); 36 | if (0 < rtCode) 37 | { 38 | listBox1.Items.Add("ERROR:0x" + rtCode.ToString("X4")); 39 | } 40 | else 41 | { 42 | for (int i = 0; i < val.Length; ++i) 43 | { 44 | listBox1.Items.Add(type.ToString() + (addr + i).ToString(CultureInfo.InvariantCulture) + "=" + val[i].ToString(CultureInfo.InvariantCulture)); 45 | } 46 | } 47 | } 48 | } 49 | else if (0 < buff.IndexOf('=')) 50 | { 51 | string[] s = buff.Split('='); 52 | if (0 < s[0].IndexOf("..", System.StringComparison.Ordinal)) 53 | { // "D10..12=0"のパターン 54 | string[] t = s[0].Replace("..", "=").Split('='); 55 | int m; 56 | int n = int.Parse(t[1]); 57 | PlcDeviceType type; 58 | McProtocolApp.GetDeviceCode(t[0], out type, out m); 59 | var data = new int[n - m + 1]; 60 | int v = int.Parse(s[1]); 61 | for (int i = 0; i < data.Length; ++i) 62 | { 63 | data[i] = v; 64 | } 65 | int rtCode = McProtocolApp.IsBitDevice(type) ? FApp.SetBitDevice(t[0], data.Length, data) : 66 | FApp.WriteDeviceBlock(t[0], data.Length, data); 67 | listBox1.Items.Add(buff.ToUpper()); 68 | if (0 < rtCode) 69 | { 70 | listBox1.Items.Add("ERROR:0x" + rtCode.ToString("X4")); 71 | } 72 | } 73 | else 74 | { // "D10=0"のパターン 75 | PlcDeviceType type; 76 | int addr; 77 | McProtocolApp.GetDeviceCode(s[0], out type, out addr); 78 | 79 | int val = int.Parse(s[1]); 80 | int rtCode; 81 | if (McProtocolApp.IsBitDevice(type)) 82 | { 83 | var data = new int[1]; 84 | data[0] = val; 85 | rtCode = FApp.SetBitDevice(s[0], data.Length, data); 86 | } 87 | else 88 | { 89 | rtCode = FApp.SetDevice(s[0], val); 90 | } 91 | listBox1.Items.Add(buff.ToUpper()); 92 | if (0 < rtCode) 93 | { 94 | listBox1.Items.Add("ERROR:0x" + rtCode.ToString("X4")); 95 | } 96 | } 97 | } 98 | else 99 | { // "D10"のパターン 100 | PlcDeviceType type; 101 | int addr; 102 | McProtocolApp.GetDeviceCode(buff.ToUpper(), out type, out addr); 103 | 104 | int n; 105 | int rtCode; 106 | if (McProtocolApp.IsBitDevice(type)) 107 | { 108 | var data = new int[1]; 109 | rtCode = FApp.GetBitDevice(buff, data.Length, data); 110 | n = data[0]; 111 | } 112 | else 113 | { 114 | rtCode = FApp.GetDevice(buff.ToUpper(), out n); 115 | } 116 | listBox1.Items.Add(buff.ToUpper() + "=" + n.ToString(CultureInfo.InvariantCulture)); 117 | if (0 < rtCode) 118 | { 119 | listBox1.Items.Add("ERROR:0x" + rtCode.ToString("X4")); 120 | } 121 | } 122 | listBox1.SelectedIndex = listBox1.Items.Count - 1; 123 | cb.Items.Insert(0, cb.Text); 124 | cb.Text = ""; 125 | } 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocolTester/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocolTester/Form1.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /McProtocol_CS/McProtocol/McProtocol.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | using System.Net.Sockets; 5 | using System.Net; 6 | 7 | namespace Nc 8 | { 9 | namespace Mitsubishi 10 | { 11 | // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 12 | public enum McFrame 13 | { 14 | MC3E 15 | , MC4E 16 | } 17 | 18 | // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 19 | // PLCデバイスの種類を定義した列挙体 20 | public enum PlcDeviceType 21 | { 22 | // PLC用デバイス 23 | M = 0x90 24 | , SM = 0x91 25 | , L = 0x92 26 | , F = 0x93 27 | , V = 0x94 28 | , S = 0x98 29 | , X = 0x9C 30 | , Y = 0x9D 31 | , B = 0xA0 32 | , SB = 0xA1 33 | , DX = 0xA2 34 | , DY = 0xA3 35 | , D = 0xA8 36 | , SD = 0xA9 37 | , R = 0xAF 38 | , ZR = 0xB0 39 | , W = 0xB4 40 | , SW = 0xB5 41 | , TC = 0xC0 42 | , TS = 0xC1 43 | , TN = 0xC2 44 | , CC = 0xC3 45 | , CS = 0xC4 46 | , CN = 0xC5 47 | , SC = 0xC6 48 | , SS = 0xC7 49 | , SN = 0xC8 50 | , Z = 0xCC 51 | , TT 52 | , TM 53 | , CT 54 | , CM 55 | , A 56 | , Max 57 | } 58 | 59 | // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 60 | // PLCと接続するための共通のインターフェースを定義する 61 | public interface Plc : IDisposable 62 | { 63 | int Open(); 64 | int Close(); 65 | int SetBitDevice(string iDeviceName, int iSize, int[] iData); 66 | int SetBitDevice(PlcDeviceType iType, int iAddress, int iSize, int[] iData); 67 | int GetBitDevice(string iDeviceName, int iSize, int[] oData); 68 | int GetBitDevice(PlcDeviceType iType, int iAddress, int iSize, int[] oData); 69 | int WriteDeviceBlock(string iDeviceName, int iSize, int[] iData); 70 | int WriteDeviceBlock(PlcDeviceType iType, int iAddress, int iSize, int[] iData); 71 | int ReadDeviceBlock(string iDeviceName, int iSize, int[] oData); 72 | int ReadDeviceBlock(PlcDeviceType iType, int iAddress, int iSize, int[] oData); 73 | int SetDevice(string iDeviceName, int iData); 74 | int SetDevice(PlcDeviceType iType, int iAddress, int iData); 75 | int GetDevice(string iDeviceName, out int oData); 76 | int GetDevice(PlcDeviceType iType, int iAddress, out int oData); 77 | } 78 | // ######################################################################################## 79 | abstract public class McProtocolApp : Plc 80 | { 81 | // ==================================================================================== 82 | public McFrame CommandFrame { get; set; } // 使用フレーム 83 | public string HostName { get; set; } // ホスト名またはIPアドレス 84 | public int PortNumber { get; set; } // ポート番号 85 | // ==================================================================================== 86 | // コンストラクタ 87 | protected McProtocolApp(string iHostName, int iPortNumber) 88 | { 89 | CommandFrame = McFrame.MC3E; 90 | HostName = iHostName; 91 | PortNumber = iPortNumber; 92 | } 93 | 94 | // ==================================================================================== 95 | // 後処理 96 | public void Dispose() 97 | { 98 | Close(); 99 | } 100 | 101 | // ==================================================================================== 102 | public int Open() 103 | { 104 | DoConnect(); 105 | Command = new McCommand(CommandFrame); 106 | return 0; 107 | } 108 | // ==================================================================================== 109 | public int Close() 110 | { 111 | DoDisconnect(); 112 | return 0; 113 | } 114 | // ==================================================================================== 115 | public int SetBitDevice(string iDeviceName, int iSize, int[] iData) 116 | { 117 | PlcDeviceType type; 118 | int addr; 119 | GetDeviceCode(iDeviceName, out type, out addr); 120 | return SetBitDevice(type, addr, iSize, iData); 121 | } 122 | // ==================================================================================== 123 | public int SetBitDevice(PlcDeviceType iType, int iAddress, int iSize, int[] iData) 124 | { 125 | var type = iType; 126 | var addr = iAddress; 127 | var data = new List(6) 128 | { 129 | (byte) addr 130 | , (byte) (addr >> 8) 131 | , (byte) (addr >> 16) 132 | , (byte) type 133 | , (byte) iSize 134 | , (byte) (iSize >> 8) 135 | }; 136 | var d = (byte)iData[0]; 137 | var i = 0; 138 | while (i < iData.Length) 139 | { 140 | if (i % 2 == 0) 141 | { 142 | d = (byte)iData[i]; 143 | d <<= 4; 144 | } 145 | else 146 | { 147 | d |= (byte)(iData[i] & 0x01); 148 | data.Add(d); 149 | } 150 | ++i; 151 | } 152 | if (i % 2 != 0) 153 | { 154 | data.Add(d); 155 | } 156 | byte[] sdCommand = Command.SetCommand(0x1401, 0x0001, data.ToArray()); 157 | byte[] rtResponse = TryExecution(sdCommand); 158 | int rtCode = Command.SetResponse(rtResponse); 159 | return rtCode; 160 | } 161 | // ==================================================================================== 162 | public int GetBitDevice(string iDeviceName, int iSize, int[] oData) 163 | { 164 | PlcDeviceType type; 165 | int addr; 166 | GetDeviceCode(iDeviceName, out type, out addr); 167 | return GetBitDevice(type, addr, iSize, oData); 168 | } 169 | // ==================================================================================== 170 | public int GetBitDevice(PlcDeviceType iType, int iAddress, int iSize, int[] oData) 171 | { 172 | PlcDeviceType type = iType; 173 | int addr = iAddress; 174 | var data = new List(6) 175 | { 176 | (byte) addr 177 | , (byte) (addr >> 8) 178 | , (byte) (addr >> 16) 179 | , (byte) type 180 | , (byte) iSize 181 | , (byte) (iSize >> 8) 182 | }; 183 | byte[] sdCommand = Command.SetCommand(0x0401, 0x0001, data.ToArray()); 184 | byte[] rtResponse = TryExecution(sdCommand); 185 | int rtCode = Command.SetResponse(rtResponse); 186 | byte[] rtData = Command.Response; 187 | for (int i = 0; i < iSize; ++i) 188 | { 189 | if (i % 2 == 0) 190 | { 191 | oData[i] = (rtCode == 0) ? ((rtData[i / 2] >> 4) & 0x01) : 0; 192 | } 193 | else 194 | { 195 | oData[i] = (rtCode == 0) ? (rtData[i / 2] & 0x01) : 0; 196 | } 197 | } 198 | return rtCode; 199 | } 200 | // ==================================================================================== 201 | public int WriteDeviceBlock(string iDeviceName, int iSize, int[] iData) 202 | { 203 | PlcDeviceType type; 204 | int addr; 205 | GetDeviceCode(iDeviceName, out type, out addr); 206 | return WriteDeviceBlock(type, addr, iSize, iData); 207 | } 208 | // ==================================================================================== 209 | public int WriteDeviceBlock(PlcDeviceType iType, int iAddress, int iSize, int[] iData) 210 | { 211 | PlcDeviceType type = iType; 212 | int addr = iAddress; 213 | var data = new List(6) 214 | { 215 | (byte) addr 216 | , (byte) (addr >> 8) 217 | , (byte) (addr >> 16) 218 | , (byte) type 219 | , (byte) iSize 220 | , (byte) (iSize >> 8) 221 | }; 222 | foreach (int t in iData) 223 | { 224 | data.Add((byte)t); 225 | data.Add((byte)(t >> 8)); 226 | } 227 | byte[] sdCommand = Command.SetCommand(0x1401, 0x0000, data.ToArray()); 228 | byte[] rtResponse = TryExecution(sdCommand); 229 | int rtCode = Command.SetResponse(rtResponse); 230 | return rtCode; 231 | } 232 | // ==================================================================================== 233 | public int ReadDeviceBlock(string iDeviceName, int iSize, int[] oData) 234 | { 235 | PlcDeviceType type; 236 | int addr; 237 | GetDeviceCode(iDeviceName, out type, out addr); 238 | return ReadDeviceBlock(type, addr, iSize, oData); 239 | } 240 | // ==================================================================================== 241 | public int ReadDeviceBlock(PlcDeviceType iType, int iAddress, int iSize, int[] oData) 242 | { 243 | PlcDeviceType type = iType; 244 | int addr = iAddress; 245 | var data = new List(6) 246 | { 247 | (byte) addr 248 | , (byte) (addr >> 8) 249 | , (byte) (addr >> 16) 250 | , (byte) type 251 | , (byte) iSize 252 | , (byte) (iSize >> 8) 253 | }; 254 | byte[] sdCommand = Command.SetCommand(0x0401, 0x0000, data.ToArray()); 255 | byte[] rtResponse = TryExecution(sdCommand); 256 | int rtCode = Command.SetResponse(rtResponse); 257 | byte[] rtData = Command.Response; 258 | for (int i = 0; i < iSize; ++i) 259 | { 260 | oData[i] = (rtCode == 0) ? BitConverter.ToInt16(rtData, i * 2) : 0; 261 | } 262 | return rtCode; 263 | } 264 | // ==================================================================================== 265 | public int SetDevice(string iDeviceName, int iData) 266 | { 267 | PlcDeviceType type; 268 | int addr; 269 | GetDeviceCode(iDeviceName, out type, out addr); 270 | return SetDevice(type, addr, iData); 271 | } 272 | // ==================================================================================== 273 | public int SetDevice(PlcDeviceType iType, int iAddress, int iData) 274 | { 275 | PlcDeviceType type = iType; 276 | int addr = iAddress; 277 | var data = new List(6) 278 | { 279 | (byte) addr 280 | , (byte) (addr >> 8) 281 | , (byte) (addr >> 16) 282 | , (byte) type 283 | , 0x01 284 | , 0x00 285 | , (byte) iData 286 | , (byte) (iData >> 8) 287 | }; 288 | byte[] sdCommand = Command.SetCommand(0x1401, 0x0000, data.ToArray()); 289 | byte[] rtResponse = TryExecution(sdCommand); 290 | int rtCode = Command.SetResponse(rtResponse); 291 | return rtCode; 292 | } 293 | // ==================================================================================== 294 | public int GetDevice(string iDeviceName, out int oData) 295 | { 296 | PlcDeviceType type; 297 | int addr; 298 | GetDeviceCode(iDeviceName, out type, out addr); 299 | return GetDevice(type, addr, out oData); 300 | } 301 | // ==================================================================================== 302 | public int GetDevice(PlcDeviceType iType, int iAddress, out int oData) 303 | { 304 | PlcDeviceType type = iType; 305 | int addr = iAddress; 306 | var data = new List(6) 307 | { 308 | (byte) addr 309 | , (byte) (addr >> 8) 310 | , (byte) (addr >> 16) 311 | , (byte) type 312 | , 0x01 313 | , 0x00 314 | }; 315 | byte[] sdCommand = Command.SetCommand(0x0401, 0x0000, data.ToArray()); 316 | byte[] rtResponse = TryExecution(sdCommand); 317 | int rtCode = Command.SetResponse(rtResponse); 318 | if (0 < rtCode) 319 | { 320 | oData = 0; 321 | } 322 | else 323 | { 324 | byte[] rtData = Command.Response; 325 | oData = BitConverter.ToInt16(rtData, 0); 326 | } 327 | return rtCode; 328 | } 329 | // ==================================================================================== 330 | //public int GetCpuType(out string oCpuName, out int oCpuType) 331 | //{ 332 | // int rtCode = Command.Execute(0x0101, 0x0000, new byte[0]); 333 | // oCpuName = "dummy"; 334 | // oCpuType = 0; 335 | // return rtCode; 336 | //} 337 | // ==================================================================================== 338 | public static PlcDeviceType GetDeviceType(string s) 339 | { 340 | return (s == "M") ? PlcDeviceType.M : 341 | (s == "SM") ? PlcDeviceType.SM : 342 | (s == "L") ? PlcDeviceType.L : 343 | (s == "F") ? PlcDeviceType.F : 344 | (s == "V") ? PlcDeviceType.V : 345 | (s == "S") ? PlcDeviceType.S : 346 | (s == "X") ? PlcDeviceType.X : 347 | (s == "Y") ? PlcDeviceType.Y : 348 | (s == "B") ? PlcDeviceType.B : 349 | (s == "SB") ? PlcDeviceType.SB : 350 | (s == "DX") ? PlcDeviceType.DX : 351 | (s == "DY") ? PlcDeviceType.DY : 352 | (s == "D") ? PlcDeviceType.D : 353 | (s == "SD") ? PlcDeviceType.SD : 354 | (s == "R") ? PlcDeviceType.R : 355 | (s == "ZR") ? PlcDeviceType.ZR : 356 | (s == "W") ? PlcDeviceType.W : 357 | (s == "SW") ? PlcDeviceType.SW : 358 | (s == "TC") ? PlcDeviceType.TC : 359 | (s == "TS") ? PlcDeviceType.TS : 360 | (s == "TN") ? PlcDeviceType.TN : 361 | (s == "CC") ? PlcDeviceType.CC : 362 | (s == "CS") ? PlcDeviceType.CS : 363 | (s == "CN") ? PlcDeviceType.CN : 364 | (s == "SC") ? PlcDeviceType.SC : 365 | (s == "SS") ? PlcDeviceType.SS : 366 | (s == "SN") ? PlcDeviceType.SN : 367 | (s == "Z") ? PlcDeviceType.Z : 368 | (s == "TT") ? PlcDeviceType.TT : 369 | (s == "TM") ? PlcDeviceType.TM : 370 | (s == "CT") ? PlcDeviceType.CT : 371 | (s == "CM") ? PlcDeviceType.CM : 372 | (s == "A") ? PlcDeviceType.A : 373 | PlcDeviceType.Max; 374 | } 375 | 376 | // ==================================================================================== 377 | public static bool IsBitDevice(PlcDeviceType type) 378 | { 379 | return !((type == PlcDeviceType.D) 380 | || (type == PlcDeviceType.SD) 381 | || (type == PlcDeviceType.Z) 382 | || (type == PlcDeviceType.ZR) 383 | || (type == PlcDeviceType.R) 384 | || (type == PlcDeviceType.W)); 385 | } 386 | 387 | // ==================================================================================== 388 | public static bool IsHexDevice(PlcDeviceType type) 389 | { 390 | return (type == PlcDeviceType.X) 391 | || (type == PlcDeviceType.Y) 392 | || (type == PlcDeviceType.B) 393 | || (type == PlcDeviceType.W); 394 | } 395 | 396 | // ==================================================================================== 397 | public static void GetDeviceCode(string iDeviceName, out PlcDeviceType oType, out int oAddress) 398 | { 399 | string s = iDeviceName.ToUpper(); 400 | string strAddress; 401 | 402 | // 1文字取り出す 403 | string strType = s.Substring(0, 1); 404 | switch (strType) 405 | { 406 | case "A": 407 | case "B": 408 | case "D": 409 | case "F": 410 | case "L": 411 | case "M": 412 | case "R": 413 | case "V": 414 | case "W": 415 | case "X": 416 | case "Y": 417 | // 2文字目以降は数値のはずなので変換する 418 | strAddress = s.Substring(1); 419 | break; 420 | case "Z": 421 | // もう1文字取り出す 422 | strType = s.Substring(0, 2); 423 | // ファイルレジスタの場合 : 2 424 | // インデックスレジスタの場合 : 1 425 | strAddress = s.Substring(strType.Equals("ZR") ? 2 : 1); 426 | break; 427 | case "C": 428 | // もう1文字取り出す 429 | strType = s.Substring(0, 2); 430 | switch (strType) 431 | { 432 | case "CC": 433 | case "CM": 434 | case "CN": 435 | case "CS": 436 | case "CT": 437 | strAddress = s.Substring(2); 438 | break; 439 | default: 440 | throw new Exception("Invalid format."); 441 | } 442 | break; 443 | case "S": 444 | // もう1文字取り出す 445 | strType = s.Substring(0, 2); 446 | switch (strType) 447 | { 448 | case "SD": 449 | case "SM": 450 | strAddress = s.Substring(2); 451 | break; 452 | default: 453 | throw new Exception("Invalid format."); 454 | } 455 | break; 456 | case "T": 457 | // もう1文字取り出す 458 | strType = s.Substring(0, 2); 459 | switch (strType) 460 | { 461 | case "TC": 462 | case "TM": 463 | case "TN": 464 | case "TS": 465 | case "TT": 466 | strAddress = s.Substring(2); 467 | break; 468 | default: 469 | throw new Exception("Invalid format."); 470 | } 471 | break; 472 | default: 473 | throw new Exception("Invalid format."); 474 | } 475 | 476 | oType = GetDeviceType(strType); 477 | oAddress = IsHexDevice(oType) ? Convert.ToInt32(strAddress, BlockSize) : 478 | Convert.ToInt32(strAddress); 479 | } 480 | // &&&&& protected &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 481 | abstract protected void DoConnect(); 482 | abstract protected void DoDisconnect(); 483 | abstract protected byte[] Execute(byte[] iCommand); 484 | // &&&&& private &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 485 | private const int BlockSize = 0x0010; 486 | private McCommand Command { get; set; } 487 | // ================================================================================ 488 | private byte[] TryExecution(byte[] iCommand) 489 | { 490 | byte[] rtResponse; 491 | int tCount = 10; 492 | do 493 | { 494 | rtResponse = Execute(iCommand); 495 | --tCount; 496 | if (tCount < 0) 497 | { 498 | throw new Exception("PLCから正しい値が取得できません."); 499 | } 500 | } while (Command.IsIncorrectResponse(rtResponse)); 501 | return rtResponse; 502 | } 503 | // #################################################################################### 504 | // 通信に使用するコマンドを表現するインナークラス 505 | class McCommand 506 | { 507 | private McFrame FrameType { get; set; } // フレーム種別 508 | private uint SerialNumber { get; set; } // シリアル番号 509 | private uint NetwrokNumber { get; set; } // ネットワーク番号 510 | private uint PcNumber { get; set; } // PC番号 511 | private uint IoNumber { get; set; } // 要求先ユニットI/O番号 512 | private uint ChannelNumber { get; set; } // 要求先ユニット局番号 513 | private uint CpuTimer { get; set; } // CPU監視タイマ 514 | private int ResultCode { get; set; } // 終了コード 515 | public byte[] Response { get; private set; } // 応答データ 516 | // ================================================================================ 517 | // コンストラクタ 518 | public McCommand(McFrame iFrame) 519 | { 520 | FrameType = iFrame; 521 | SerialNumber = 0x0001u; 522 | NetwrokNumber = 0x0000u; 523 | PcNumber = 0x00FFu; 524 | IoNumber = 0x03FFu; 525 | ChannelNumber = 0x0000u; 526 | CpuTimer = 0x0010u; 527 | } 528 | // ================================================================================ 529 | public byte[] SetCommand(uint iMainCommand, uint iSubCommand, byte[] iData) 530 | { 531 | var dataLength = (uint)(iData.Length + 6); 532 | var ret = new List(iData.Length + 20); 533 | uint frame = (FrameType == McFrame.MC3E) ? 0x0050u : 534 | (FrameType == McFrame.MC4E) ? 0x0054u : 0x0000u; 535 | ret.Add((byte)frame); 536 | ret.Add((byte)(frame >> 8)); 537 | if (FrameType == McFrame.MC4E) 538 | { 539 | ret.Add((byte)SerialNumber); 540 | ret.Add((byte)(SerialNumber >> 8)); 541 | ret.Add(0x00); 542 | ret.Add(0x00); 543 | } 544 | ret.Add((byte)NetwrokNumber); 545 | ret.Add((byte)PcNumber); 546 | ret.Add((byte)IoNumber); 547 | ret.Add((byte)(IoNumber >> 8)); 548 | ret.Add((byte)ChannelNumber); 549 | ret.Add((byte)dataLength); 550 | ret.Add((byte)(dataLength >> 8)); 551 | ret.Add((byte)CpuTimer); 552 | ret.Add((byte)(CpuTimer >> 8)); 553 | ret.Add((byte)iMainCommand); 554 | ret.Add((byte)(iMainCommand >> 8)); 555 | ret.Add((byte)iSubCommand); 556 | ret.Add((byte)(iSubCommand >> 8)); 557 | ret.AddRange(iData); 558 | return ret.ToArray(); 559 | } 560 | // ================================================================================ 561 | public int SetResponse(byte[] iResponse) 562 | { 563 | int min = (FrameType == McFrame.MC3E) ? 11 : 15; 564 | if (min <= iResponse.Length) 565 | { 566 | var btCount = new[] { iResponse[min - 4], iResponse[min - 3] }; 567 | var btCode = new[] { iResponse[min - 2], iResponse[min - 1] }; 568 | int rsCount = BitConverter.ToUInt16(btCount, 0); 569 | ResultCode = BitConverter.ToUInt16(btCode, 0); 570 | Response = new byte[rsCount - 2]; 571 | Buffer.BlockCopy(iResponse, min, Response, 0, Response.Length); 572 | } 573 | return ResultCode; 574 | } 575 | // ================================================================================ 576 | public bool IsIncorrectResponse(byte[] iResponse) 577 | { 578 | var min = (FrameType == McFrame.MC3E) ? 11 : 15; 579 | var btCount = new[] { iResponse[min - 4], iResponse[min - 3] }; 580 | var btCode = new[] { iResponse[min - 2], iResponse[min - 1] }; 581 | var rsCount = BitConverter.ToUInt16(btCount, 0) - 2; 582 | var rsCode = BitConverter.ToUInt16(btCode, 0); 583 | return (rsCode == 0 && rsCount != (iResponse.Length - min)); 584 | } 585 | } 586 | } 587 | 588 | // ######################################################################################## 589 | public class McProtocolTcp : McProtocolApp 590 | { 591 | // ==================================================================================== 592 | // コンストラクタ 593 | public McProtocolTcp() : this("", 0) { } 594 | public McProtocolTcp(string iHostName, int iPortNumber) 595 | : base(iHostName, iPortNumber) 596 | { 597 | Client = new TcpClient(); 598 | } 599 | 600 | // &&&&& protected &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 601 | override protected void DoConnect() 602 | { 603 | TcpClient c = Client; 604 | if (!c.Connected) 605 | { 606 | // Keep Alive機能の実装 607 | var ka = new List(sizeof(uint) * 3); 608 | ka.AddRange(BitConverter.GetBytes(1u)); 609 | ka.AddRange(BitConverter.GetBytes(45000u)); 610 | ka.AddRange(BitConverter.GetBytes(5000u)); 611 | c.Client.IOControl(IOControlCode.KeepAliveValues, ka.ToArray(), null); 612 | c.Connect(HostName, PortNumber); 613 | Stream = c.GetStream(); 614 | } 615 | } 616 | // ==================================================================================== 617 | override protected void DoDisconnect() 618 | { 619 | TcpClient c = Client; 620 | if (c.Connected) 621 | { 622 | c.Close(); 623 | } 624 | } 625 | // ================================================================================ 626 | override protected byte[] Execute(byte[] iCommand) 627 | { 628 | NetworkStream ns = Stream; 629 | ns.Write(iCommand, 0, iCommand.Length); 630 | ns.Flush(); 631 | 632 | using (var ms = new MemoryStream()) 633 | { 634 | var buff = new byte[256]; 635 | do 636 | { 637 | int sz = ns.Read(buff, 0, buff.Length); 638 | if (sz == 0) 639 | { 640 | throw new Exception("切断されました"); 641 | } 642 | ms.Write(buff, 0, sz); 643 | } while (ns.DataAvailable); 644 | return ms.ToArray(); 645 | } 646 | } 647 | // &&&&& private &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 648 | private TcpClient Client { get; set; } 649 | private NetworkStream Stream { get; set; } 650 | } 651 | // ######################################################################################## 652 | public class McProtocolUdp : McProtocolApp 653 | { 654 | // ==================================================================================== 655 | // コンストラクタ 656 | public McProtocolUdp(int iPortNumber) : this("", iPortNumber) { } 657 | public McProtocolUdp(string iHostName, int iPortNumber) 658 | : base(iHostName, iPortNumber) 659 | { 660 | Client = new UdpClient(iPortNumber); 661 | } 662 | 663 | // &&&&& protected &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 664 | override protected void DoConnect() 665 | { 666 | UdpClient c = Client; 667 | c.Connect(HostName, PortNumber); 668 | } 669 | // ==================================================================================== 670 | override protected void DoDisconnect() 671 | { 672 | // UDPでは何もしない 673 | } 674 | // ================================================================================ 675 | override protected byte[] Execute(byte[] iCommand) 676 | { 677 | UdpClient c = Client; 678 | // 送信 679 | c.Send(iCommand, iCommand.Length); 680 | 681 | using (var ms = new MemoryStream()) 682 | { 683 | IPAddress ip = IPAddress.Parse(HostName); 684 | var ep = new IPEndPoint(ip, PortNumber); 685 | do 686 | { 687 | // 受信 688 | byte[] buff = c.Receive(ref ep); 689 | ms.Write(buff, 0, buff.Length); 690 | } while (0 < c.Available); 691 | return ms.ToArray(); 692 | } 693 | } 694 | // &&&&& private &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 695 | private UdpClient Client { get; set; } 696 | } 697 | } // namespace Mitsubishi 698 | } // namespace Nc 699 | --------------------------------------------------------------------------------