1. 在milo中,处理WORD等数据类型
例子如下:
VariableNode node = client.getAddressSpace().createVariableNode(
new NodeId(2, "ch1.d1.tag1")); Variant newVal = new Variant(Unsigned.ushort(111));
DataValue va = new DataValue(newVal, null, null);
StatusCode writeStatus = node.writeValue(va).get(); 也可以用UaOpcClient client.writeValue(
new NodeId(2, "ch1.d1.tag1"),
va
).get();
Variant 是milo中处理VARIANT变量的类,milo在Identifiers类中定义了各种类型的初始化如WORD LONG等,这里的Unsigned.ushort(111)就是将111初始化为WORD
2. 在utgard中,处理各种类型
(1)写入数据:
在utgard中采用jinterop中的JIVariant类型进行各种类型的处理,包括boolean, floate, double, short, int等,对boolean可处理如下:
Group group = server.addGroup();
Item item = group.addItem(mitem); final JIVariant value = new JIVariant(val);
item.write(value);
其中val是boolean型
(3)写入double和float数组数据:
当写入double或float的数组数据时,因JIArray不支持基本类型数组,所以必须使用Double和Float对象进行处理,如下:
server.connect(); Group group = server.addGroup();
Item item = group.addItem("Square Waves.Real4"); final Float[] Data = new Float[] { 1202f, 1203f, 1204f }; //这里没有使用float基本类型,使用了对象Float,如果是double,则需要使用Double类
final JIArray array = new JIArray(Data, false);
final JIVariant value = new JIVariant(array); item.write(value);
(4)写入无符号数据:
java基本数据类型中没有无符号数据,所以需要使用JInterop中的无符号数类来生成JIUnsignedShort、JIUnsignedByte、JIUnsignedInt类型。
Group group = server.addGroup();
Item item = group.addItem(itemId);
JIUnsignedShort n5 = (JIUnsignedShort) JIUnsignedFactory.getUnsigned(5, JIFlags.FLAG_REPRESENTATION_UNSIGNED_SHORT);//JIUnsignedByte、JIUnsignedInt类型处理相同
final JIVariant value = new JIVariant(n5);
item.write(value);
Thread.sleep(2000);
(5) 写入无符号数组数据:
Group group = server.addGroup();
Item item = group.addItem(itemId);
// wait a little bit 有个10秒延时
JIUnsignedShort n1 = (JIUnsignedShort) JIUnsignedFactory.getUnsigned(5, JIFlags.FLAG_REPRESENTATION_UNSIGNED_SHORT);
JIUnsignedShort n2 = (JIUnsignedShort) JIUnsignedFactory.getUnsigned(5, JIFlags.FLAG_REPRESENTATION_UNSIGNED_SHORT);
JIUnsignedShort n3 = (JIUnsignedShort) JIUnsignedFactory.getUnsigned(5, JIFlags.FLAG_REPRESENTATION_UNSIGNED_SHORT);
JIUnsignedShort n4 = (JIUnsignedShort) JIUnsignedFactory.getUnsigned(5, JIFlags.FLAG_REPRESENTATION_UNSIGNED_SHORT);
JIUnsignedShort n5 = (JIUnsignedShort) JIUnsignedFactory.getUnsigned(5, JIFlags.FLAG_REPRESENTATION_UNSIGNED_SHORT);
final JIUnsignedShort[] dData = {n5, n4, n3, n2, n1 };
final JIArray array = new JIArray(dData, true);
final JIVariant value = new JIVariant(array);
item.write(value);
Thread.sleep(2000);
(6) 读出数据:
同样,读出的数据状态是ItemState, 它通过getValue得到一个JIVariant类型的值,这时需要进行分类处理:
int type = 0;
try {
type = itemState.getValue().getType();
} catch (JIException e) {
e.printStackTrace();
} // 如果读到是short类型的值ֵ
if (type == JIVariant.VT_I2) {
short n = 0;
try {
n = itemState.getValue().getObjectAsShort();
} catch (JIException e) {
e.printStackTrace();
}
System.out.println("-----short类型 " + n);
} //如果读到是字符串类型的值ֵ
if(type == JIVariant.VT_BSTR) {
JIString value = null;
try {
value = itemState.getValue().getObjectAsString();
} catch (JIException e) {
e.printStackTrace();
} //
String str = value.getString();
System.out.println("-----String类型 " + str);
}
}
//如果是无符号整数(WORD,DWORD)这里WORD对对应JIVariant.VT_UI2, DWORD对应JIVariant.VT_UI4 if(type == JIVariant.VT_UI2) {
Number value = 0;
try {
value =itemState.getValue().getObjectAsUnsigned().getValue();//这里生成一个Number类型的数
} catch (JIException e) {
e.printStackTrace();
} // System.out.println("-----无符号类型 " + value );
}
}
这里WORD对对应JIVariant.VT_UI2, DWORD对应JIVariant.VT_UI4,BYTE对应JIVariant.VT_UI1,都可以用Number类取出
但是在取QWORD(四字节)时出错,这个问题还没有办法解决。
(4) 读出数组数据
Group group = server.addGroup("group");
//Item item = group.addItem(itemId);
Map<String, Item> item_map = group.addItems( "ch1.d1.tag8"); //这是一个双字数组WORD ARRAY
Set<String> keySet = item_map.keySet();
//遍历存放所有key的Set集合
for(int i=0; i<6; i++) {
Iterator<String> it = keySet.iterator();
while(it.hasNext())
{
String key = it.next();
Item item = item_map.get(key);
ItemState itemState = item.read(true);
int type = itemState.getValue().getType(); if(type > 0x2000 && type < 0x3000)
{
int etype = type - 0x2000;
JIVariant val = itemState.getValue();
JIArray array = val.getObjectAsArray();
int d = array.getDimensions();
int upbounds[] = array.getUpperBounds(); if(etype == JIVariant.VT_UI2) {//如果是字节则用VT_UI1,双字用VT_UI4, 可以用switch case @SuppressWarnings("rawtypes")
Class clazz = array.getArrayClass();
Object ob = array.getArrayInstance(); JIUnsignedShort[] num = (JIUnsignedShort[])ob; //将Object强制转换为JIUnsignedShort数组,如果是字节则用JIUnsignedByte,双字用JIUnsignedInteger System.out.println("first element is :" + num[0].getValue()); //这里getValue返回一个Number类型,可以调用Number的成员函数将数据转换成其他类型
}
} else if(etype == JIVariant.VT_R4) { //JIVariant.VT_R4对应float,JIVariant.VT_R8对应double,两种处理方法一致 @SuppressWarnings("rawtypes")
Class clazz = array.getArrayClass();
Object ob = array.getArrayInstance(); Number[] num = (Number[])ob; //将Object强制转换为Number数组,
System.out.println("first element is :" + num[3]); }