using IACommService4CSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PlcCom
{
public class DeltaAxPLCNetConfig
{
private string axIP = "192.168.0.10";
public string GetAxIP()
{
return axIP;
}
private void SetAxIP(string value)
{
axIP = value;
}
private string axPort = "11740";
public string GetAxPort()
{
return axPort;
}
private void SetAxPort(string value)
{
axPort = value;
}
}
public class DeltaAxCommProtocol : IPlcComProtocol
{
public bool IsConnected { get; set; }
public string AxIP { get; }
public string AxPort { get; }
AXClient_t client;
public DeltaAxCommProtocol(DeltaAxPLCNetConfig deltaAxPLCNetConfig)
{
AxIP= deltaAxPLCNetConfig.GetAxIP();
AxPort= deltaAxPLCNetConfig.GetAxPort();
var rt = Initialize();
client = new AXClient_t();
}
public DeltaAxCommProtocol():this(new DeltaAxPLCNetConfig())
{
}
private uint Initialize()
{
//Configure log
LogConfig_t logConfig = new LogConfig_t();
logConfig.Enable = true;
logConfig.Path = "./";
logConfig.DateFormat = "%Y-%m-%d %H:%M:%S.%q";
logConfig.MaxFileSize = 512 * 1024;
logConfig.MaxBackupIndex = 5;
logConfig.LevelMask = Convert.ToUInt32(LevelMask_t.LOG_LEVEL_FATAL | LevelMask_t.LOG_LEVEL_ERROR | LevelMask_t.LOG_LEVEL_WARN | LevelMask_t.LOG_LEVEL_INFO);
logConfig.CategoryMask = Convert.ToUInt32(CategoryMask_t.LOG_CATEGORY_SYSTEM | CategoryMask_t.LOG_CATEGORY_CONNECT);
var status = IACommService_t.SetLogConfig(logConfig);
//Global initialization
status = IACommService_t.Initialise();
return status;
}
private uint WaitOperationComplete(ICallback callback, uint timeout/*second*/)
{
if (null == callback)
return ErrorCode.InvalidArgument;
uint status = ErrorCode.Good;
TimeSpan begin = new TimeSpan(DateTime.Now.Ticks);
while (!callback.IsDone &&
((new TimeSpan(DateTime.Now.Ticks)) - begin).TotalSeconds <= timeout)
{
Thread.Sleep(100);
}
if (!callback.IsDone)
{
status = ErrorCode.Timeout;
}
return status;
}
private uint Connect(AXClient_t client)
{
if (null == client)
return ErrorCode.InvalidArgument;
//Connection type
bool isConnectByIPAddr = true;
bool isConnectViaGateWay = false;
//Gateway IP, port
string gatewayIPAddr = "127.0.0.1";
string gatewayPort = "1217";
//Device IP, port, logic address
//string axIPAddr = "192.168.0.10";
//string axPort = "11740";
string axIPAddr = AxIP;
string axPort = AxPort;
string axAddress = "3001.50A8";
//User, password
string userName = "";
string password = "";
uint status;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("===================== Start to Connect ====================");
if (isConnectByIPAddr)
{
if (isConnectViaGateWay)
status = client.ConnectViaGateway(gatewayIPAddr, gatewayPort, axIPAddr, axPort, userName, password);
else
status = client.Connect(axIPAddr, axPort, userName, password);
}
else
{
if (isConnectViaGateWay)
status = client.ConnectViaGatewayByAddress(gatewayIPAddr, gatewayPort, axAddress, userName, password);
else
status = client.ConnectByAddress(axAddress, userName, password);
}
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("==================== Connect Completed ===================\n");
return status;
}
///
/// 连接PLC
///
/// 返回代码,0:成功,其它:失败
public (uint rt,string errReason) ConnectPLC()
{
//BrowseDeviceCallback browseDeviceCB = new BrowseDeviceCallback();
//rt = AXClient_t.BrowseAsync(null, null, browseDeviceCB);
//if (rt != 0)
// return (rt, "BrowseAsync");
//rt =WaitOperationComplete(browseDeviceCB, 3/*seconds*/);
//if (rt != 0)
// return (rt, "WaitOperationComplete");
var rt = Connect(client);
if (rt != 0)
{
IsConnected = false;
return (rt, "Connect Failed");
}
else
{
var r = ReadValue("Application.Var_state.al", out object al);
if (r == 0 && al is bool b && !b)
{
Close();
return (0x7FFFFFFF, "");
}
IsConnected = true;
return (rt, "Connect Success");
}
}
///
/// 关闭连接
///
/// 返回代码,0:成功,其它:失败
public uint Close()
{
var rt=client.Disconnect();
client.Dispose();
return rt;
}
///
/// 读取指定地址和指定类型的值
///
/// PLC地址字符串
/// 返回的读取结果
/// 返回代码,0:成功,其它:失败
public uint ReadValue(string address, out object result)
{
DataItem_t dataItemFromRead = null;
var status = client.Read(address, ref dataItemFromRead);
if (ErrorCode.Good == status && dataItemFromRead != null)
{
result = dataItemFromRead.Value.Get();
}
else
{
result = null;
}
if (dataItemFromRead != null)
{
dataItemFromRead.Dispose();
}
return status;
}
///
/// 批量读取指定地址和指定类型的值
///
/// PLC地址字符串列表
/// 返回的读取结果列表
/// 返回代码,0:成功,其它:失败
public uint ReadValue(List addresses, out List