📄 opctest.java
字号:
package com.zcsoft.opc;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
import java.util.Random;
class OpcTest implements CallbackListener //implements Runnable
{
OpcConnector opc;
static final String GROUP_NAME = "testGroup";
//
// static final String[] items = {
// "DB101,DWORD4,1",
// "DB101,DWORD8,1",
// "DB101,DWORD20,1"
// };
// static final Variant[] vt = {new Variant(Variant.VT_I4), new Variant(Variant.VT_I4), new Variant(Variant.VT_I4)};
static final String[] items = {
"DB10,B1,1",
"DB10,B2,1",
"DB10,W3,1",
};
static final Variant[] vt = {new Variant(), new Variant(), new Variant(Variant.VT_UI2)};
OpcTest(OpcConnector opc) throws IOException
{
this.opc = opc;
opc.setHostRequest(java.net.InetAddress.getByName("192.168.0.198"));
// opc.setTopic("S7C");
opc.setTopic("DEMO");
opc.setProtocol("S7");
// new Thread(this).start();
}
void activeGroup(boolean a)
{
//opc.setGroupActive(GROUP_NAME, a);
}
void addGroups()
{
ItemGroup group = new ItemGroup(GROUP_NAME);
group.setUpdateRate(500);
Item item;
try
{
for (int i = 0; i < items.length; i++)
{
item = new Item(items[i], vt[i]);
group.addItem(item);
}
}
catch (DuplicatedException ex)
{
}
opc.addGroup(group);
group.addCallbackListener(this);
}
void connect() throws IOException
{
opc.connect();
}
void disconnect()
{
opc.disconnect();
}
void asyncReadAllItems() throws IOException
{
//opc.asyncReadItems(GROUP_NAME);
}
void asyncWriteItemValue() throws IOException
{
}
Random random = new Random();
void readItemValue() throws IOException
{
String itemName = items[Math.abs(random.nextInt()%items.length)];
//Item item = opc.syncReadItem(GROUP_NAME, itemName);
//System.out.println(itemName + "'value is " + item.getValue());
}
void writeItemValue() throws IOException
{
int index = Math.abs(random.nextInt()%items.length);
String itemName = items[index];
Variant value = vt[index];
value.iValue = (random.nextInt());
opc.getGroup(0).syncWrite(new int[]{index}, 1, new Variant[]{value});
//System.out.println("syncWrite " + itemName + " with value " + value);
}
public static void main(String[] args)
{
try
{
if(args.length < 1)
{
testUnderDosMode();
}
else
{
testUnderGUIMode();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public void run()
{
byte[] bytes = new byte[50];
boolean active = false;
while (true)
{
try
{
int len = System.in.read(bytes);
if (len > 0)
{
byte command = bytes[0];
if (command == 'q' || command == 'Q')
{
disconnect();
break;
}
else if (command == 'r')
{
readItemValue();
}
else if (command == 'w')
{
writeItemValue();
}
else if (command == 'a' && len > 1 && bytes[1] == 'w')
{
asyncWriteItemValue();
}
else if (command == 'a' && len > 1 && bytes[1] == 'r')
{
asyncReadAllItems();
}
else if (command == 'a')
{
activeGroup(active = !active);
}
}
}
catch (Exception ex)
{
//disconnect();
ex.printStackTrace();
}
}
//disconnect();
}
static void testUnderDosMode() throws IOException
{
OpcTest ot = new OpcTest(new OpcConnector());
ot.addGroups();
ot.connect();
ot.run();
// ot.asyncReadAllItems();
// ot.readItemValue();
// ot.writeItemValue();
// ot.disconnect();
// ot.unmount();
}
static void testUnderGUIMode() throws IOException
{
final JButton
cmdConnect = new JButton(),
cmdDisconnect = new JButton(),
cmdAsyncRead = new JButton(),
cmdSyncRead = new JButton(),
cmdSyncWrite = new JButton();
final JCheckBox chkActive = new JCheckBox("Active");
final OpcTest ot = new OpcTest(new OpcConnector());
JPanel testPanel = new JPanel(new BorderLayout());
JPanel commandsPane = new JPanel();
cmdConnect.setAction(new AbstractAction("Connect"){
public void actionPerformed(ActionEvent e)
{
try
{
ot.connect();
}
catch (IOException ex)
{
ex.printStackTrace();
}
cmdDisconnect.setEnabled(true);
this.setEnabled(false);
}
});
chkActive.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
ot.activeGroup(chkActive.isSelected());
}
});
cmdAsyncRead.setAction(new AbstractAction("AsyncRead"){
public void actionPerformed(ActionEvent e)
{
try
{
ot.asyncReadAllItems();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
});
cmdSyncRead.setAction(new AbstractAction("SyncRead"){
public void actionPerformed(ActionEvent e)
{
try
{
ot.readItemValue();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
});
cmdSyncWrite.setAction(new AbstractAction("SyncWrite"){
public void actionPerformed(ActionEvent e)
{
try
{
ot.writeItemValue();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
});
cmdDisconnect.setAction(new AbstractAction("Disconnect"){
public void actionPerformed(ActionEvent e)
{
ot.disconnect();
cmdConnect.setEnabled(true);
//cmdAddGroup.setEnabled(true);
this.setEnabled(false);
}
});
cmdDisconnect.setEnabled(false);
commandsPane.add(cmdConnect);
commandsPane.add(chkActive);
commandsPane.add(cmdAsyncRead);
commandsPane.add(cmdSyncRead);
commandsPane.add(cmdSyncWrite);
commandsPane.add(cmdDisconnect);
testPanel.add(commandsPane);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(testPanel);
f.pack();
f.setLocation(450, 350);
f.show();
}
public void dataChanged(CallbackEvent e)
{
System.out.println(e.getCntItems() + " items' value changed");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -