⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 此文件为OPC Client IsVJTrend源代码,在VJ++开发环境下调试
💻 JAVA
字号:
/////////////////////////////////////////////////////////////////////////////
//
//  OPC Trend VJ Client: server.java
//                       (Source File)
//
/////////////////////////////////////////////////////////////////////////////
//
//          Author: Raphael Imhof
//    Initial Date: 11/04/98
//       $Workfile: Server.java $
//       $Revision: 1 $
//           $Date: 7/27/99 5:09p $
//   Target System: Microsoft Windows NT 4.0
//     Environment: Visual J++ 6.0 / OPC DataAccess 2.0
//         Remarks:
//
/////////////////////////////////////////////////////////////////////////////
//
//     Description: implementation of the OPC Server encapsulation class.
//
//
/////////////////////////////////////////////////////////////////////////////
//
//  History of Changes     (Please remove very old comments and blank lines!)
//            $Log: /IDK/OPCServer/clients/VJ++/ISVjTrend/Server.java $
// 
// 1     7/27/99 5:09p Imhof
// 
// 1     7/27/99 4:39p Imhof
// 
// 1     7/27/99 4:33p Imhof
// 
// 6     1/23/99 4:52p Imhof
// Exchanged Exception with the base class Throwable in the catch blocks.
// => get all exceptions.
// 
// 5     1/20/99 3:17p Imhof
// Updated header
//
//
//  $Nokeywords:$ (To avoid useless search while checking in.)
/////////////////////////////////////////////////////////////////////////////
//  Copyright (C) 1999, Siemens Building Technologies, Inc. Landis Division
//
//  SIEMENS BUILDING TECHNOLOGIES, INC. IS PROVIDING THE FOLLOWING EXAMPLES
//  OF CODE AS SAMPLE ONLY.
//  SIEMENS BUILDING TECHNOLOGIES, INC. MAKES NO REPRESENTATIONS OR WARRANTIES
//  OF ANY KIND WITH RESPECT TO THE VALIDTY OF THE CODES OR DESIRED RESULTS
//  AND DISCLAIMS ALL SUCH REPRESENTATIONS AND WARRANTIES, INCLUDING FOR EXAMPLE,
//  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//  SIEMENS BUILIDNG TECHNOLOGIES, INC. DOES NOT REPRESENT OR WARRANT THAT
//  THE FOLLOWING CODE SAMPLES ARE ACCURATE, VALID, COMPLETE OR CURRENT.
//
/////////////////////////////////////////////////////////////////////////////

import opcdaauto.*;
import com.ms.com.*;
import com.ms.wfc.ui.*;

public class Server
{
	private OPCServer	opcServer;
	private OPCGroup	opcGroup;
	private OPCItem		opcPoint;
	private String		sPoint;
	private boolean		bConnected;
	
	/*
		the OPC automation DLL uses VB array bounds
		we have to use the array range 1-n of an java array
		therefore we add 1 to the array size and access the array [i + 1]
	*/
	public static int	vbArrayOffset = 1;
	
	public Server()
	{
		super();
		
		//TODO:
		bConnected = false;
		
	}
	
	public String GetTagName()
	{
		return sPoint;
	}
	
	public OPCServer GetOPCServer()
	{
		return opcServer;
	}
	
	public boolean IsConnected()
	{
		return bConnected;
	}
	
	public boolean Connect()
	{
		try
		{
			if(IsConnected()) return true;
			
			Variant  node = new Variant();
	
			opcServer = new OPCServer();
		
			ConnectServer fConnectDlg = new ConnectServer();
			
			fConnectDlg.server = opcServer;
								
			if (fConnectDlg.showDialog() == DialogResult.OK )
			{
				//IOPCAutoServer testServer = (IOPCAutoServer)new OPCServer();
				node.putString(fConnectDlg.ServerComputer.getText());
				opcServer.Connect(fConnectDlg.sOPCServer, node);
				bConnected = true;
			
				return Init();
			}
			else return false;
		}
		catch(Throwable excpt)
		{
			MessageBox.show (excpt.getMessage(), "ISVjTrend::Server::Connect()", MessageBox.ICONERROR | MessageBox.OK);
			return false;
		}	
	}
	
	public boolean Disconnect()
	{
		try
		{
			//TODO: remove item ?
			OPCGroups opcGroups = opcServer.getOPCGroups();
			opcGroups.RemoveAll();
							   
			opcServer.Disconnect();

			bConnected = false;
				
			return true;
		}
		catch(Throwable excpt)
		{
			MessageBox.show (excpt.getMessage(), "ISVjTrend::Server::Disconnect()", MessageBox.ICONERROR | MessageBox.OK);
			bConnected = false;
			return false;
		}
	}
	
	private boolean Init()
	{
		try
		{
			OPCGroups opcGroups;
	
			Variant	groupName = new Variant();
			groupName.putString("Group1");
			opcGroups = opcServer.getOPCGroups();
	
			opcGroup = opcGroups.Add(groupName);
			opcGroup.setIsActive(true);
			opcGroup.setIsSubscribed(true);
			opcGroup.setUpdateRate(100);
			
			return true;
		}
		catch(Throwable excpt)
		{
			MessageBox.show (excpt.getMessage(), "ISVjTrend::Server::Init()", MessageBox.ICONERROR | MessageBox.OK);
			return false;
		}
	}
	
	public boolean SelectTag()
	{
		try
		{
			BrowserDlg fBrowser = new BrowserDlg();
		
			fBrowser.server = opcServer;
			
			if (fBrowser.showDialog() == DialogResult.OK )
			{
				//add item
				OPCItems opcItems = opcGroup.getOPCItems();
				if(opcItems.getCount() > 0)
				{
					SafeArray Errors = new SafeArray(Variant.VariantInt , 1 + vbArrayOffset);//out					
					SafeArray ServerHandles = new SafeArray(Variant.VariantInt, 1 + vbArrayOffset); //in
					ServerHandles.setInt(0 + vbArrayOffset,opcPoint.getServerHandle()); 
					opcItems.Remove(1, ServerHandles, Errors);
				}
				
				SafeArray ClientHandles = new SafeArray(Variant.VariantInt, 1 + vbArrayOffset); //in
				ClientHandles.setInt(0 + vbArrayOffset ,1); 
				SafeArray OPCItemIDs = new SafeArray(Variant.VariantString, 1 + vbArrayOffset); //in
				OPCItemIDs.setString(0 + vbArrayOffset, fBrowser.sTag);
				
				SafeArray ItemServerHandles = new SafeArray(Variant.VariantInt , 1 + vbArrayOffset); //out
				SafeArray ItemServerErrors = new SafeArray(Variant.VariantInt , 1 + vbArrayOffset);//out
				Variant RequestedDataTypes = new Variant(); //in
				Variant AccessPaths = new Variant(); //in


				opcItems.AddItems(1, OPCItemIDs, ClientHandles, ItemServerHandles, ItemServerErrors, RequestedDataTypes, AccessPaths);
				opcPoint = opcItems.GetOPCItem(ItemServerHandles.getInt(0 + vbArrayOffset));
				opcItems.SetActive( 1, ItemServerHandles, true, ItemServerErrors);
	
				sPoint = fBrowser.sTag;
						
				return true; 
			}
			else return false;
		}
		catch(Throwable excpt)
		{
			MessageBox.show (excpt.getMessage(), "ISVjTrend::Server::SelectTag()", MessageBox.ICONERROR | MessageBox.OK);
			return false;
		}
		
	}
	
	public boolean ReadTrend(int Samples, SafeArray Values, SafeArray Errors, Variant Qualities,Variant TimeStamps)
	{ 
		//only sync supported by InfoServer
				
		try
		{
			SafeArray ReadServerHandles = new SafeArray(Variant.VariantInt, Samples + vbArrayOffset); //in
		
			//do the init
			for(int i = 0; i < Samples; i++)
			{
				ReadServerHandles.setInt(i + vbArrayOffset, opcPoint.getServerHandle());
			}
				
			opcGroup.SyncRead(OPCDataSource.OPCCache, Samples, ReadServerHandles, Values, Errors, Qualities, TimeStamps);
			
			return true;
		}
		catch(Throwable excpt)
		{
			MessageBox.show (excpt.getMessage(), "ISVjTrend::Server::ReadTrend()", MessageBox.ICONERROR | MessageBox.OK);
			return false;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -