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

@@ -0,0 +1,212 @@
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}");
}
}
}
}