📄 opcconnector.java
字号:
{
conn.writeHeader(String.valueOf(ig.index));
}
}
String hdr = conn.readHeader();
if (getResultCode(hdr) != 0)
{
conn.close();
throw new OpcException(hdr);
}
conn.close();
}
/**
* 刷新所有确定了index值的变量组上的变量值
*/
public void refreshGroups() throws IOException
{
TcpConnection conn = getConnection();
conn.writeHeader("RF" + DELIMETER + Integer.toString(3));//3=事务码
ItemGroup ig;
for (int groupIndex = 0, cntGroups = groups.size(); groupIndex < cntGroups; groupIndex++)
{
ig = (ItemGroup)groups.get(groupIndex);
if (ig.index != -1)
{
conn.writeHeader(Integer.toString(ig.index));
}
}
//conn.flushHeanders();
String hdr = conn.readHeader();
if (getResultCode(hdr) != 0)
{
conn.close();
throw new OpcException(hdr);
}
conn.close();
}
public boolean isConnected()
{
return connected;
}
/**
* 关闭到OPC服务的所有连接,释放资源
*/
public void disconnect()
{
if (socketRcvUDP != null)
{
socketRcvUDP.close();
socketRcvUDP = null;
}
if (!connected)
{
return;
}
int cntGroups = groups.size();
if (connected && cntGroups > 0)
{
try
{
TcpConnection conn = getConnection();
conn.writeHeader("RM" + DELIMETER + cntGroups);
ItemGroup ig;
for (int groupIndex = cntGroups - 1; groupIndex >= 0; groupIndex--)
{
ig = (ItemGroup)groups.get(groupIndex);
conn.writeHeader(Integer.toString(ig.index));
ig.index = -1;
}
//conn.flushHeanders();
//debug("disconnect " + conn.readHeader());
conn.close();
connected = false;
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
/**
* 确保释放资源
*/
protected void finalize()
{
this.disconnect();
}
/**
* 获得加入的各个组构成集合上的某个组
* @param index 索引号
* @return ItemGroup对象。如果指定索引无效,则抛出异常ArrayIndexOutOfBoundsException。
*/
public final ItemGroup getGroup(int index)
{
return (ItemGroup)groups.get(index);
}
/**
* 变量值发生变化通知的处理
* 遵循2.2版本协议处理
*/
void dataChanged(byte[] data, int offset, int length)
{
if (length < 16) return;//should never happen//20=4+2+2+4+1+(2+1)
int end = offset + length;
offset += 4;//跳过事务号
int hGroup;
//XXX 严格意义上,应该 & 0xFFFF,按考虑到hGroup的值不会超过32512,故省去
ItemGroup ig = groupAt(hGroup = ((data[offset++] << 8) | data[offset++]));
if (ig == null)
{//should never happen
System.err.println("Caution: groupIndex " + hGroup + " is illegal.");
return;
}
//XXX 严格意义上,应该 & 0xFFFF,按考虑到itemCount的值不会超过32512,故省去
int itemCount = ((data[offset++] << 8) | data[offset++]);
boolean hasError = ((data[offset++] << 24) | (data[offset++] << 16)
| (data[offset++] << 8) | data[offset++]) != 0;
boolean notAllGood = data[offset++] != 0;
ig.ensureCapacity(itemCount);
Item[] itemsChanged = ig.cachedActiveItems;
Variant[] oldValues = ig.cachedOldValues;
Item item;
int cntValueBytes;
for (int i = 0; offset < end && i < itemCount; i++)
{
//XXX 严格意义上,应该 & 0xFFFF,但考虑到data[offset++]<<8的值不会超过32512,故省去
itemsChanged[i] = item = ig.getItem(((data[offset++] << 8) | data[offset++]));
//XXX 严格意义上,应该 & 0xFF,但考虑到目前valueBytes的值不会超出127,故省去
cntValueBytes = data[offset++];
oldValues[i] = item.setValue(data, offset);
offset += cntValueBytes;
if (notAllGood)
{
item.quality = (short)(data[offset++] & 0xFF);
debug(item.getID() + "'quality=" + Item.getQualityText(item.quality));
}
if (hasError) offset += 4;//忽略对变量值错误的处理
}
ig.fireDataChanged(new CallbackEvent(ig, itemCount));
}
/**
* 异步写入操作执行完成通知的处理
* 遵循2.3版本协议处理
*/
void writeCompleted(byte[] data, int offset, int length)
{
if (length < 12) return;//should never happen//20=4+2+2+4
int end = offset + length;
offset += 4;//跳过事务号
int hGroup;
//XXX 严格意义上,应该 & 0xFFFF,按考虑到hGroup的值不会超过32512,故省去
ItemGroup ig = groupAt(hGroup = ((data[offset++] << 8) | data[offset++]));
if (ig == null)
{//should never happen
System.err.println("Caution: groupIndex " + hGroup + " is illegal.");
return;
}
//XXX 严格意义上,应该 & 0xFFFF,按考虑到itemCount的值不会超过32512,故省去
int itemCount = ((data[offset++] << 8) | data[offset++]);
boolean hasError = ((data[offset++] << 24) | (data[offset++] << 16)
| (data[offset++] << 8) | data[offset++]) != 0;
ig.ensureCapacity(itemCount);
Item[] items = ig.cachedActiveItems;
for (int i = 0; offset < end && i < itemCount; i++)
{
//XXX 严格意义上,应该 & 0xFFFF,但考虑到data[offset++]<<8的值不会超过32512,故省去
items[i] = ig.getItem(((data[offset++] << 8) | data[offset++]));
if (hasError) offset += 4;//忽略对变量值错误的处理
}
//ig.fireWriteComplete(new CallbackEvent(ig, itemCount));
}
/**
* 异步取消操作执行完成通知的处理
* 遵循2.3版本协议处理
*/
void cancelCompleted(byte[] data, int offset, int length)
{
if (length < 6) return;//should never happen//20=4+2
int end = offset + length;
offset += 4;//跳过事务号
int hGroup;
//XXX 严格意义上,应该 & 0xFFFF,按考虑到hGroup的值不会超过32512,故省去
ItemGroup ig = groupAt(hGroup = ((data[offset++] << 8) | data[offset++]));
if (ig == null)
{//should never happen
System.err.println("Caution: groupIndex " + hGroup + " is illegal.");
return;
}
//ig.fireCancelComplete(new CallbackEvent(ig));
}
void debug(String msg)
{
System.out.println(msg);
}
/**
*
* 根据OPC控制中心提供的变量组索引码查找对应的ItemGroup对象
* @return 如果没有找到,则返回null,否则返回非负数
*/
public final ItemGroup groupAt(int groupIndex)
{
List groups = this.groups;
ItemGroup ig;
if (groups.size() > groupIndex)//多数情况下groupIndex就是数组下标
{
for (int i = groupIndex; i >= 0; i--)
{
ig = (ItemGroup)groups.get(i);
if (ig.index == groupIndex)
{
return ig;
}
}
}
for (int i = groups.size() - 1; i > groupIndex; i--)
{
ig = (ItemGroup)groups.get(i);
if (ig.index == groupIndex)
{
return ig;
}
}
return null;
}
/**
*
* 根据组的ID查找它的ItemGroup对象在加入的组集合上的索引
* @return 如果没有找到,则返回-1,否则返回非负数
*/
public final int indexOfGroup(String groupId)
{
int index = -1;
List groups = this.groups;
for (int i = groups.size() - 1; i >= 0; i--)
{
if (((ItemGroup)groups.get(i)).getName().equals(groupId))
{
index = i;
break;
}
}
return index;
}
/**
* 根据组的ID查找它的ItemGroup对象
* @return 如果没有找到,则返回null
*/
public ItemGroup findGroup(String groupId)
{
int index = indexOfGroup(groupId);
return index != -1?getGroup(index):null;
}
/**
* CommandSent实现
*/
public void request(java.util.List headerStrings) throws IOException
{
if (!connected)
{
throw new OpcException("Has't connected to OpcCtrl");
}
TcpConnection conn = this.getConnection();
try
{
for (int i = 0, n = headerStrings.size(); i < n; i++)
{
//System.out.println(" ." + headerStrings.get(i));
conn.writeHeader((String)headerStrings.get(i));
}
//conn.flushHeanders();
String response = conn.readHeader();
if (getResultCode(response) != 0)
{
throw new OpcException(response);
}
}
finally
{
conn.close();
}
}
public int getPortRequest()
{
return portRequest;
}
/**
*
* @param portRequest OPC控制中心上的外部请求监听端口
*/
public void setPortRequest(int portRequest)
{
this.portRequest = portRequest;
}
/**
*
* @return OPC控制中心所在机器的IP地址
*/
public InetAddress getHostRequest()
{
return hostRequest;
}
public void setHostRequest(InetAddress hostRequest)
{
this.hostRequest = hostRequest;
}
private int getResultCode(String firstReponseLine)
{
int indexDelim = firstReponseLine != null?firstReponseLine.indexOf(DELIMETER):-1;
return indexDelim != -1?Integer.parseInt(firstReponseLine.substring(0, indexDelim)):Integer.MIN_VALUE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -