給電廠做的項目,需要從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;
}
}
專業做電廠的項目,如果有相關外包項目可以聯繫我,微信xiaoyiyz