?? opcconnector.java
字號(hào):
{
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=事務(wù)碼
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;
}
/**
* 關(guān)閉到OPC服務(wù)的所有連接,釋放資源
*/
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();
}
/**
* 獲得加入的各個(gè)組構(gòu)成集合上的某個(gè)組
* @param index 索引號(hào)
* @return ItemGroup對(duì)象。如果指定索引無效,則拋出異常ArrayIndexOutOfBoundsException。
*/
public final ItemGroup getGroup(int index)
{
return (ItemGroup)groups.get(index);
}
/**
* 變量值發(fā)生變化通知的處理
* 遵循2.2版本協(xié)議處理
*/
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;//跳過事務(wù)號(hào)
int hGroup;
//XXX 嚴(yán)格意義上,應(yīng)該 & 0xFFFF,按考慮到hGroup的值不會(huì)超過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 嚴(yán)格意義上,應(yīng)該 & 0xFFFF,按考慮到itemCount的值不會(huì)超過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 嚴(yán)格意義上,應(yīng)該 & 0xFFFF,但考慮到data[offset++]<<8的值不會(huì)超過32512,故省去
itemsChanged[i] = item = ig.getItem(((data[offset++] << 8) | data[offset++]));
//XXX 嚴(yán)格意義上,應(yīng)該 & 0xFF,但考慮到目前valueBytes的值不會(huì)超出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;//忽略對(duì)變量值錯(cuò)誤的處理
}
ig.fireDataChanged(new CallbackEvent(ig, itemCount));
}
/**
* 異步寫入操作執(zhí)行完成通知的處理
* 遵循2.3版本協(xié)議處理
*/
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;//跳過事務(wù)號(hào)
int hGroup;
//XXX 嚴(yán)格意義上,應(yīng)該 & 0xFFFF,按考慮到hGroup的值不會(huì)超過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 嚴(yán)格意義上,應(yīng)該 & 0xFFFF,按考慮到itemCount的值不會(huì)超過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 嚴(yán)格意義上,應(yīng)該 & 0xFFFF,但考慮到data[offset++]<<8的值不會(huì)超過32512,故省去
items[i] = ig.getItem(((data[offset++] << 8) | data[offset++]));
if (hasError) offset += 4;//忽略對(duì)變量值錯(cuò)誤的處理
}
//ig.fireWriteComplete(new CallbackEvent(ig, itemCount));
}
/**
* 異步取消操作執(zhí)行完成通知的處理
* 遵循2.3版本協(xié)議處理
*/
void cancelCompleted(byte[] data, int offset, int length)
{
if (length < 6) return;//should never happen//20=4+2
int end = offset + length;
offset += 4;//跳過事務(wù)號(hào)
int hGroup;
//XXX 嚴(yán)格意義上,應(yīng)該 & 0xFFFF,按考慮到hGroup的值不會(huì)超過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);
}
/**
*
* 根據(jù)OPC控制中心提供的變量組索引碼查找對(duì)應(yīng)的ItemGroup對(duì)象
* @return 如果沒有找到,則返回null,否則返回非負(fù)數(shù)
*/
public final ItemGroup groupAt(int groupIndex)
{
List groups = this.groups;
ItemGroup ig;
if (groups.size() > groupIndex)//多數(shù)情況下groupIndex就是數(shù)組下標(biāo)
{
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;
}
/**
*
* 根據(jù)組的ID查找它的ItemGroup對(duì)象在加入的組集合上的索引
* @return 如果沒有找到,則返回-1,否則返回非負(fù)數(shù)
*/
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;
}
/**
* 根據(jù)組的ID查找它的ItemGroup對(duì)象
* @return 如果沒有找到,則返回null
*/
public ItemGroup findGroup(String groupId)
{
int index = indexOfGroup(groupId);
return index != -1?getGroup(index):null;
}
/**
* CommandSent實(shí)現(xiàn)
*/
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控制中心上的外部請(qǐng)求監(jiān)聽端口
*/
public void setPortRequest(int portRequest)
{
this.portRequest = portRequest;
}
/**
*
* @return OPC控制中心所在機(jī)器的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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -