我有GetMethod来读取OPC Item值。它第一次工作正常,然后需要更长时间,几次没有响应。但在我的控制台中,价值得到了更新。控制台和客户端服务都在同一服务器上运行。
这是我的代码:(_订阅 - 控制台订阅)
public OPCItem GetOPCItem(string itemName)
{
if (_Subscription == null)
throw new Exception("Have not Subscription.");
List<Opc.Da.Item> itemvalues = new List<Opc.Da.Item>();
foreach (Opc.Da.Item item in _Subscription.Items)
{
if (item.ItemName == itemName)
{
Opc.Da.Item itemTemp = new Opc.Da.Item((Opc.ItemIdentifier)item);
itemvalues.Add(itemTemp);
break;
}
}
if (itemvalues.Count == 1)
{
Opc.Da.ItemValueResult[] values = _Subscription.Read(itemvalues.ToArray());
foreach (Opc.Da.ItemValueResult item in values)
{
if (item.ItemName == itemName)
{
OPCItem itemResult = new OPCItem();
itemResult.ItemName = itemName;
itemResult.ItemValue = item.Value;
itemResult.OperationTime = item.Timestamp;
return itemResult;
}
}
return null;
}
else
throw new Exception("Read Unknown Tag Value.");
}
我从Webproxy打电话
public object GetOPCItemValue(string opcItemName)
{
try
{
Automation.OPCItem mItem = _OPCServer.GetOPCItem(opcItemName);
WriteEvenMssage("[ReadItem]" + mItem.ItemValue.ToString() + ":" + opcItemName);
return mItem.ItemValue;
}
catch (Exception ex)
{
WriteErrorLog(ex.ToString());
return null;
}
}
在我的客户端表单中,我试图在计时器控件中读取OPC Item(sBatchID):
private void timerMain_Tick(object sender, EventArgs e)
{
try
{
//30 Seconds to refresh无操作30秒刷新一次
if ((DateTime.Now - ClientContext.Instance.LastOperationDateTime).TotalSeconds < 15)
return;
//Refresh
btnRefresh.PerformClick();
if (_DsOPCItem == null)
{
string sEquipment = StringHelper.NVL(_Ds.Tables[0].Rows[0]["EquipmentID"], string.Empty);
}
if (_DsOPCItem.Tables[0].Rows.Count == 1)
{
if (string.IsNullOrEmpty(txtBatchNO.Text) || txtBatchNO.Properties.Appearance.BackColor != System.Drawing.Color.Green)
{
//....Get OPC ITemvalue by OPCItemName
string sOPCItemName = _DsOPCItem.Tables[0].Rows[0]["ItemName"].ToString();
string sBatchID = SrvOPCServer.GetOPCItemValue(sOPCItemName).ToString();
if (sBatchID != "0")
{
txtBatchNO.Text = sBatchID;
int rowHandle = viewJobOrder.LocateByValue("BatchNO", sBatchID);
if (rowHandle != DevExpress.XtraGrid.GridControl.InvalidRowHandle)
{
viewJobOrder.FocusedRowHandle = rowHandle;
txtBatchNO.Properties.Appearance.BackColor = System.Drawing.Color.Green;
txtBatchNO.Properties.Appearance.ForeColor = System.Drawing.Color.White;
}
}
}
}
}
catch (Exception ex)
{
timerMain.Enabled = false;
MessageBoxHelper.ShowException(ex);
}
finally
{
timerMain.Enabled = true;
}
}
调试此代码时,我发现第一次快速响应并在读取OPCItem(sBatchID)时开始变慢。我将计时器间隔设置为3秒。除了从数组中读取以外的任何读取opc项的方法吗?