package ng.devices;
|
|
import java.sql.SQLException;
|
|
import ng.db.DBHelper;
|
import ng.db.DBSession;
|
import ng.db.DBSession.StdCallResult;
|
|
|
public abstract class ModbusService implements Runnable {
|
|
//modbus客户端
|
protected ModbusClient client;
|
//自动读数据地址(字节为单位,目前是0)
|
protected int readOffset;
|
//自动读取数据长度(字节为单位,可超长,目前用50,最大250)
|
protected int readSize;
|
//设备ID
|
protected int machineID;
|
//连接类型
|
protected byte functionType;
|
//线程运行状态
|
protected boolean running;
|
|
//设备IP
|
protected String ip;
|
//设备端口
|
protected int port;
|
//线程
|
java.lang.Thread thread;
|
//缓冲区(服务允许发送超过协议要求的数据,所以缓冲区更大)
|
byte[] buff=new byte[10000];
|
|
|
|
public String LastReturn;
|
|
//关闭线程
|
public void close(){
|
this.running=false;
|
}
|
|
//运行
|
public void Run(int MachineID,String ip,int port,byte state,int ReadOffset,int ReadSize,int timeout,byte FunctionType){
|
//设置参数
|
this.machineID=MachineID;
|
this.ip=ip;
|
this.port=port;
|
this.readOffset=ReadOffset;
|
this.readSize=ReadSize;
|
this.functionType= FunctionType;
|
this.client=new ModbusClient();
|
//启动线程
|
this.client.setConnectionParam(MachineID,ip,port,(byte)state,timeout);
|
|
thread=new java.lang.Thread(this);
|
thread.start();
|
}
|
|
|
//一次读取行为
|
String once(){
|
//按照配置读数据
|
String msg= client.read(this.readOffset,this.readSize,buff,0,this.functionType);
|
this.LastReturn=msg;
|
if(msg=="ok"){
|
try {
|
//请求打包
|
ModbusDataPackage pack=new ModbusDataPackage();
|
pack.ByteSize=this.readSize;
|
pack.Address=this.readOffset;
|
pack.Content=buff;
|
ModbusDataPackage rsp=new ModbusDataPackage();
|
//调用服务
|
if(this.Service(pack,rsp)){
|
//服务要求会数据,则会
|
if(rsp.Content!=null){
|
return client.write(rsp.Address,rsp.ByteSize,rsp.Content,0);
|
}
|
}
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
finally{
|
}
|
|
|
}
|
return msg;
|
}
|
|
public void SendPLC(int Address, int count, byte[] buffer, int offset) {
|
this.client.write(Address, count, buffer, offset);
|
}
|
public boolean isRunning(){
|
return this.running;
|
}
|
|
public boolean isOnLine(){
|
ModbusClient c=this.client;
|
if(this.client!=null)
|
return this.client.IsEnable();
|
return false;
|
}
|
|
protected int UpadateInterval=200;
|
|
//线程运行函数
|
void proc(){
|
running=true;
|
while(running){
|
if(client.IsEnable()){
|
try {
|
java.lang.Thread.sleep(UpadateInterval);
|
String back=once();
|
} catch (InterruptedException e) {
|
// TODO Auto-generated catch block
|
}
|
}
|
else{
|
client.connect();
|
if(client.IsEnable()==false){
|
try {
|
java.lang.Thread.sleep(4000);
|
} catch (InterruptedException e) {
|
// TODO Auto-generated catch block
|
}
|
}
|
// else{
|
// System.out.print("IO链接成功");
|
// }
|
}
|
}
|
this.running=false;
|
client.close();
|
|
}
|
|
//线程入口
|
@Override
|
public void run() {
|
// TODO Auto-generated method stub
|
proc();
|
}
|
public class ModbusDataPackage{
|
public int Address;
|
public int ByteSize;
|
public byte[] Content;
|
}
|
protected abstract boolean Service(ModbusDataPackage request,ModbusDataPackage response);
|
|
|
}
|