You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
246 lines
8.5 KiB
C#
246 lines
8.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using Mesnac.Equips;
|
|
using Mesnac.Equips.BaseInfo;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace Mesnac.Equip.AllenBradley.AB5000.Net
|
|
{
|
|
public class Equip : BaseEquip
|
|
{
|
|
private IntPtr structToIntPtr<T>(T[] lst)
|
|
{
|
|
int nSizeOfT = Marshal.SizeOf(typeof(T));
|
|
int nSizeOfIntPtr = Marshal.SizeOf(typeof(IntPtr));
|
|
IntPtr Result = Marshal.AllocHGlobal(nSizeOfIntPtr * lst.Length);
|
|
for (int i = 0; i < lst.Length; i++)
|
|
{
|
|
Marshal.StructureToPtr(lst[i], (IntPtr)((UInt32)Result + i * nSizeOfIntPtr), true);
|
|
}
|
|
return Result;
|
|
}
|
|
private T[] IntPtrTostruct<T>(IntPtr p, T[] lst)
|
|
{
|
|
int nSizeOfIntPtr = Marshal.SizeOf(typeof(IntPtr));
|
|
for (int i = 0; i < lst.Length; i++)
|
|
{
|
|
T r = (T)Marshal.PtrToStructure((IntPtr)((UInt32)p + i * nSizeOfIntPtr), typeof(T));
|
|
lst[i] = r;
|
|
}
|
|
return lst;
|
|
}
|
|
private string getDriveNameFileFullName()
|
|
{
|
|
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log", "AB5000.DriverName.bin");
|
|
}
|
|
private void saveDriveName(string name)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
{
|
|
return;
|
|
}
|
|
string path = getDriveNameFileFullName();
|
|
FileInfo fi = new FileInfo(path);
|
|
if (!fi.Directory.Exists)
|
|
{
|
|
fi.Directory.Create();
|
|
}
|
|
if (fi.Exists)
|
|
{
|
|
fi.Delete();
|
|
}
|
|
System.IO.StreamWriter sw = new System.IO.StreamWriter(fi.FullName, false);
|
|
sw.WriteLine(name);
|
|
sw.Close();
|
|
}
|
|
private string getDriveName()
|
|
{
|
|
string path = getDriveNameFileFullName();
|
|
FileInfo fi = new FileInfo(path);
|
|
if (!fi.Directory.Exists)
|
|
{
|
|
fi.Directory.Create();
|
|
}
|
|
if (!fi.Exists)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
System.IO.StreamReader sw = new System.IO.StreamReader(fi.FullName);
|
|
string ss = sw.ReadToEnd();
|
|
sw.Close();
|
|
return ss;
|
|
}
|
|
public override bool Open()
|
|
{
|
|
lock (this)
|
|
{
|
|
getIP();
|
|
this.State = false;
|
|
Logger.Instance.Info("DTL_INIT");
|
|
int iResult = 0;
|
|
uint id = 0;
|
|
iResult = DTL32.DTL_INIT(2048);
|
|
Logger.Instance.Info("DTL_INIT Result=" + iResult.ToString());
|
|
if ((iResult != 0) && (iResult != 39)) // 39已经初始化
|
|
{
|
|
this.State = false;
|
|
return this.State;
|
|
}
|
|
string driveName = getDriveName();
|
|
if (string.IsNullOrWhiteSpace(driveName))
|
|
{
|
|
Logger.Instance.Info("get szDriverName");
|
|
uint drivers = 0;
|
|
DTL32.DtlDriver[] dtlDriver = new DTL32.DtlDriver[16];
|
|
IntPtr pDriver = structToIntPtr<DTL32.DtlDriver>(dtlDriver);
|
|
Logger.Instance.Info("DTL_DRIVER_LIST");
|
|
iResult = DTL32.DTL_DRIVER_LIST(pDriver, ref drivers, 1000);
|
|
Logger.Instance.Info("DTL_DRIVER_LIST Result=" + iResult.ToString());
|
|
dtlDriver = IntPtrTostruct<DTL32.DtlDriver>(pDriver, dtlDriver);
|
|
Logger.Instance.Info("IntPtrTostruct");
|
|
driveName = new string(dtlDriver[id].szDriverName);
|
|
Logger.Instance.Info("szDriverName=" + driveName);
|
|
saveDriveName(driveName);
|
|
Marshal.FreeHGlobal(pDriver);
|
|
Logger.Instance.Info("FreeHGlobal");
|
|
if ((iResult != 0))
|
|
{
|
|
this.State = false;
|
|
return this.State;
|
|
}
|
|
}
|
|
iResult = DTL32.DTL_DRIVER_OPEN(id, driveName, 5000);
|
|
Logger.Instance.Info("DTL_DRIVER_OPEN Result=" + iResult.ToString());
|
|
if ((iResult == 0) || (iResult == 47)) // 47已经连接上
|
|
{
|
|
this.State = true;
|
|
}
|
|
return this.State;
|
|
}
|
|
}
|
|
private string getIP()
|
|
{
|
|
string result = string.Empty;
|
|
string ip = ((Mesnac.Equips.Connection.Net.ConnType)this.Main.ConnType).IP;
|
|
int iplen = ip.Length;
|
|
if (ip.Length % 2 != 0)
|
|
{
|
|
iplen++;
|
|
}
|
|
result = iplen.ToString();
|
|
foreach (char s in ip)
|
|
{
|
|
result += "." + Convert.ToInt32(s).ToString();
|
|
}
|
|
if (ip.Length % 2 != 0)
|
|
{
|
|
result += ".0";
|
|
}
|
|
return result;
|
|
}
|
|
public override bool Read(string block, int start, int len, out object[] buff)
|
|
{
|
|
lock (this)
|
|
{
|
|
buff = new object[len];
|
|
if (!this.Open())
|
|
{
|
|
return false;
|
|
}
|
|
int state = 0;
|
|
ushort[] _buff = new ushort[len];
|
|
string ss = "$N" + block.ToString() + ":" + start.ToString() + "," +
|
|
len.ToString() + ",WORD,READ,AB:CIP,16." + getIP() + ".1.0,PLC5,0'";
|
|
Logger.Instance.Info("DTL_C_DEFINE=" + ss);
|
|
uint id = 0;
|
|
int iResult = DTL32.DTL_C_DEFINE(ref id, ss);
|
|
Logger.Instance.Info("DTL_C_DEFINE Result=" + iResult.ToString());
|
|
if (iResult != 0)
|
|
{
|
|
return false;
|
|
}
|
|
iResult = DTL32.DTL_READ_W(id, _buff, ref state, 1000);
|
|
Logger.Instance.Info("DTL_READ_W Result=" + iResult.ToString());
|
|
DTL32.DTL_UNDEF(id);
|
|
if (iResult != 0)
|
|
{
|
|
return false;
|
|
}
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
int value = _buff[i];
|
|
if (value > ushort.MaxValue)
|
|
{
|
|
value = ushort.MaxValue - value;
|
|
}
|
|
buff[i] = value;
|
|
}
|
|
Logger.Instance.Info("Read True");
|
|
return true;
|
|
}
|
|
}
|
|
private ushort ToValue(object obj, ushort defaultValue)
|
|
{
|
|
ushort result = 0;
|
|
if (obj != null
|
|
&& obj != DBNull.Value
|
|
&& ushort.TryParse(obj.ToString(), out result))
|
|
{
|
|
return result;
|
|
}
|
|
return defaultValue;
|
|
}
|
|
public override bool Write(int block, int start, object[] buff)
|
|
{
|
|
lock (this)
|
|
{
|
|
if (buff.Length == 0)
|
|
{
|
|
return true;
|
|
}
|
|
Logger.Instance.Info("Open");
|
|
if (!this.Open())
|
|
{
|
|
return false;
|
|
}
|
|
int state = 0;
|
|
ushort[] _buff = new ushort[buff.Length];
|
|
for (int i = 0; i < buff.Length; i++)
|
|
{
|
|
_buff[i] = ToValue(buff[i], 0);
|
|
}
|
|
string ss = "$N" + block.ToString() + ":" + start.ToString() + "," +
|
|
buff.Length.ToString() + ",WORD,MODIFY,AB:CIP,16." + getIP() + ".1.0,PLC5,0'";
|
|
uint id = 0;
|
|
int iResult = DTL32.DTL_C_DEFINE(ref id, ss);
|
|
if (iResult != 0)
|
|
{
|
|
return false;
|
|
}
|
|
iResult = DTL32.DTL_WRITE_W(id, _buff, ref state, 1000);
|
|
DTL32.DTL_UNDEF(id);
|
|
if (iResult != 0)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
public override void Close()
|
|
{
|
|
lock (this)
|
|
{
|
|
try
|
|
{
|
|
DTL32.DTL_UNINIT(0);
|
|
DTL32.DTL_UNDEF(0);
|
|
DTL32.DTL_DRIVER_CLOSE(0, 100);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|