项目中需要与中控的软件链接并实时的读取出数据库中的值。但是实验室的很多算法都是用matlab写的,但是,中控给出的只有java和c的接口,因此首先得写一个中间链接数据和程序的小程序,使得数据可以实时的流入我们的程序。
基本的过程是这样的:
这里主要说一下java链接中控和matlab中间那一块。这里主要需要中控主机的ip地址,软件的用户名,密码,以及需要检索的时间,还的提供需要检测的测点。根据这些要求有如下啊的草图设计
最终设计的结果是这样的,用的是java的swing,第一次用,确实丑的厉害!
不过基本的功能都是实现了,运行良好。
其中关键代码如下:
package com.hemin.util;
import com.supconsoft.plantwrapex.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class JAVAconn {
//程序入口
public static void main(String[] args) throws InterruptedException{
String ip="219.226.86.149";
String username="administrator";
String passwd="supcon";
String timedely = "1";
List<String> taglist=new ArrayList<>();
taglist.add("Tag35001.MV");
boolean check_conn = ConnZK(ip,username , passwd , timedely , taglist );
if(check_conn)
System.out.println("ok");
else{
System.out.println("false");
}
}
//链接中控
public static boolean ConnZK(String ip,String username ,String passwd , String timedely ,List<String> taglist ) throws InterruptedException{
//循环一直发送数据到指定端口
while(true){
IConnection conn=null;
String url = ip+":5150";
ITag tag = null;
try{
String cur_time = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
conn = ConnectionCreator.getConnection(url);
conn.login(username, passwd);
for (String str : taglist) {
tag = conn.getTagFactory().findTag(str);
readValues( cur_time , tag ,timedely);
}
}catch(PlantWrapException e){
e.printStackTrace();
return false;
}finally {
conn.close();
}
try {
Thread.sleep(Integer.parseInt(timedely)*1000);
} catch (NumberFormatException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}
//readValues
public static void readValues(String cur_time ,ITag tag ,String timedely) throws InterruptedException{
Calendar calendar = Calendar.getInstance();
Date tEnd = new Date(cur_time);
calendar.setTime(tEnd);
int time = 0-Integer.parseInt(timedely);
calendar.add(Calendar.MINUTE, time);
Date tStart = calendar.getTime();
try {
TagValue[] vals = tag.readDiskValue(tStart, tEnd, BoundOption.Bound_None);
for (int i = 0; i < vals.length; i++)
{
//此处应发送
sendInfo(vals[i].getValue().toString());
String hour=String.format("%tT", vals[i].getTimeStamp());
String date=String.format("%tF", vals[i].getTimeStamp());
System.out.printf(" 时间:\t%s\t%s\tDiskValue:\t%s\r\n",date,hour, vals[i].getValue().toString());
}
} catch (PlantWrapException e) {
e.printStackTrace();
}
}
//发送数据
public static void sendInfo(String sb) throws InterruptedException{
try {
ServerSocket serverSocket = new ServerSocket(9995);
Socket socket = serverSocket.accept();
// 读取客户端数据
InputStream info = socket.getInputStream();
DataInputStream dis = new DataInputStream(info);
System.out.println(dis.readUTF());
// 向客户端输出数据
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(sb.toString());
dos.flush();
// dos.writeUTF("Hello!");
// dos.flush();
serverSocket.close();
dos.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
Thread.sleep(500);
///
}
}
这是用来连接 的,
package com.hemin.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class killPro {
public static void clear() {
String port = "9999";
List<String > list = new ArrayList<>();
String pid=null;
Runtime run =Runtime.getRuntime();
Runtime run2 =Runtime.getRuntime();
Process pro = null;
Process pro2 = null;
//查询端口的pid
String commend = "cmd /c netstat -ano |findstr "+port;
try {
pro = run.exec(commend);
BufferedReader bf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
String line = null;
while((line = bf.readLine())!=null){
//System.out.println(line);
list.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
pro.destroy();
}
//判断是否存在指定端口
if(list.size()==0){
System.out.println("指定端口没有被使用!");
}else{
for (String str : list) {
String ip = str.split(" +")[2];
int beg = ip.lastIndexOf(':')+1;
int end = ip.length();
String destport = ip.substring(beg, end);
if(destport.equals(port)){
System.out.println(str.split(" +")[5]);
pid = str.split(" +")[5];
break;
}
}
if(pid ==null){
System.out.println("没有对应端口没有被使用!");
}else{
//kill port
String command1 ="cmd /c taskkill /pid "+pid+" /F";
try {
pro2 = run2.exec(command1);
} catch (IOException e) {
e.printStackTrace();
}finally {
System.out.println("以杀掉占用程序");
pro.destroy();
}
}
}
}
}
这是用来清除端口的,上次这个代码已经说过了,就不再说了!