首页 .NET/Web FTP推送

FTP推送

作者:胡同里的砖头 围观群众:49 更新于:2025-08-01

using System;
using System.Collections.Generic;
using Opc.Da;
using BottleMgn.Interface;
using YCS.Communications.OPC;

namespace BottleMgn.Implements
{
public class OPCReaderPLC : IOPCReader
{
private const string OpcServerName = "KEPware.KEPServerEx.V4";
private readonly OPCClient _opcClient;

public OPCReaderPLC()
{
_opcClient = Var.OpcClient ?? new OPCClient(OpcServerName);
Var.OpcClient = _opcClient;
}

/// <summary>
/// Reads the current value from a PLC address
/// </summary>
/// <param name="plcAddress">The full OPC address of the PLC item</param>
/// <returns>ResultModel containing the read value or error information</returns>
public ResultModel ReadPLCValue(string plcAddress)
{
try
{
var item = CreateOpcItem(plcAddress);

_opcClient.Connect();
var result = _opcClient.GetItem(item);
_opcClient.Disconnect();

if (result.Value == null)
{
return ResultMsg.OPCClientReaderError();
}

var stringValue = NormalizeValue(result.Value);
return new ResultModel { Object = stringValue, IsError = false };
}
catch (Exception ex)
{
return new ResultModel
{
IsError = true,
Msg = $"Error reading PLC value: {ex.Message}"
};
}
}

/// <summary>
/// Writes a value to a PLC address
/// </summary>
/// <param name="plcAddress">The full OPC address of the PLC item</param>
/// <param name="value">The value to write (will be converted to appropriate type)</param>
/// <returns>ResultModel indicating success or failure</returns>
public ResultModel WritePLCValue(string plcAddress, object value)
{
try
{
var item = new ItemValue
{
ClientHandle = Guid.NewGuid().ToString(),
ItemPath = GetItemPath(plcAddress),
ItemName = GetItemName(plcAddress),
Value = value
};

_opcClient.Connect();
var result = _opcClient.SetItem(item);
_opcClient.Disconnect();

return result.ResultID == Opc.ResultID.S_OK
? ResultMsg.Success()
: ResultMsg.OPCClientWriteError();
}
catch (Exception ex)
{
return new ResultModel
{
IsError = true,
Msg = $"Error writing to PLC: {ex.Message}"
};
}
}

private Item CreateOpcItem(string plcAddress)
{
return new Item
{
ClientHandle = Guid.NewGuid().ToString(),
ItemPath = GetItemPath(plcAddress),
ItemName = GetItemName(plcAddress)
};
}

private string NormalizeValue(object value)
{
if (value == null) return string.Empty;

string stringValue = value.ToString();

// Convert boolean values to "1"/"0" for consistency
if (stringValue.Equals("true", StringComparison.OrdinalIgnoreCase)) return "1";
if (stringValue.Equals("false", StringComparison.OrdinalIgnoreCase)) return "0";

return stringValue;
}

private string GetItemPath(string fullAddress)
{
// Implement logic to extract path from full address
// Example: if fullAddress is "PLC1.Machine1.Tag1" return "PLC1.Machine1"
var lastDot = fullAddress.LastIndexOf('.');
return lastDot > 0 ? fullAddress.Substring(0, lastDot) : string.Empty;
}

private string GetItemName(string fullAddress)
{
// Implement logic to extract name from full address
// Example: if fullAddress is "PLC1.Machine1.Tag1" return "Tag1"
var lastDot = fullAddress.LastIndexOf('.');
return lastDot > 0 ? fullAddress.Substring(lastDot + 1) : fullAddress;
}
}
}

  • 本文标题: FTP推送
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8