AutomationException: 0x80070057 - One or more arguments are invalid |
|
You may get AutomationException: 0x80070057 - One or more arguments are invalid when programming OPC in Java. This error is caused by incorrect array indices. The indices of some OPC collection objects have to start from 1. This is not a problem in VB because the index of a VB array also starts from 1. For example, the VB program below works fine:
Dim TestServer As OPCServer
Dim TestGroupCollection As OPCGroups
Dim WithEvents Group1 As OPCGroup
Dim ItemCollection1 As OPCItems
Dim OPCItemIDs() As String
Dim ClientHandles() As Long
Dim ReadWriteHandles() As Long
Dim ItemServerHandles() As Long
Dim ItemServerErrors() As Long
Dim RequestedDataTypes As Variant
Dim AccessPaths As Variant
Dim MaxItems As Integer
' Start monitoring the value
Private Sub StartBtn_Click()
Dim ItemTag As String
ItemTag = "D57PT201.AI_MEAS"
Dim ItemValues() As Variant
Dim ItemQualities As Variant
Dim ItemTimeStamps As Variant
Dim idx As Integer
MaxItems = 1
ReDim OPCItemIDs(MaxItems)
ReDim ClientHandles(MaxItems)
ReDim ReadWriteHandles(MaxItems)
' Create connection to the OPC server
Set TestServer = CreateObject("Matrikon.OPC.Automation.1")
TestServer.Connect "Matrikon.OPC.Simulation.1"
' Create a group to contain the tag
Set TestGroupCollection = TestServer.OPCGroups
Set Group1 = TestGroupCollection.Add("group1")
Group1.ClientHandle = 100
Group1.UpdateRate = 1000
Set ItemCollection1 = Group1.OPCItems
ItemCollection1.DefaultAccessPath = ""
' Add the tag
For idx = 1 To MaxItems
ClientHandles(idx) = idx
OPCItemIDs(idx) = ItemTag
Next idx
ItemCollection1.AddItems MaxItems, OPCItemIDs, ClientHandles, ItemServerHandles, ItemServerErrors, RequestedDataTypes, AccessPaths
MsgBox "Success"
End Sub |
However if you map the above VB code to Java code, you need to increment the index of corresponding Java array by 1. In the Java example below, please pay attention to how Java arrays opcItemIDs and clientHandles are instantiated, and also how the for loop handles the index.
import opcauto.*;
public class OpcExample {
OPCServer testServer;
OPCGroups testGroupCollection;
OPCGroup group1;
OPCItems itemCollection1;
String[] opcItemIDs;
int[] clientHandles;
int[] readWriteHandles;
int[][] itemServerHandles;
int[][] itemServerErrors;
Object requestedDataTypes = null;
Object accessPaths = null;
int maxItems;
boolean started = false;
public OpcExample() {
}
public static void main(String[] args) throws Exception {
OpcExample opcExample = new OpcExample();
opcExample.start();
}
private void start(){
try {
String tagVal;
String ItemTag = "D57PT201.AI_MEAS";
Object[][] itemValues;
Object[] itemQualities;
Object[] itemTimeStamps;
int idx;
if (!started) {
maxItems = 1;
opcItemIDs = new String[maxItems+1];
clientHandles = new int[maxItems+1];
readWriteHandles = new int[maxItems];
itemServerHandles = new int[1][maxItems];
itemServerErrors = new int[1][maxItems];
itemValues = new Object[1][maxItems];
itemQualities = new Object[maxItems];
itemTimeStamps = new Object[maxItems];
// Create connection to the OPC server
testServer = new OPCServer();
testServer.connect("Matrikon.OPC.Simulation.1", null);
// Create a group to contain the tag
testGroupCollection = testServer.getOPCGroups();
group1 = testGroupCollection.add("group1");
group1.setClientHandle(100);
group1.setUpdateRate(1000);
itemCollection1 = group1.getOPCItems();
itemCollection1.setDefaultAccessPath("");
// Add the tag
for(idx=1; idx<maxItems+1; idx++) {
clientHandles[idx] = idx;
opcItemIDs[idx] = ItemTag;
}
itemCollection1.addItems(maxItems, opcItemIDs, clientHandles, itemServerHandles, itemServerErrors, requestedDataTypes, accessPaths);
System.out.println("Success");
}
} catch (Exception e){
e.printStackTrace();
} finally {
com.linar.jintegra.Cleaner.releaseAll();
}
}
} |