TAG 1 개 읽기 성공!

This commit is contained in:
2026-02-21 06:14:35 +09:00
parent 3181052619
commit 20ee22ae0c
62 changed files with 3685 additions and 170 deletions

View File

@@ -1,191 +1,145 @@
// cd ~/projects/OpcConnectionTest
// 기존 파일 삭제
// rm Program.cs
// # 새 파일 생성
//cat > Program.cs << 'EOF'
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
namespace OpcConnectionTest
{
public class StatusCodeInfo
{
public string Name { get; set; } = "";
public string Hex { get; set; } = "";
public ulong Decimal { get; set; }
public string Description { get; set; } = "";
}
class Program
{
static Dictionary<string, StatusCodeInfo> _statusCodeMap = new(StringComparer.OrdinalIgnoreCase);
static void LoadStatusCodes()
{
string path = Path.Combine(Directory.GetCurrentDirectory(), "statuscode.json");
if (File.Exists(path))
{
try {
var json = File.ReadAllText(path);
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var list = JsonSerializer.Deserialize<List<StatusCodeInfo>>(json, options);
if (list != null) {
foreach (var item in list) _statusCodeMap[item.Hex] = item;
Console.WriteLine($"✅ {_statusCodeMap.Count}개의 에러 코드 정의 로드 완료.");
}
} catch { }
}
}
static async Task Main(string[] args)
{
Console.WriteLine("=== Experion OPC UA Connection Test ===\n");
LoadStatusCodes();
// ⚠️ Experion 서버 IP 주소 수정
string primaryEndpoint = "opc.tcp://192.168.0.20:4840";
string secondaryEndpoint = "opc.tcp://192.168.1.20:4840";
// 1. 핵심 변수 (서버 이름 사칭 방지)
string serverHostName = "192.168.0.20"; //DESKTOP-8VS6SD2
string clientHostName = "dbsvr";
string endpointUrl = $"opc.tcp://{serverHostName}:4840";
string applicationUri = $"urn:{clientHostName}:OpcTestClient";
string pfxPath = Path.Combine(Directory.GetCurrentDirectory(), "pki/own/certs/OpcTestClient.pfx");
string pfxPassword = "";
string userName = "mngr";
string password = "mngr";
Console.WriteLine($"Primary: {primaryEndpoint}");
Console.WriteLine($"Secondary: {secondaryEndpoint}\n");
// 필수 폴더들 생성 (TrustedIssuer 포함)
Directory.CreateDirectory(Path.GetDirectoryName(pfxPath)!);
Directory.CreateDirectory("pki/trusted/certs");
Directory.CreateDirectory("pki/issuers/certs"); // ← 이 경로가 필요함
Directory.CreateDirectory("pki/rejected/certs");
X509Certificate2? clientCert = null;
if (!File.Exists(pfxPath))
{
var builder = CertificateFactory.CreateCertificate(
applicationUri, "OpcTestClient", $"CN=OpcTestClient, O=MyCompany",
new List<string> { "localhost", clientHostName, "192.168.0.5", "192.168.0.102" }
);
clientCert = builder.CreateForRSA();
File.WriteAllBytes(pfxPath, clientCert.Export(X509ContentType.Pfx, pfxPassword));
Console.WriteLine($"✨ 새 인증서 생성됨: {applicationUri}");
}
else
{
clientCert = new X509Certificate2(pfxPath, pfxPassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
}
var config = new ApplicationConfiguration
{
ApplicationName = "OpcTestClient",
ApplicationType = ApplicationType.Client,
ApplicationUri = applicationUri,
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier { Certificate = clientCert },
// ⚠️ 에러 발생했던 지점: 아래 3개 경로가 모두 명시되어야 함
TrustedPeerCertificates = new CertificateTrustList { StoreType = "Directory", StorePath = Path.GetFullPath("pki/trusted") },
TrustedIssuerCertificates = new CertificateTrustList { StoreType = "Directory", StorePath = Path.GetFullPath("pki/issuers") },
RejectedCertificateStore = new CertificateTrustList { StoreType = "Directory", StorePath = Path.GetFullPath("pki/rejected") },
AutoAcceptUntrustedCertificates = true,
AddAppCertToTrustedStore = true
},
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
};
await config.ValidateAsync(ApplicationType.Client);
config.CertificateValidator.CertificateValidation += (v, e) => { if (e.Error.StatusCode != StatusCodes.Good) e.Accept = true; };
ISession? session = null;
try
{
var config = new ApplicationConfiguration()
Console.WriteLine($"Step 1: Connecting to {endpointUrl}...");
var endpointConfig = EndpointConfiguration.Create(config);
using (var discovery = await DiscoveryClient.CreateAsync(config, new Uri(endpointUrl), DiagnosticsMasks.All, CancellationToken.None))
{
ApplicationName = "OPC Test",
ApplicationType = ApplicationType.Client,
ApplicationUri = "urn:OpcTest",
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier
{
StoreType = "Directory",
StorePath = "OPC Foundation/CertificateStores/MachineDefault"
},
AutoAcceptUntrustedCertificates = true,
RejectSHA1SignedCertificates = false
},
TransportConfigurations = new TransportConfigurationCollection(),
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
};
var endpoints = await discovery.GetEndpointsAsync(null);
var selected = endpoints.OrderByDescending(e => e.SecurityLevel)
.FirstOrDefault(e => e.SecurityPolicyUri.Contains("Basic256Sha256")) ?? endpoints[0];
Console.WriteLine($"🔍 정책 선택됨: {selected.SecurityPolicyUri}");
var endpoint = new ConfiguredEndpoint(null, selected, endpointConfig);
await config.ValidateAsync(ApplicationType.Client);
Console.WriteLine("Step 1: Discovering endpoints...");
var endpoints = await DiscoverAsync(config, primaryEndpoint);
if (endpoints.Count == 0)
{
Console.WriteLine("❌ No endpoints found!");
return;
Console.WriteLine("Step 2: Creating session...");
var identity = new UserIdentity(userName, Encoding.UTF8.GetBytes(password));
#pragma warning disable CS0618
session = await Session.Create(config, endpoint, false, "OpcTestSession", 60000, identity, null);
#pragma warning restore CS0618
}
Console.WriteLine($"✅ Found {endpoints.Count} endpoint(s)");
Console.WriteLine("\nStep 2: Connecting...");
session = await ConnectAsync(config, endpoints[0]);
if (session == null)
{
Console.WriteLine("❌ Connection failed!");
return;
}
Console.WriteLine($"✅ Connected! ID: {session.SessionId}");
Console.WriteLine("\nStep 3: Reading server info...");
await ReadInfoAsync(session);
Console.WriteLine("\nStep 4: Checking redundancy...");
await CheckRedundancyAsync(session);
Console.WriteLine("\n✅ Test completed!");
Console.WriteLine($"✅ Connected! SessionID: {session.SessionId}");
var result = await session.ReadValueAsync("ns=1;s=shinam:p-6102.hzset.fieldvalue");
Console.WriteLine($"🟢 TAG VALUE: {result.Value}");
}
catch (Exception ex)
{
Console.WriteLine($"\n❌ Error: {ex.Message}");
}
finally
{
if (session != null)
if (ex is ServiceResultException sre)
{
await session.CloseAsync();
session.Dispose();
string hexCode = $"0x{((uint)sre.StatusCode):X8}";
Console.WriteLine($"\n❌ OPC UA 서비스 오류: {hexCode}");
if (_statusCodeMap.TryGetValue(hexCode, out var info)) {
Console.WriteLine($"👉 에러명: {info.Name}");
Console.WriteLine($"👉 설 명: {info.Description}");
}
}
else Console.WriteLine($"❌ 오류: {ex.Message}");
}
Console.WriteLine("\nPress Enter to exit...");
finally { if (session != null) { await session.CloseAsync(); session.Dispose(); } }
Console.WriteLine("\n엔터를 누르면 종료됩니다...");
Console.ReadLine();
}
static async Task<EndpointDescriptionCollection> DiscoverAsync(
ApplicationConfiguration config, string url)
{
try
{
var epConfig = EndpointConfiguration.Create(config);
epConfig.OperationTimeout = 10000;
var client = await DiscoveryClient.CreateAsync(
new Uri(url), epConfig, config,
DiagnosticsMasks.None, CancellationToken.None);
var eps = await client.GetEndpointsAsync(null);
client.Dispose();
return eps;
}
catch
{
return new EndpointDescriptionCollection();
}
}
static async Task<ISession?> ConnectAsync(
ApplicationConfiguration config, EndpointDescription ep)
{
try
{
var epConfig = EndpointConfiguration.Create(config);
var configEp = new ConfiguredEndpoint(null, ep, epConfig);
var channel = SessionChannel.Create(
config,
configEp.Description,
configEp.Configuration,
X509CertificateValidator.TolerateAll,
null);
var sess = new Session(channel, config, configEp, null);
await sess.OpenAsync(
config.ApplicationName, 60000,
new UserIdentity(new AnonymousIdentityToken()),
null, CancellationToken.None);
return sess;
}
catch
{
return null;
}
}
static async Task ReadInfoAsync(ISession s)
{
try
{
var v1 = await s.ReadValueAsync(new NodeId(Variables.Server_ServerStatus_State));
Console.WriteLine($" State: {v1.Value}");
var v2 = await s.ReadValueAsync(new NodeId(Variables.Server_ServerStatus_CurrentTime));
Console.WriteLine($" Time: {v2.Value}");
}
catch (Exception ex)
{
Console.WriteLine($" Failed: {ex.Message}");
}
}
static async Task CheckRedundancyAsync(ISession s)
{
try
{
var v = await s.ReadValueAsync(new NodeId(Variables.Server_ServiceLevel));
byte level = Convert.ToByte(v.Value);
Console.WriteLine($" Service Level: {level}");
if (level >= 200)
Console.WriteLine(" ✅ PRIMARY server");
else if (level > 0)
Console.WriteLine(" ⚠️ SECONDARY server");
else
Console.WriteLine(" STANDALONE");
}
catch (Exception ex)
{
Console.WriteLine($" Failed: {ex.Message}");
}
}
}
}
}