我在java(使用NetBean)中创建了一个小程序,它使用libnodave库libnodave-java.0.1每500ms读取一个字节(Interval)。我对阅读操作持续时间感到失望,大约250ms(dt)。
这是我的主要课程。如您所见,连接请求仅在第9行执行一次:
private int area;
private long secondo=0;
public MachCtrl() {
initComponents();
Mradio.setSelected(true);
db.setText("0");
db.setEnabled(false);
area=Nodave.FLAGS;
DataIsoTCP.Start("172.17.5.31");
ActionListener listener = new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt) {
long start, elapsedTime;
if(type.getSelectedItem().toString()=="float"){
start = System.nanoTime();
float r=(float)DataIsoTCP.Read(area, type.getSelectedItem().toString(), Integer.parseInt(db.getText()), Integer.parseInt(address.getText()), Integer.parseInt(bit.getSelectedItem().toString()));
elapsedTime = System.nanoTime() - start;
value.setText(Float.toString(r));
dt.setText(Long.toString(Math.round(elapsedTime/1000000)));
}
if(type.getSelectedItem().toString()=="double"){
start = System.nanoTime();
long r=(long)DataIsoTCP.Read(area, type.getSelectedItem().toString(), Integer.parseInt(db.getText()), Integer.parseInt(address.getText()), Integer.parseInt(bit.getSelectedItem().toString()));
elapsedTime = System.nanoTime() - start;
value.setText(Long.toString(r));
dt.setText(Long.toString(Math.round(elapsedTime/1000000)));
}
if(type.getSelectedItem().toString()=="word"){
start = System.nanoTime();
long r=(long)DataIsoTCP.Read(area, type.getSelectedItem().toString(), Integer.parseInt(db.getText()), Integer.parseInt(address.getText()), Integer.parseInt(bit.getSelectedItem().toString()));
elapsedTime = System.nanoTime() - start;
value.setText(Long.toString(r));
dt.setText(Long.toString(Math.round(elapsedTime/1000000)));
}
if(type.getSelectedItem().toString()=="byte"){
start = System.nanoTime();
byte r=(byte)DataIsoTCP.Read(area, type.getSelectedItem().toString(), Integer.parseInt(db.getText()), Integer.parseInt(address.getText()), Integer.parseInt(bit.getSelectedItem().toString()));
elapsedTime = System.nanoTime() - start;
value.setText(Integer.toString(r));
dt.setText(Long.toString(Math.round(elapsedTime/1000000)));
}
if(type.getSelectedItem().toString()=="bit"){
start = System.nanoTime();
byte r=(byte)DataIsoTCP.Read(area, type.getSelectedItem().toString(), Integer.parseInt(db.getText()), Integer.parseInt(address.getText()), Integer.parseInt(bit.getSelectedItem().toString()));
elapsedTime = System.nanoTime() - start;
value.setText(Integer.toString(r));
dt.setText(Long.toString(Math.round(elapsedTime/1000000)));
}
interval.setText(Long.toString((System.nanoTime() - secondo)/1000000));
secondo = System.nanoTime();
}
};
Timer timert = new Timer(500,listener);
timert.start();
}
这是DataIsoTCP类,它包含read函数。正如你所看到的只是一个" readBytes()"请求,没有可能延迟代码执行的繁重操作。
package examples;
import PLC.Nodave;
import PLC.PLCinterface;
import PLC.TCPConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class DataIsoTCP {
public static boolean Connection = false;
private static int i, j;
public static byte b, b1, b2, b3;
public static long a, c;
//private static float d, e, f;
private static char buf[];
public static byte buf1[];
public static PLCinterface di;
public static TCPConnection dc;
public static Socket sock;
private static int slot;
public static byte[] by;
public static String IP;
DataIsoTCP(String host) {
IP = host;
//Nodave.Debug=Nodave.DEBUG_ALL;
buf = new char[Nodave.OrderCodeSize];
buf1 = new byte[Nodave.PartnerListSize];
try {
sock = new Socket(host, 102);
} catch (IOException e) {
System.out.println(e);
}
}
public static void Start(String adres) {
Nodave.Debug=Nodave.DEBUG_ALL^(Nodave.DEBUG_IFACE|Nodave.DEBUG_SPECIALCHARS);
DataIsoTCP tp = new DataIsoTCP(adres);
DataIsoTCP.StartConnection();
}
public static void StartConnection() {
Connection = false;
OutputStream oStream = null;
InputStream iStream = null;
slot = 2;
if (sock != null) {
try {
oStream = sock.getOutputStream();
} catch (IOException e) {
System.out.println(e);
}
try {
iStream = sock.getInputStream();
} catch (IOException e) {
System.out.println(e);
}
di = new PLCinterface(
oStream,
iStream,
"IF1",
0,
Nodave.PROTOCOL_ISOTCP);
dc = new TCPConnection(di, 0, slot);
int res = dc.connectPLC();
if (0 == res) {
Connection = true;
System.out.println("Connection OK ");
} else {
System.out.println("No connection");
}
}
}
public static void StopConnection() {
if (Connection == true) {
Connection = false;
dc.disconnectPLC();
di.disconnectAdapter();
}
}
public static float Read(int area, String type, int db, int address, int bit) {
int bytes;
if("float".equals(type)){
float r=0;
bytes=4;
int res = dc.readBytes(area, db, address, bytes, null);
if(res==0){
r=dc.getFloat();
}
return r;
}
else if("double".equals(type)){
float r=0;
bytes=4;
int res = dc.readBytes(area, db, address, bytes, null);
if(res==0){
r=dc.getU32();
}
return r;
}
else if("word".equals(type)){
int r=0;
bytes=2;
int res = dc.readBytes(area, db, address, bytes, null);
if(res==0){
r=dc.getWORD();
}
return r;
}
else if("byte".equals(type)){
int r=0;
bytes=1;
int res = dc.readBytes(area, db, address, bytes, null);
if(res==0){
r=dc.getBYTE();
}
return r;
}
else if("bit".equals(type)){
int r=0;
bytes=1;
int res = dc.readBytes(area, db, address, bytes, null);
if(res==0){
r=dc.getBYTE();
}
return (r >> bit)&1;
}else{
return 0;
}
}
}
我需要至少每250ms读取一次数据,然后读取操作的持续时间应减半。有人设法加快这项行动吗? 谢谢您的帮助! 斯特凡诺
答案 0 :(得分:0)
我也试过VB库,结果相同。我试过PLCSIM和IM151-8。结果是一样的。我也尝试过模拟CPU315和CPU317而没有成功。在联系西门子支持后,它似乎是PLC响应延迟,由PLC循环操作引入(可能用于保护西门子产品)。这个答案并没有让我满意,所以我会尝试不同的方法让PLC快速响应。