廖井涛
2024-03-04 eae17d27ec56a6b7887f5597335e38ca40273ef4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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);     
   
 
}