태그1개읽어서 DB INSERT 5회 성공!

This commit is contained in:
2026-02-21 08:16:37 +09:00
parent 20ee22ae0c
commit 171aaf6115
28 changed files with 277 additions and 1540 deletions

View File

@@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql" Version="10.0.1" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua" Version="1.5.378.106" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Client" Version="1.5.378.106" />
</ItemGroup>

View File

@@ -0,0 +1,145 @@
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;
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)
{
LoadStatusCodes();
// 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";
// 필수 폴더들 생성 (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
{
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))
{
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);
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($"✅ 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)
{
if (ex is ServiceResultException sre)
{
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}");
}
finally { if (session != null) { await session.CloseAsync(); session.Dispose(); } }
Console.WriteLine("\n엔터를 누르면 종료됩니다...");
Console.ReadLine();
}
}
}

View File

@@ -1,358 +0,0 @@
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
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
{
[Obsolete]
static Dictionary<uint, StatusCodeInfo> _statusCodeMap = new();
static void LoadStatusCodes()
{
try
{
string path = Path.Combine(Directory.GetCurrentDirectory(), "statuscode.json");
if (!File.Exists(path))
{
Console.WriteLine("⚠️ statuscode.json 파일을 찾을 수 없습니다.");
return;
}
var json = File.ReadAllText(path);
var list = JsonSerializer.Deserialize<List<StatusCodeInfo>>(json);
if (list != null)
{
foreach (var item in list)
{
_statusCodeMap[(uint)item.Decimal] = item;
}
}
Console.WriteLine($"✅ StatusCode { _statusCodeMap.Count }개 로드 완료\n");
}
catch (Exception ex)
{
Console.WriteLine($"❌ statuscode.json 로드 실패: {ex.Message}");
}
}
static async Task Main(string[] args)
{
Console.WriteLine("=== Experion OPC UA Connection Test ===\n");
LoadStatusCodes();
// ⚠️ Experion 서버 IP 주소 수정
string endpoint = "opc.tcp://192.168.0.20:4840";
// ⚠️ Honeywell CA로 서명된 클라이언트 인증서 경로 및 비밀번호
string pfxPath = Path.Combine(Directory.GetCurrentDirectory(), "pki/own/certs/OpcTestClient.pfx");
string pfxPassword = ""; // ⚠️ PFX 비밀번호 (없으면 빈 문자열)
// ⚠️ Experion Windows 계정 정보
string userName = "mngr"; // 예: "DOMAIN\\user" 또는 "localuser"
string password = "mngr";
Console.WriteLine($"Server: {endpoint}");
Console.WriteLine($"User: {userName}\n");
// PFX 파일 존재 여부 확인
if (!File.Exists(pfxPath))
{
Console.WriteLine($"❌ PFX 파일을 찾을 수 없습니다: {pfxPath}");
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
return;
}
// Honeywell CA 서명 인증서 로드
// Exportable: OPC UA 채널 서명 시 개인키 접근 필요
X509Certificate2 clientCertificate = new X509Certificate2(
pfxPath,
pfxPassword,
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
Console.WriteLine($"✅ 인증서 로드: {clientCertificate.Subject}");
Console.WriteLine($" 유효기간: {clientCertificate.NotBefore:yyyy-MM-dd} ~ {clientCertificate.NotAfter:yyyy-MM-dd}");
Console.WriteLine($" 개인키 포함: {clientCertificate.HasPrivateKey}\n");
if (!clientCertificate.HasPrivateKey)
{
Console.WriteLine("❌ 개인키가 없습니다. PFX 파일을 확인하세요.");
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
return;
}
ISession? session = null;
try
{
// 1. Configuration 생성
var config = new ApplicationConfiguration()
{
ApplicationName = "OPC Test Client",
ApplicationType = ApplicationType.Client,
ApplicationUri = "urn:OpcTestClient",
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier
{
StoreType = "Directory",
StorePath = Path.GetFullPath("pki/own"),
SubjectName = clientCertificate.Subject,
// Honeywell CA 서명 인증서를 직접 주입
Certificate = clientCertificate
},
TrustedPeerCertificates = new CertificateTrustList
{
StoreType = "Directory",
StorePath = Path.GetFullPath("pki/trusted")
},
TrustedIssuerCertificates = new CertificateTrustList
{
StoreType = "Directory",
// ⚠️ Honeywell 내부 CA 인증서를 이 폴더에 배치해야 합니다
// Experion Station에서 CA 인증서를 내보내어 pki/issuers/certs/ 에 복사
StorePath = Path.GetFullPath("pki/issuers")
},
RejectedCertificateStore = new CertificateTrustList
{
StoreType = "Directory",
StorePath = Path.GetFullPath("pki/rejected")
},
// Honeywell CA 서명 인증서이므로 자동 수락 불필요 — false 권장
// 테스트 중 서버 인증서 검증 실패 시 true로 임시 변경 가능
AutoAcceptUntrustedCertificates = false,
RejectSHA1SignedCertificates = false,
AddAppCertToTrustedStore = false
},
TransportConfigurations = new TransportConfigurationCollection(),
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
};
// 서버 인증서 검증 실패 시 로그만 출력하는 핸들러
// Honeywell CA를 pki/issuers/에 제대로 넣으면 이 핸들러까지 오지 않아야 정상
// 불가피한 경우 e.Accept = true; 주석 해제 (운영 환경에서는 사용 금지)
config.CertificateValidator.CertificateValidation += (validator, e) =>
{
Console.WriteLine($" ⚠️ 서버 인증서 검증 이슈: {e.Error.StatusCode} — {e.Certificate.Subject}");
// e.Accept = true;
};
await config.ValidateAsync(ApplicationType.Client);
Console.WriteLine("Step 1: Discovering endpoints...");
// 2. Endpoint 검색
var endpointConfig = EndpointConfiguration.Create(config);
endpointConfig.OperationTimeout = 10000;
var discoveryClient = await DiscoveryClient.CreateAsync(
new Uri(endpoint),
endpointConfig,
config,
DiagnosticsMasks.None,
CancellationToken.None);
var endpoints = await discoveryClient.GetEndpointsAsync(null);
discoveryClient.Dispose();
if (endpoints == null || endpoints.Count == 0)
{
Console.WriteLine("❌ No endpoints found!");
return;
}
Console.WriteLine($"✅ Found {endpoints.Count} endpoint(s)");
foreach (var ep in endpoints)
{
Console.WriteLine($" - {ep.EndpointUrl} | Security: {ep.SecurityMode} | Policy: {ep.SecurityPolicyUri?.Split('#').Last()}");
}
// SignAndEncrypt 엔드포인트 우선 선택, 없으면 Sign, 없으면 첫 번째
var selectedEndpoint =
endpoints.FirstOrDefault(e => e.SecurityMode == MessageSecurityMode.SignAndEncrypt)
?? endpoints.FirstOrDefault(e => e.SecurityMode == MessageSecurityMode.Sign)
?? endpoints[0];
Console.WriteLine($"\n선택된 엔드포인트: {selectedEndpoint.EndpointUrl} ({selectedEndpoint.SecurityMode})");
Console.WriteLine("\nStep 2: Creating session...");
// 3. ConfiguredEndpoint 생성
var configuredEndpoint = new ConfiguredEndpoint(
null,
selectedEndpoint,
EndpointConfiguration.Create(config));
// 4. Session 생성 — UserIdentity에 Windows 계정 사용
// Experion R530은 Windows 계정을 OPC UA UserNameIdentityToken으로 인증
var userIdentity = new UserIdentity(new UserNameIdentityToken
{
UserName = userName,
DecryptedPassword = System.Text.Encoding.UTF8.GetBytes(password)
});
session = await Session.Create(
config,
configuredEndpoint,
false,
"OPC Test Session",
60000,
userIdentity,
null);
Console.WriteLine($"✅ Connected!");
Console.WriteLine($" Session ID: {session.SessionId}");
Console.WriteLine("\nStep 3: Reading server info...");
await ReadServerInfoAsync(session);
Console.WriteLine("\nStep 4: Checking redundancy...");
await CheckRedundancyAsync(session);
Console.WriteLine("\nStep 5: Browsing nodes...");
await BrowseNodesAsync(session);
Console.WriteLine("\n✅ All tests completed!");
}
catch (ServiceResultException sre)
{
uint code = (uint)sre.StatusCode;
// Console.WriteLine($"\n❌ OPC UA 오류: {sre.Message}");
Console.WriteLine($" StatusCode: 0x{code:X8} ({code})");
if (_statusCodeMap.TryGetValue(code, out var info))
{
Console.WriteLine($" Name: {info.Name}");
Console.WriteLine($" Description: {info.Description}");
}
else
{
Console.WriteLine(" ⚠️ statuscode.json에 정의되지 않은 코드입니다.");
}
}
finally
{
if (session != null)
{
try
{
Console.WriteLine("\nClosing session...");
await session.CloseAsync();
session.Dispose();
Console.WriteLine("✅ Session closed");
}
catch { }
}
}
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
}
static async Task ReadServerInfoAsync(ISession session)
{
try
{
var state = await session.ReadValueAsync(
new NodeId(Variables.Server_ServerStatus_State));
Console.WriteLine($" State: {state.Value}");
var time = await session.ReadValueAsync(
new NodeId(Variables.Server_ServerStatus_CurrentTime));
Console.WriteLine($" Time: {time.Value}");
}
catch (Exception ex)
{
Console.WriteLine($" Error: {ex.Message}");
}
}
static async Task CheckRedundancyAsync(ISession session)
{
try
{
var serviceLevel = await session.ReadValueAsync(
new NodeId(Variables.Server_ServiceLevel));
byte level = Convert.ToByte(serviceLevel.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($" Error: {ex.Message}");
}
}
static async Task BrowseNodesAsync(ISession session)
{
try
{
var browser = new Browser(session)
{
BrowseDirection = BrowseDirection.Forward,
ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences,
IncludeSubtypes = true,
NodeClassMask = (int)(NodeClass.Object | NodeClass.Variable)
};
var references = await browser.BrowseAsync(ObjectIds.ObjectsFolder);
Console.WriteLine($" Found {references.Count} top-level nodes:");
int count = 0;
foreach (var r in references)
{
Console.WriteLine($" {r.DisplayName} ({r.NodeClass})");
if (++count >= 5)
{
Console.WriteLine($" ... and {references.Count - 5} more");
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine($" Error: {ex.Message}");
}
}
}
}

View File

@@ -1,253 +0,0 @@
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
using System.Security.Cryptography.X509Certificates;
namespace OpcConnectionTest
{
class Program
{
[Obsolete]
static async Task Main(string[] args)
{
Console.WriteLine("=== Experion OPC UA Connection Test ===\n");
// ⚠️ Experion 서버 IP 주소 수정
string endpoint = "opc.tcp://192.168.0.20:4840";
string pfxPath = Path.Combine(Directory.GetCurrentDirectory(), "pki/own/certs/OpcTestClient.pfx");
X509Certificate2 clientCertificate = new X509Certificate2(pfxPath, "");
Console.WriteLine($"Server: {endpoint}\n");
ISession? session = null;
try
{
// 1. Configuration 생성
var config = new ApplicationConfiguration()
{
ApplicationName = "OPC Test Client",
ApplicationType = ApplicationType.Client,
ApplicationUri = "urn:OpcTestClient",
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier
{
StoreType = "Directory",
StorePath = Path.GetFullPath("pki/own"),
SubjectName = "CN=OpcTestClient"
},
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,
RejectSHA1SignedCertificates = false,
AddAppCertToTrustedStore = true
},
TransportConfigurations = new TransportConfigurationCollection(),
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
};
await config.ValidateAsync(ApplicationType.Client);
Console.WriteLine("Step 1: Discovering endpoints...");
// 2. Endpoint 검색
var endpointConfig = EndpointConfiguration.Create(config);
endpointConfig.OperationTimeout = 10000;
var discoveryClient = await DiscoveryClient.CreateAsync(
new Uri(endpoint),
endpointConfig,
config,
DiagnosticsMasks.None,
CancellationToken.None);
var endpoints = await discoveryClient.GetEndpointsAsync(null);
discoveryClient.Dispose();
if (endpoints == null || endpoints.Count == 0)
{
Console.WriteLine("❌ No endpoints found!");
return;
}
Console.WriteLine($"✅ Found {endpoints.Count} endpoint(s)");
foreach (var ep in endpoints)
{
Console.WriteLine($" - {ep.EndpointUrl} ({ep.SecurityMode})");
}
Console.WriteLine("\nStep 2: Creating session...");
// 3. ConfiguredEndpoint 생성
var selectedEndpoint = endpoints[0];
var configuredEndpoint = new ConfiguredEndpoint(
null,
selectedEndpoint,
EndpointConfiguration.Create(config));
// Session.Create 호출 직전에 추가
var cert = await config.SecurityConfiguration.ApplicationCertificate.Find(true);
if (cert == null)
{
Console.WriteLine("❌ [Internal Error] 코드가 인증서 파일을 찾지 못했습니다!");
Console.WriteLine($"확인 경로: {Path.GetFullPath(config.SecurityConfiguration.ApplicationCertificate.StorePath)}");
return;
}
else
{
Console.WriteLine($"✅ 인증서 로드 성공: {cert.Subject}");
}
// 4. Session 생성 (최신 API)
session = await Session.Create(
config,
configuredEndpoint,
false,
"OPC Test Session",
60000,
new UserIdentity(new AnonymousIdentityToken()),
null);
Console.WriteLine($"✅ Connected!");
Console.WriteLine($" Session ID: {session.SessionId}");
Console.WriteLine("\nStep 3: Reading server info...");
await ReadServerInfoAsync(session);
Console.WriteLine("\nStep 4: Checking redundancy...");
await CheckRedundancyAsync(session);
Console.WriteLine("\nStep 5: Browsing nodes...");
await BrowseNodesAsync(session);
Console.WriteLine("\n✅ All tests completed!");
}
catch (Exception ex)
{
Console.WriteLine($"\n❌ Error: {ex.Message}");
Console.WriteLine($"Type: {ex.GetType().Name}");
if (ex.InnerException != null)
{
Console.WriteLine($"Inner: {ex.InnerException.Message}");
}
}
finally
{
if (session != null)
{
try
{
Console.WriteLine("\nClosing session...");
await session.CloseAsync();
session.Dispose();
Console.WriteLine("✅ Session closed");
}
catch { }
}
}
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
}
static async Task ReadServerInfoAsync(ISession session)
{
try
{
// Server State
var state = await session.ReadValueAsync(
new NodeId(Variables.Server_ServerStatus_State));
Console.WriteLine($" State: {state.Value}");
// Server Time
var time = await session.ReadValueAsync(
new NodeId(Variables.Server_ServerStatus_CurrentTime));
Console.WriteLine($" Time: {time.Value}");
}
catch (Exception ex)
{
Console.WriteLine($" Error: {ex.Message}");
}
}
static async Task CheckRedundancyAsync(ISession session)
{
try
{
var serviceLevel = await session.ReadValueAsync(
new NodeId(Variables.Server_ServiceLevel));
byte level = Convert.ToByte(serviceLevel.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($" Error: {ex.Message}");
}
}
static async Task BrowseNodesAsync(ISession session)
{
try
{
var browser = new Browser(session)
{
BrowseDirection = BrowseDirection.Forward,
ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences,
IncludeSubtypes = true,
NodeClassMask = (int)(NodeClass.Object | NodeClass.Variable)
};
var references = await browser.BrowseAsync(ObjectIds.ObjectsFolder);
Console.WriteLine($" Found {references.Count} top-level nodes:");
int count = 0;
foreach (var r in references)
{
Console.WriteLine($" {r.DisplayName} ({r.NodeClass})");
if (++count >= 5)
{
Console.WriteLine($" ... and {references.Count - 5} more");
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine($" Error: {ex.Message}");
}
}
}
}

View File

@@ -1,212 +0,0 @@
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Text;
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<uint, StatusCodeInfo> _statusCodeMap = new();
// statuscode.json 로드 로직
static void LoadStatusCodes()
{
string path = Path.Combine(Directory.GetCurrentDirectory(), "statuscode.json");
if (!File.Exists(path))
{
Console.WriteLine("⚠️ statuscode.json 파일을 찾을 수 없습니다. (에러 분석 기능 제한)");
return;
}
try
{
var json = File.ReadAllText(path);
var list = JsonSerializer.Deserialize<List<StatusCodeInfo>>(json);
if (list != null)
foreach (var item in list)
_statusCodeMap[(uint)item.Decimal] = item;
Console.WriteLine($"✅ StatusCode {_statusCodeMap.Count}개 로드 완료\n");
}
catch (Exception ex)
{
Console.WriteLine($"❌ statuscode.json 로드 실패: {ex.Message}");
}
}
static async Task Main(string[] args)
{
LoadStatusCodes();
// 1. 설정 정보 (UaExpert 정보 기반)
string endpointUrl = "opc.tcp://DESKTOP-8VS6SD2:4840";
string pfxPath = Path.Combine(Directory.GetCurrentDirectory(), "pki/own/certs/OpcTestClient.pfx");
string pfxPassword = ""; // 저장된 정보 기반 공백 유지
string userName = "mngr";
string password = "mngr";
if (!File.Exists(pfxPath))
{
Console.WriteLine($"❌ PFX 파일 없음: {pfxPath}");
return;
}
// 2. 인증서 로드 및 애플리케이션 설정
var clientCert = new X509Certificate2(
pfxPath,
pfxPassword,
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet
);
var config = new ApplicationConfiguration
{
ApplicationName = "OpcTestClient",
ApplicationType = ApplicationType.Client,
ApplicationUri = "urn:OpcTestClient",
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier { Certificate = clientCert },
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 }
};
config.CertificateValidator.CertificateValidation += (validator, e) =>
{
if (e.Error != null && e.Error.StatusCode != StatusCodes.Good)
{
Console.WriteLine($"인증서 보안 경고 무시(Ignore): {e.Error.StatusCode}");
e.Accept = true;
}
};
await config.ValidateAsync(ApplicationType.Client);
ISession? session = null;
try
{
// 3. 엔드포인트 탐색 및 선택 (UaExpert 보안 정책 반영)
Console.WriteLine("Step 1: Discovering endpoints...");
using (var discovery = DiscoveryClient.Create(new Uri(endpointUrl), EndpointConfiguration.Create(config)))
{
var endpoints = discovery.GetEndpoints(null);
Console.WriteLine($"✅ {endpoints.Count} 엔드포인트 발견");
// 가장 높은 보안 수준을 가진 엔드포인트 중 Aes256_Sha256_RsaPss 우선 선택
var selected = endpoints.OrderByDescending(e => e.SecurityLevel)
.FirstOrDefault(e => e.SecurityPolicyUri.Contains("Basic256Sha256"))
?? endpoints.OrderByDescending(e => e.SecurityLevel).First();
Console.WriteLine($"👉 선택된 엔드포인트: {selected.SecurityMode} | {selected.SecurityPolicyUri.Split('#').Last()}");
var configured = new ConfiguredEndpoint(null, selected, EndpointConfiguration.Create(config));
// 4. 세션 생성 (mngr 계정)
Console.WriteLine("\nStep 2: Creating session with user identity...");
// 패스워드를 byte[]로 요구하는 최신 SDK 방식 적용
var identity = new UserIdentity(userName, Encoding.UTF8.GetBytes(password));
session = await Session.Create(
config,
configured,
false, // updateBeforeConnect
"OPC Test Session",
60000,
identity,
null
);
}
Console.WriteLine($"✅ Connected! SessionID: {session.SessionId}");
// 5. 서버 상태 확인
await ReadServerInfoAsync(session);
// 6. UaExpert에서 확인한 하니웰 실제 태그 데이터 읽기
await ReadHoneywellTagAsync(session);
}
catch (ServiceResultException sre)
{
uint code = (uint)sre.StatusCode;
Console.WriteLine($"\n❌ OPC UA 오류: {sre.Message}");
Console.WriteLine($" StatusCode: 0x{code:X8}");
if (_statusCodeMap.TryGetValue(code, out var info))
Console.WriteLine($" Name: {info.Name}\n Description: {info.Description}");
}
catch (Exception ex)
{
Console.WriteLine($"\n❌ 일반 오류 발생: {ex.Message}");
}
finally
{
if (session != null)
{
await session.CloseAsync();
session.Dispose();
Console.WriteLine("\n✅ Session closed");
}
}
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
}
// 서버 기본 정보 읽기
static async Task ReadServerInfoAsync(ISession session)
{
var state = await session.ReadValueAsync(Variables.Server_ServerStatus_State);
var time = await session.ReadValueAsync(Variables.Server_ServerStatus_CurrentTime);
Console.WriteLine($"\n[Server Status]\n - State: {state.Value}\n - Time: {time.Value}");
}
// 🎯 하니웰 특정 태그 읽기 (UaExpert 데이터 기반)
static async Task ReadHoneywellTagAsync(ISession session)
{
string nodeId = "ns=1;s=shinam:p-6102.hzset.fieldvalue";
Console.WriteLine($"\nStep 3: Reading Honeywell Tag [{nodeId}]...");
try
{
var result = await session.ReadValueAsync(nodeId);
if (StatusCode.IsGood(result.StatusCode))
{
Console.WriteLine("========================================");
Console.WriteLine($"🟢 TAG VALUE : {result.Value}");
Console.WriteLine($"🟡 DATATYPE : {result.WrappedValue.TypeInfo.BuiltInType}");
Console.WriteLine($"🔵 TIMESTAMP : {result.SourceTimestamp.ToLocalTime()}");
Console.WriteLine($"⚪️ STATUS : {result.StatusCode}");
Console.WriteLine("========================================");
}
else
{
Console.WriteLine($"❌ 읽기 실패: {result.StatusCode}");
}
}
catch (Exception ex)
{
Console.WriteLine($"❌ 태그 읽기 중 예외 발생: {ex.Message}");
}
}
}
}

View File

@@ -9,6 +9,7 @@ using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using Opc.Ua;
using Opc.Ua.Client;
using Npgsql; // 👈 PostgreSQL 라이브러리 추가
namespace OpcConnectionTest
{
@@ -24,6 +25,9 @@ namespace OpcConnectionTest
{
static Dictionary<string, StatusCodeInfo> _statusCodeMap = new(StringComparer.OrdinalIgnoreCase);
// 1. DB 연결 문자열 (비밀번호를 본인 설정에 맞게 수정하세요)
static string dbConnString = "Host=localhost;Username=postgres;Password=postgres;Database=opcdb";
static void LoadStatusCodes()
{
string path = Path.Combine(Directory.GetCurrentDirectory(), "statuscode.json");
@@ -41,52 +45,53 @@ namespace OpcConnectionTest
}
}
// 2. DB 저장 함수
static async Task SaveToDatabase(string tagName, double val, string status)
{
try {
using var conn = new NpgsqlConnection(dbConnString);
await conn.OpenAsync();
string sql = "INSERT INTO opc_history (tag_name, tag_value, status_code) VALUES (@tag, @val, @status)";
using var cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue("tag", tagName);
cmd.Parameters.AddWithValue("val", val);
cmd.Parameters.AddWithValue("status", status);
await cmd.ExecuteNonQueryAsync();
Console.WriteLine("💾 DB 저장 완료.");
}
catch (Exception ex) {
Console.WriteLine($"❌ DB 저장 실패: {ex.Message}");
}
}
static async Task Main(string[] args)
{
LoadStatusCodes();
// 1. 핵심 변수 (서버 이름 사칭 방지)
string serverHostName = "192.168.0.20"; //DESKTOP-8VS6SD2
string serverHostName = "192.168.0.20";
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";
// 필수 폴더들 생성 (TrustedIssuer 포함)
Directory.CreateDirectory(Path.GetDirectoryName(pfxPath)!);
Directory.CreateDirectory("pki/trusted/certs");
Directory.CreateDirectory("pki/issuers/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);
}
X509Certificate2? clientCert = new X509Certificate2(pfxPath, pfxPassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
var config = new ApplicationConfiguration
{
var config = new ApplicationConfiguration {
ApplicationName = "OpcTestClient",
ApplicationType = ApplicationType.Client,
ApplicationUri = applicationUri,
SecurityConfiguration = new SecurityConfiguration
{
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") },
@@ -105,40 +110,39 @@ namespace OpcConnectionTest
{
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))
{
using var discovery = await DiscoveryClient.CreateAsync(config, new Uri(endpointUrl), DiagnosticsMasks.All, CancellationToken.None);
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);
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($"✅ Connected! SessionID: {session.SessionId}");
var result = await session.ReadValueAsync("ns=1;s=shinam:p-6102.hzset.fieldvalue");
Console.WriteLine($"🟢 TAG VALUE: {result.Value}");
// 3. 데이터 읽기 및 DB 저장 루프 (테스트용으로 5회 반복)
string nodeID = "ns=1;s=shinam:p-6102.hzset.fieldvalue";
for (int i = 0; i < 5; i++)
{
var result = await session.ReadValueAsync(nodeID);
double val = Convert.ToDouble(result.Value);
string status = result.StatusCode.ToString();
Console.WriteLine($"[{i+1}] TAG: {val} (Status: {status})");
// DB에 저장
await SaveToDatabase("p-6102", val, status);
await Task.Delay(2000); // 2초 대기
}
}
catch (Exception ex)
{
if (ex is ServiceResultException sre)
{
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($"❌ 오류 발생: {ex.Message}");
}
finally { if (session != null) { await session.CloseAsync(); session.Dispose(); } }
Console.WriteLine("\n엔터를 누르면 종료됩니다...");
Console.WriteLine("\n작업 완료. 엔터를 누르세요...");
Console.ReadLine();
}
}

View File

@@ -1,187 +0,0 @@
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Text;
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<uint, StatusCodeInfo> _statusCodeMap = new();
static void LoadStatusCodes()
{
string path = Path.Combine(Directory.GetCurrentDirectory(), "statuscode.json");
if (!File.Exists(path))
{
Console.WriteLine("⚠️ statuscode.json 파일을 찾을 수 없습니다.");
return;
}
try
{
var json = File.ReadAllText(path);
var list = JsonSerializer.Deserialize<List<StatusCodeInfo>>(json);
if (list != null)
foreach (var item in list)
_statusCodeMap[(uint)item.Decimal] = item;
Console.WriteLine($"✅ StatusCode {_statusCodeMap.Count}개 로드 완료\n");
}
catch (Exception ex)
{
Console.WriteLine($"❌ statuscode.json 로드 실패: {ex.Message}");
}
}
static async Task Main(string[] args)
{
LoadStatusCodes();
string endpointUrl = "opc.tcp://192.168.0.20:4840";
string pfxPath = Path.Combine(Directory.GetCurrentDirectory(), "pki/own/certs/OpcTestClient.pfx");
string pfxPassword = "";
string userName = "mngr";
string password = "mngr";
if (!File.Exists(pfxPath))
{
Console.WriteLine($"❌ PFX 파일 없음: {pfxPath}");
return;
}
var clientCert = new X509Certificate2(
pfxPath,
pfxPassword,
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet
);
if (!clientCert.HasPrivateKey)
{
Console.WriteLine("❌ 인증서에 개인키 없음");
return;
}
var config = new ApplicationConfiguration
{
ApplicationName = "OPC Test Client",
ApplicationType = ApplicationType.Client,
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier
{
StoreType = "Directory",
StorePath = Path.GetFullPath("pki/own"),
Certificate = clientCert
},
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
},
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
};
config.CertificateValidator.CertificateValidation += (s, e) =>
{
Console.WriteLine($"⚠️ 서버 인증서 검증 실패: {e.Error.StatusCode} - {e.Certificate.Subject}");
};
await config.ValidateAsync(ApplicationType.Client);
ISession? session = null;
try
{
Console.WriteLine("Step 1: Discovering endpoints...");
var discovery = DiscoveryClient.Create(new Uri(endpointUrl));
var endpoints = discovery.GetEndpoints(null);
Console.WriteLine($"✅ {endpoints.Count} 엔드포인트 발견");
foreach (var ep in endpoints)
Console.WriteLine($" - {ep.EndpointUrl} | {ep.SecurityMode} | {ep.SecurityPolicyUri?.Split('#').Last()}");
// AES256/SHA256 우선 선택
var selected = endpoints.FirstOrDefault(ep =>
ep.SecurityMode == MessageSecurityMode.SignAndEncrypt &&
ep.SecurityPolicyUri.EndsWith("Basic256Sha256"))
?? endpoints.First();
Console.WriteLine($"\n선택된 엔드포인트: {selected.EndpointUrl} ({selected.SecurityMode})");
var configured = new ConfiguredEndpoint(null, selected, EndpointConfiguration.Create(config));
// ✅ Session.CreateAsync 사용
session = await Session.Create(
config,
configured,
false,
"OPC Test Session",
60000,
new UserIdentity(userName, Encoding.UTF8.GetBytes(password)),
null
);
Console.WriteLine($"✅ Connected! SessionID: {session.SessionId}");
await ReadServerInfoAsync(session);
}
catch (ServiceResultException sre)
{
uint code = (uint)sre.StatusCode;
Console.WriteLine($"❌ OPC UA 오류: {sre.Message}");
Console.WriteLine($" StatusCode: 0x{code:X8}");
if (_statusCodeMap.TryGetValue(code, out var info))
Console.WriteLine($" Name: {info.Name}\n Description: {info.Description}");
else
Console.WriteLine(" ⚠️ statuscode.json에 정의되지 않은 코드입니다.");
}
finally
{
if (session != null)
{
await session.CloseAsync();
session.Dispose();
Console.WriteLine("✅ Session closed");
}
}
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
}
static async Task ReadServerInfoAsync(ISession session)
{
var state = await session.ReadValueAsync(Variables.Server_ServerStatus_State);
var time = await session.ReadValueAsync(Variables.Server_ServerStatus_CurrentTime);
Console.WriteLine($"Server State: {state.Value}");
Console.WriteLine($"Current Time: {time.Value}");
}
}
}

View File

@@ -1,274 +0,0 @@
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
using System.Security.Cryptography.X509Certificates;
namespace OpcConnectionTest
{
class Program
{
[Obsolete]
static async Task Main(string[] args)
{
Console.WriteLine("=== Experion OPC UA Connection Test ===\n");
// ⚠️ Experion 서버 IP 주소 수정
string endpoint = "opc.tcp://192.168.0.20:4840";
string pfxPath = Path.Combine(Directory.GetCurrentDirectory(), "pki/own/certs/OpcTestClient.pfx");
// PFX 파일 존재 여부 먼저 확인
if (!File.Exists(pfxPath))
{
Console.WriteLine($"❌ PFX 파일을 찾을 수 없습니다: {pfxPath}");
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
return;
}
// PFX를 직접 로드 (Exportable 플래그 필수 — OPC UA 서명에 개인키 접근 필요)
X509Certificate2 clientCertificate = new X509Certificate2(
pfxPath,
"",
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
Console.WriteLine($"Server: {endpoint}");
Console.WriteLine($"Certificate: {clientCertificate.Subject}\n");
ISession? session = null;
try
{
// 1. Configuration 생성
var config = new ApplicationConfiguration()
{
ApplicationName = "OPC Test Client",
ApplicationType = ApplicationType.Client,
ApplicationUri = "urn:OpcTestClient",
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier
{
StoreType = "Directory",
StorePath = Path.GetFullPath("pki/own"),
SubjectName = "CN=OpcTestClient",
// PFX를 직접 주입 — Directory Store 탐색 없이 이 인증서를 사용
Certificate = clientCertificate
},
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,
RejectSHA1SignedCertificates = false,
AddAppCertToTrustedStore = true
},
TransportConfigurations = new TransportConfigurationCollection(),
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
};
await config.ValidateAsync(ApplicationType.Client);
// 인증서 로드 확인 (Find 대신 직접 참조)
var cert = config.SecurityConfiguration.ApplicationCertificate.Certificate;
if (cert == null)
{
Console.WriteLine("❌ [Internal Error] 인증서 주입에 실패했습니다!");
return;
}
Console.WriteLine($"✅ 인증서 로드 성공: {cert.Subject}");
Console.WriteLine($" 유효기간: {cert.NotBefore:yyyy-MM-dd} ~ {cert.NotAfter:yyyy-MM-dd}");
Console.WriteLine($" 개인키 포함: {cert.HasPrivateKey}");
if (!cert.HasPrivateKey)
{
Console.WriteLine("❌ 개인키가 없습니다. PFX 파일 또는 KeyStorageFlags를 확인하세요.");
return;
}
Console.WriteLine("\nStep 1: Discovering endpoints...");
// 2. Endpoint 검색
var endpointConfig = EndpointConfiguration.Create(config);
endpointConfig.OperationTimeout = 10000;
var discoveryClient = await DiscoveryClient.CreateAsync(
new Uri(endpoint),
endpointConfig,
config,
DiagnosticsMasks.None,
CancellationToken.None);
var endpoints = await discoveryClient.GetEndpointsAsync(null);
discoveryClient.Dispose();
if (endpoints == null || endpoints.Count == 0)
{
Console.WriteLine("❌ No endpoints found!");
return;
}
Console.WriteLine($"✅ Found {endpoints.Count} endpoint(s)");
foreach (var ep in endpoints)
{
Console.WriteLine($" - {ep.EndpointUrl} ({ep.SecurityMode})");
}
Console.WriteLine("\nStep 2: Creating session...");
// 3. ConfiguredEndpoint 생성
var selectedEndpoint = endpoints[0];
var configuredEndpoint = new ConfiguredEndpoint(
null,
selectedEndpoint,
EndpointConfiguration.Create(config));
// 4. Session 생성
session = await Session.Create(
config,
configuredEndpoint,
false,
"OPC Test Session",
60000,
new UserIdentity(new AnonymousIdentityToken()),
null);
Console.WriteLine($"✅ Connected!");
Console.WriteLine($" Session ID: {session.SessionId}");
Console.WriteLine("\nStep 3: Reading server info...");
await ReadServerInfoAsync(session);
Console.WriteLine("\nStep 4: Checking redundancy...");
await CheckRedundancyAsync(session);
Console.WriteLine("\nStep 5: Browsing nodes...");
await BrowseNodesAsync(session);
Console.WriteLine("\n✅ All tests completed!");
}
catch (Exception ex)
{
Console.WriteLine($"\n❌ Error: {ex.Message}");
Console.WriteLine($"Type: {ex.GetType().Name}");
if (ex.InnerException != null)
{
Console.WriteLine($"Inner: {ex.InnerException.Message}");
}
}
finally
{
if (session != null)
{
try
{
Console.WriteLine("\nClosing session...");
await session.CloseAsync();
session.Dispose();
Console.WriteLine("✅ Session closed");
}
catch { }
}
}
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
}
static async Task ReadServerInfoAsync(ISession session)
{
try
{
// Server State
var state = await session.ReadValueAsync(
new NodeId(Variables.Server_ServerStatus_State));
Console.WriteLine($" State: {state.Value}");
// Server Time
var time = await session.ReadValueAsync(
new NodeId(Variables.Server_ServerStatus_CurrentTime));
Console.WriteLine($" Time: {time.Value}");
}
catch (Exception ex)
{
Console.WriteLine($" Error: {ex.Message}");
}
}
static async Task CheckRedundancyAsync(ISession session)
{
try
{
var serviceLevel = await session.ReadValueAsync(
new NodeId(Variables.Server_ServiceLevel));
byte level = Convert.ToByte(serviceLevel.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($" Error: {ex.Message}");
}
}
static async Task BrowseNodesAsync(ISession session)
{
try
{
var browser = new Browser(session)
{
BrowseDirection = BrowseDirection.Forward,
ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences,
IncludeSubtypes = true,
NodeClassMask = (int)(NodeClass.Object | NodeClass.Variable)
};
var references = await browser.BrowseAsync(ObjectIds.ObjectsFolder);
Console.WriteLine($" Found {references.Count} top-level nodes:");
int count = 0;
foreach (var r in references)
{
Console.WriteLine($" {r.DisplayName} ({r.NodeClass})");
if (++count >= 5)
{
Console.WriteLine($" ... and {references.Count - 5} more");
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine($" Error: {ex.Message}");
}
}
}
}

View File

@@ -1,196 +0,0 @@
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
using System.Security.Cryptography.X509Certificates;
namespace OpcConnectionTest
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("=== Experion OPC UA Connection Test ===\n");
// 1. 기본 경로 및 설정 정보
string serverUrl = "opc.tcp://192.168.0.20:4840";
string basePkiPath = Path.GetFullPath("pki");
string pfxPath = Path.Combine(basePkiPath, "own/certs/OpcTestClient.pfx");
// 필수 폴더 자동 생성 (Issuer/Rejected 저장소 에러 방지)
Directory.CreateDirectory(Path.Combine(basePkiPath, "issuers/certs"));
Directory.CreateDirectory(Path.Combine(basePkiPath, "rejected/certs"));
Directory.CreateDirectory(Path.Combine(basePkiPath, "trusted/certs"));
Console.WriteLine($"Server: {serverUrl}");
Console.WriteLine($"PKI Root: {basePkiPath}\n");
ISession? session = null;
try
{
// 2. Application Configuration 설정
var config = new ApplicationConfiguration()
{
ApplicationName = "OpcTestClient",
ApplicationType = ApplicationType.Client,
ApplicationUri = "urn:OpcTestClient",
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier
{
StoreType = "Directory",
StorePath = Path.Combine(basePkiPath, "own"),
SubjectName = "CN=OpcTestClient"
},
TrustedPeerCertificates = new CertificateTrustList
{
StoreType = "Directory",
StorePath = Path.Combine(basePkiPath, "trusted")
},
// 💡 중요: Issuer와 Rejected 경로를 명시해야 에러가 나지 않습니다.
TrustedIssuerCertificates = new CertificateTrustList
{
StoreType = "Directory",
StorePath = Path.Combine(basePkiPath, "issuers")
},
RejectedCertificateStore = new CertificateTrustList
{
StoreType = "Directory",
StorePath = Path.Combine(basePkiPath, "rejected")
},
AutoAcceptUntrustedCertificates = true,
RejectSHA1SignedCertificates = false,
AddAppCertToTrustedStore = true
},
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
};
// 설정 검증
await config.ValidateAsync(ApplicationType.Client);
// 3. 인증서 수동 주입 (리눅스 PFX 인식 해결)
if (File.Exists(pfxPath))
{
X509Certificate2 clientCertificate = new X509Certificate2(pfxPath, "");
config.SecurityConfiguration.ApplicationCertificate.Certificate = clientCertificate;
Console.WriteLine($"✅ 인증서 수동 로드 성공: {clientCertificate.Subject}");
}
else
{
Console.WriteLine($"❌ PFX 파일을 찾을 수 없습니다! 경로 확인: {pfxPath}");
return;
}
// 4. Endpoint 검색
Console.WriteLine("\nStep 1: Discovering endpoints...");
var endpointConfig = EndpointConfiguration.Create(config);
using (var discoveryClient = DiscoveryClient.Create(new Uri(serverUrl), endpointConfig))
{
var endpoints = discoveryClient.GetEndpoints(null);
if (endpoints == null || endpoints.Count == 0)
{
Console.WriteLine("❌ No endpoints found!");
return;
}
Console.WriteLine($"✅ Found {endpoints.Count} endpoint(s)");
// 가장 높은 보안 수준의 엔드포인트 선택
var selectedEndpoint = endpoints.OrderByDescending(e => e.SecurityLevel).First();
Console.WriteLine($"👉 Selected: {selectedEndpoint.SecurityMode} | {selectedEndpoint.SecurityPolicyUri}");
// 5. Session 생성 (mngr 계정)
Console.WriteLine("\nStep 2: Creating session with mngr account...");
var token = new UserNameIdentityToken
{
UserName = "mngr",
DecryptedPassword = System.Text.Encoding.UTF8.GetBytes("mngr")
};
var userIdentity = new UserIdentity(token);
var configuredEndpoint = new ConfiguredEndpoint(null, selectedEndpoint, endpointConfig);
session = await Session.Create(
config,
configuredEndpoint,
updateBeforeConnect: true,
"OPC Test Session",
60000,
userIdentity,
null);
}
Console.WriteLine($"✅ Connected! Session ID: {session.SessionId}");
// 6. 데이터 읽기 및 브라우징 테스트
Console.WriteLine("\nStep 3: Reading server info...");
await ReadServerInfoAsync(session);
Console.WriteLine("\nStep 4: Checking redundancy...");
await CheckRedundancyAsync(session);
Console.WriteLine("\nStep 5: Browsing nodes...");
await BrowseNodesAsync(session);
Console.WriteLine("\n✅ All tests completed successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"\n❌ Error: {ex.Message}");
if (ex.InnerException != null)
Console.WriteLine($" Inner: {ex.InnerException.Message}");
}
finally
{
if (session != null)
{
await session.CloseAsync();
Console.WriteLine("\nSession closed.");
}
}
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
}
// --- 헬퍼 메서드들 ---
static async Task ReadServerInfoAsync(ISession session)
{
var state = await session.ReadValueAsync(new NodeId(Variables.Server_ServerStatus_State));
var time = await session.ReadValueAsync(new NodeId(Variables.Server_ServerStatus_CurrentTime));
Console.WriteLine($" Server State: {state.Value}");
Console.WriteLine($" Server Time: {time.Value}");
}
static async Task CheckRedundancyAsync(ISession session)
{
var serviceLevel = await session.ReadValueAsync(new NodeId(Variables.Server_ServiceLevel));
byte level = Convert.ToByte(serviceLevel.Value);
string status = level >= 200 ? "PRIMARY" : (level > 0 ? "SECONDARY" : "STANDALONE");
Console.WriteLine($" Service Level: {level} ({status})");
}
static async Task BrowseNodesAsync(ISession session)
{
var browser = new Browser(session)
{
BrowseDirection = BrowseDirection.Forward,
ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences,
IncludeSubtypes = true,
NodeClassMask = (int)(NodeClass.Object | NodeClass.Variable)
};
var references = await browser.BrowseAsync(ObjectIds.ObjectsFolder);
Console.WriteLine($" Found {references.Count} nodes in Objects folder. Top 5:");
foreach (var r in references.Take(5))
{
Console.WriteLine($" - {r.DisplayName} ({r.NodeClass})");
}
}
}
}

Binary file not shown.

View File

@@ -8,6 +8,7 @@
".NETCoreApp,Version=v8.0": {
"OpcConnectionTest/1.0.0": {
"dependencies": {
"Npgsql": "10.0.1",
"OPCFoundation.NetStandard.Opc.Ua": "1.5.378.106",
"OPCFoundation.NetStandard.Opc.Ua.Client": "1.5.378.106"
},
@@ -95,6 +96,18 @@
}
}
},
"Npgsql/10.0.1": {
"dependencies": {
"Microsoft.Extensions.Logging.Abstractions": "10.0.2",
"System.Diagnostics.DiagnosticSource": "10.0.2"
},
"runtime": {
"lib/net8.0/Npgsql.dll": {
"assemblyVersion": "10.0.1.0",
"fileVersion": "10.0.1.0"
}
}
},
"OPCFoundation.NetStandard.Opc.Ua/1.5.378.106": {
"dependencies": {
"OPCFoundation.NetStandard.Opc.Ua.Client": "1.5.378.106",
@@ -333,6 +346,13 @@
"path": "newtonsoft.json/13.0.4",
"hashPath": "newtonsoft.json.13.0.4.nupkg.sha512"
},
"Npgsql/10.0.1": {
"type": "package",
"serviceable": true,
"sha512": "sha512-XyUcxEfqlFomhNTG/ZdGlec+uSOQArKz0Mzz8jYKP/Jj9GM2YabU5CVZtp0yiC4f9hRp+tRZTnHMatJeJ3rwgw==",
"path": "npgsql/10.0.1",
"hashPath": "npgsql.10.0.1.nupkg.sha512"
},
"OPCFoundation.NetStandard.Opc.Ua/1.5.378.106": {
"type": "package",
"serviceable": true,

View File

@@ -13,10 +13,10 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("OpcConnectionTest")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+3181052619700dceeeef81a9a0851130498f177e")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+20ee22ae0c85a902f6767918a8ebece87301c37f")]
[assembly: System.Reflection.AssemblyProductAttribute("OpcConnectionTest")]
[assembly: System.Reflection.AssemblyTitleAttribute("OpcConnectionTest")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.
// MSBuild WriteCodeFragment 클래스에서 생성되었습니다.

View File

@@ -1 +1 @@
c2e941992237f396c84fee88bc09bdecf79f725598c8300136293f3ac50e6b79
f71e0f6a7ff186b47ef80c0b9a2fc70fce275730017c4f885ccba6c266a8ced1

View File

@@ -1 +1 @@
859302d0d0eef299017c6ba5eb484ad68daade90e3fd76bac845760289b34007
0eb9e9b331aa7d76fb1655a62377fb6789f9c255d7f277df7b240c1efef9f843

View File

@@ -37,3 +37,4 @@
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.pdb
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.genruntimeconfig.cache
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/ref/OpcConnectionTest.dll
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/Npgsql.dll

View File

@@ -38,6 +38,10 @@
"net8.0": {
"targetAlias": "net8.0",
"dependencies": {
"Npgsql": {
"target": "Package",
"version": "[10.0.1, )"
},
"OPCFoundation.NetStandard.Opc.Ua": {
"target": "Package",
"version": "[1.5.378.106, )"
@@ -63,7 +67,7 @@
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/lib/dotnet/sdk/8.0.123/PortableRuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "/usr/lib/dotnet/sdk/8.0.124/PortableRuntimeIdentifierGraph.json"
}
}
}

View File

@@ -140,6 +140,23 @@
}
}
},
"Npgsql/10.0.1": {
"type": "package",
"dependencies": {
"Microsoft.Extensions.Logging.Abstractions": "8.0.0",
"System.Diagnostics.DiagnosticSource": "9.0.11"
},
"compile": {
"lib/net8.0/Npgsql.dll": {
"related": ".xml"
}
},
"runtime": {
"lib/net8.0/Npgsql.dll": {
"related": ".xml"
}
}
},
"OPCFoundation.NetStandard.Opc.Ua/1.5.378.106": {
"type": "package",
"dependencies": {
@@ -693,6 +710,25 @@
"packageIcon.png"
]
},
"Npgsql/10.0.1": {
"sha512": "XyUcxEfqlFomhNTG/ZdGlec+uSOQArKz0Mzz8jYKP/Jj9GM2YabU5CVZtp0yiC4f9hRp+tRZTnHMatJeJ3rwgw==",
"type": "package",
"path": "npgsql/10.0.1",
"files": [
".nupkg.metadata",
".signature.p7s",
"README.md",
"lib/net10.0/Npgsql.dll",
"lib/net10.0/Npgsql.xml",
"lib/net8.0/Npgsql.dll",
"lib/net8.0/Npgsql.xml",
"lib/net9.0/Npgsql.dll",
"lib/net9.0/Npgsql.xml",
"npgsql.10.0.1.nupkg.sha512",
"npgsql.nuspec",
"postgresql.png"
]
},
"OPCFoundation.NetStandard.Opc.Ua/1.5.378.106": {
"sha512": "9s7VfRmWRp2W/mrBpCxUe8SCDQCWBPgMsdz9gy3Z19ZCfigukBQj5DoluPz1758lzZpnp/ZJ2eN4mhTNfprwUA==",
"type": "package",
@@ -1144,6 +1180,7 @@
},
"projectFileDependencyGroups": {
"net8.0": [
"Npgsql >= 10.0.1",
"OPCFoundation.NetStandard.Opc.Ua >= 1.5.378.106",
"OPCFoundation.NetStandard.Opc.Ua.Client >= 1.5.378.106"
]
@@ -1185,6 +1222,10 @@
"net8.0": {
"targetAlias": "net8.0",
"dependencies": {
"Npgsql": {
"target": "Package",
"version": "[10.0.1, )"
},
"OPCFoundation.NetStandard.Opc.Ua": {
"target": "Package",
"version": "[1.5.378.106, )"
@@ -1210,7 +1251,7 @@
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/lib/dotnet/sdk/8.0.123/PortableRuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "/usr/lib/dotnet/sdk/8.0.124/PortableRuntimeIdentifierGraph.json"
}
}
}

View File

@@ -1,6 +1,6 @@
{
"version": 2,
"dgSpecHash": "KrguJvkD41Un9qS/sxFwULOR3owX1Gzwg9Kz5TmdqikJypnpz2UYGOX4knjW3w4fcsh2k1y/V+pMLyI7ALdxig==",
"dgSpecHash": "JkmbqYtRIj+9sIbdQTCB0cgSbEyAbiWV8DLdqSl1OhMWi1LBGVOLnmIcwM57vO7JoXP5Gqg3b+s87A0obW20xQ==",
"success": true,
"projectFilePath": "/home/pacer/projects/OpcConnectionTest/OpcConnectionTest.csproj",
"expectedPackageFiles": [
@@ -12,6 +12,7 @@
"/home/pacer/.nuget/packages/microsoft.extensions.options/10.0.2/microsoft.extensions.options.10.0.2.nupkg.sha512",
"/home/pacer/.nuget/packages/microsoft.extensions.primitives/10.0.2/microsoft.extensions.primitives.10.0.2.nupkg.sha512",
"/home/pacer/.nuget/packages/newtonsoft.json/13.0.4/newtonsoft.json.13.0.4.nupkg.sha512",
"/home/pacer/.nuget/packages/npgsql/10.0.1/npgsql.10.0.1.nupkg.sha512",
"/home/pacer/.nuget/packages/opcfoundation.netstandard.opc.ua/1.5.378.106/opcfoundation.netstandard.opc.ua.1.5.378.106.nupkg.sha512",
"/home/pacer/.nuget/packages/opcfoundation.netstandard.opc.ua.client/1.5.378.106/opcfoundation.netstandard.opc.ua.client.1.5.378.106.nupkg.sha512",
"/home/pacer/.nuget/packages/opcfoundation.netstandard.opc.ua.configuration/1.5.378.106/opcfoundation.netstandard.opc.ua.configuration.1.5.378.106.nupkg.sha512",