diff --git a/ModbusStudy/CommClient/Client/OpcUaMaster.cs b/ModbusStudy/CommClient/Client/OpcUaMaster.cs new file mode 100644 index 0000000..92aba39 --- /dev/null +++ b/ModbusStudy/CommClient/Client/OpcUaMaster.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Opc.Ua; +using Opc.Ua.Client; + +namespace MasterClient.Client +{ + class OpcUaMaster : ICommClient + { + private readonly string ENDPOINT_TMPL = "opc.tcp://{0}:{1}"; + private Session session; + + private string ip; + private int port; + + public void Connect() + { + try + { + if (GetStatus()) + Disconnect(); + + ConfiguredEndpoint endPoint = new ConfiguredEndpoint(null, new EndpointDescription(CreateEndpointURL())); + + ApplicationConfiguration config = CreateOpcUaConfig(); + + session = Session.Create(config, endPoint, true, "MySession", 1000, null, null).Result; + } + catch (Exception ex) + { + throw ex; + } + } + + public void Disconnect() + { + if (!GetStatus()) + throw new Exception("Not connected"); + + try + { + session.Close(); + } + catch (Exception ex) + { + throw ex; + } + } + + public bool GetStatus() + { + return session != null && session.Connected; + } + + public string ReadValue(string name) + { + if (!GetStatus()) + throw new Exception("Not connected"); + + try + { + return session.ReadValue(name).Value.ToString(); + } + catch (Exception ex) + { + throw ex; + } + } + + public void SetServer(string ip, int port) + { + this.ip = ip; + this.port = port; + } + + public void WriteValue(string name, string value) + { + if (!GetStatus()) + throw new Exception("Not connected"); + + try + { + WriteValue nodeToWrite = new WriteValue() + { + NodeId = new NodeId(name), + AttributeId = Attributes.Value, + Value = new DataValue() + }; + + Variant writeValue = new Variant(value); + nodeToWrite.Value.WrappedValue = writeValue; + + WriteValueCollection nodesToWrite = new WriteValueCollection(); + nodesToWrite.Add(nodeToWrite); + + StatusCodeCollection results = null; + DiagnosticInfoCollection diagnosticInfos = null; + + ResponseHeader responseHeader = session.Write( + null, + nodesToWrite, + out results, + out diagnosticInfos); + + ClientBase.ValidateResponse(results, nodesToWrite); + ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToWrite); + + if (StatusCode.IsBad(results[0])) + throw ServiceResultException.Create(results[0], 0, diagnosticInfos, responseHeader.StringTable); + } + catch (Exception ex) + { + throw ex; + } + } + + private ApplicationConfiguration CreateOpcUaConfig() + { + ApplicationConfiguration config = new ApplicationConfiguration() + { + ApplicationName = "OPC-UA-Client", + ApplicationType = ApplicationType.Client, + + SecurityConfiguration = new SecurityConfiguration() + { + ApplicationCertificate = new CertificateIdentifier() + }, + + ClientConfiguration = new ClientConfiguration() + { + DefaultSessionTimeout = 30 * 1000 + } + }; + + config.Validate(ApplicationType.Client); + + return config; + } + + private string CreateEndpointURL() + { + return string.Format(ENDPOINT_TMPL, ip, port); + } + } +} diff --git a/ModbusStudy/CommClient/MainForm.cs b/ModbusStudy/CommClient/MainForm.cs index 442c7b8..c6e96e0 100644 --- a/ModbusStudy/CommClient/MainForm.cs +++ b/ModbusStudy/CommClient/MainForm.cs @@ -93,11 +93,16 @@ namespace MasterClient } else if (chkOPCUA.Checked) { + client = new Client.OpcUaMaster(); + nudPort.Value = 37800; + tbReadName.Text = "ns=12380;s=TAG1"; + tbWriteName.Text = "ns=12380;s=TAG1"; + Log("--- Run as OPCUA mode ---"); } else if (chkSdk.Checked) { - Log("--- Run as SDK mode ---"); + Log("--- On Develop ---"); } } diff --git a/ModbusStudy/CommClient/MasterClient.csproj b/ModbusStudy/CommClient/MasterClient.csproj index d56c769..dfcfc73 100644 --- a/ModbusStudy/CommClient/MasterClient.csproj +++ b/ModbusStudy/CommClient/MasterClient.csproj @@ -36,11 +36,51 @@ Graphicloads-100-Flat-2-Arrow-next.ico + + ..\packages\Portable.BouncyCastle.1.9.0\lib\net40\BouncyCastle.Crypto.dll + ..\packages\hima.lib.easymodbus.5.6.0\lib\EasyModbus.dll + + ..\packages\Microsoft.Extensions.Logging.Abstractions.3.1.27\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll + + + ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + + ..\packages\OPCFoundation.NetStandard.Opc.Ua.Client.1.4.370.12\lib\net462\Opc.Ua.Client.dll + + + ..\packages\OPCFoundation.NetStandard.Opc.Ua.Configuration.1.4.370.12\lib\net462\Opc.Ua.Configuration.dll + + + ..\packages\OPCFoundation.NetStandard.Opc.Ua.Core.1.4.370.12\lib\net462\Opc.Ua.Core.dll + + + ..\packages\OPCFoundation.NetStandard.Opc.Ua.Security.Certificates.1.4.370.12\lib\net462\Opc.Ua.Security.Certificates.dll + + + ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + + ..\packages\System.Formats.Asn1.6.0.0\lib\net461\System.Formats.Asn1.dll + + + ..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll + + + + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll + @@ -54,6 +94,7 @@ + Form diff --git a/ModbusStudy/CommClient/packages.config b/ModbusStudy/CommClient/packages.config index 144b2e9..6d2f737 100644 --- a/ModbusStudy/CommClient/packages.config +++ b/ModbusStudy/CommClient/packages.config @@ -1,4 +1,17 @@  + + + + + + + + + + + + + \ No newline at end of file