给电厂做的项目,需要从SIS取数,之前做过很多厂的SIS都是PI实时数据库,这个厂用的是上海麦杰的openPlant,取数方式大同小异,但是感觉openplant要比PI快很多,用着很顺手,上千个点秒级取数 相当给力;还接触过一个厂的SIS用的是华电天仁VeStore,但是没有取数操作过;
下面看看对openplant的操作
private int m_port = 8200;
private int m_timeout = 5;
private int m_version = 4;
private string ipAddr;
private string uName;
private string pwd;
bool isConn = false;
IntPtr op = new IntPtr();
IntPtr group; //创建点组
public OpenPlant(string ipAddr, string uName, string pwd)
{
this.ipAddr = ipAddr;
this.uName = uName;
this.pwd = pwd;
ConnOpenPlant();
}
/// <summary>
/// 连接
/// </summary>
/// <param name="ipAddr"></param>
/// <param name="uName"></param>
/// <param name="pwd"></param>
/// <returns></returns>
public bool ConnOpenPlant()
{
//if (op.ToInt32() > 1) //这个判断不准确
// return true;
if (isConn)
return true;
try
{
op = OPAPI2.op2_init(m_version, ipAddr, m_port, m_timeout, uName, pwd, ".", 100);
group = OPAPI2.op2_new_group();
//GlobalVariables.OPCState = true;//如果找不到也不会报错 ZZH
}
catch (Exception ex)
{
GlobalVariables.OPCState = false;
isConn = false;
WriteLog.WriteLogs(ex.ToString());
return false;
}
if (op.ToInt32() == 0)
{
GlobalVariables.OPCState = false;
isConn = false;
OPAPI2.op2_free_group(group); //网络断开组依然会创建,所以要释放 ZZH
return false;
}
else
{
GlobalVariables.OPCState = true;
isConn = true;
}
return true;
}
/// <summary>
/// 关闭
/// </summary>
public void CloseOpenPlant()
{
try
{
if (group.ToInt32() > 0)
{
//关闭的时候释放组
OPAPI2.op2_free_group(group);
TagHS.Clear();
}
OPAPI2.op2_close(op);
isConn = false;
GC.Collect();
}
catch
{
GC.Collect();
}
}
Hashtable TagHS = new Hashtable();
/// <summary>
/// 取数
/// </summary>
/// <param name="tags"></param>
/// <returns></returns>
public string[] GetOPValues(List<string> tags)
{
try
{
if (!ConnOpenPlant())
return null;
if (group.ToInt32() <= 0)
{
group = OPAPI2.op2_new_group(); //创建点组
TagHS.Clear(); //如果新建了组,需要把HashTable清空 ZZH
}
foreach (string tag in tags)
{
if (!TagHS.ContainsKey(tag))
{
OPAPI2.op2_add_group_point(group, tag);
TagHS.Add(tag, "");
}
}
int pointNum = TagHS.Count;
string[] resultStr = new string[pointNum];
//取pointNum个点的实时
short[] status = new short[pointNum];
int[] errors = new int[pointNum];
int[] tm = new int[pointNum];
double[] read_value = new double[pointNum];
int ret = OPAPI2.op2_get_value_byname(op, group, tm, status, read_value, errors);
if (ret != 0)
{
try
{
if(isConn)
CloseOpenPlant();
GlobalVariables.OPCState = false;
return null;
}
catch
{ }
}
string strValue = "";
for (int i = 0; i < read_value.Count(); i++)
{
strValue += "点:" + tags[i] + ", 值:" + read_value[i] + "\r\n";
resultStr[i] = read_value[i].ToString();
}
WriteLog.WriteLogs(strValue);
return resultStr;
}
catch (Exception ee)
{
WriteLog.WriteLogs(ee.ToString());
return null;
}
}