廖井涛
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package ng.devices;
 
import ng.db.DBHelper;
import ng.db.DBSession;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class ModbusClient implements IoBlock {
 
    String name;        //Ãû³Æ
    byte mbstate;        //Õ¾ºÅ
    int timeout;        //µÈ´ý³¬Ê±Ê±¼ä
    Socket sock;        //Ì×½Ó×Ö
    int state=0;        //ÔËÐÐ״̬ 0£¨Î´Á¬½Ó£©  1£¨ÒÑÁ¬½Ó£©
    int machineID;
    String ip;            
    int port;
    byte[] wbuff;
    byte[] rbuff;
    String zhengzailianjie="ÕýÔÚÁ¬½Ó";
    String yilianjie="ÒÑÁ¬½Ó";
    short frame=0;        //Ö¡ºÅ
    
    
    public ModbusClient(){
        wbuff=new byte[500];
        rbuff=new byte[1024*10];
    }
    
    public byte getModbusState(){
        return mbstate;
    }
    
    public String getIP(){
        return ip;
    }
    
    public String getName(){
        return name;
    }
    
    public void setName(String name){
        this.name=name;
    }
    
    public int getPort(){
        return port;
    }
 
    public boolean isOpened(){
        return state==1;
    }
    
    
 
    
    @Override
    public String read(int Address, int count, byte[] buffer, int offset,byte command) {
        if(this.IsEnable()==false){
            return "ͨѶδʹÄÜ";
        }
        if(command==3 &&(Address%2!=0 || count%2!=0)){
            return "µØÖ·ºÍ¶ÁÈ¡ÊýÁ¿±ØÐëÊÇżÊý";
        }
        int ct=count;
        int tmp1=Address;
        int tmp2=0;
        int tmp=0;
        while(tmp2<ct){
            tmp=240;
            if(ct-tmp2<240){
                tmp=ct-tmp2;
            }
            String s=inner_read(tmp2+tmp1,tmp,buffer,offset+tmp2,command);
            if(s=="ok"){
                tmp2+=tmp;
            }
            else
                return s;
        }
        return "ok";
    }
    
    @Override
    public String write(int Address, int count, byte[] buffer, int offset) {
    
        if(this.IsEnable()==false){
            return "ͨѶδʹÄÜ";
        }
        if(Address%2!=0 || count%2!=0){
            return "µØÖ·ºÍ¶ÁÈ¡ÊýÁ¿±ØÐëÊÇżÊý";
        }
        int ct=count;
        int tmp1=Address;
        int tmp2=0;
        int tmp=0;
        while(tmp2<ct){
            tmp=240;
            if(ct-tmp2<240){
                tmp=ct-tmp2;
            }
            String s=inner_write(tmp2+tmp1,tmp,buffer,offset+tmp2);
            if(s=="ok"){
                tmp2+=tmp;
            }
            else
                return s;
        }
        return "ok";
        
    }
    
    String inner_read(int Address, int count, byte[] buffer, int offset,byte command) {
 
        if (this.IsEnable() == false) {
            return "ͨѶδÁ¬½Ó";
        }
        // TODO Auto-generated method stub
        int addr = Address;
        int ct = count;
 
            addr = Address / 2;
            ct = count / 2;
 
        frame++;
        wbuff[0] = (byte) ((frame & 0xff00) >> 8); // buff[0..1]Ö¡ºÅ
        wbuff[1] = (byte) (frame & 0xff);
        wbuff[2] = 0; //// buff[2..3] Ìî0
        wbuff[3] = 0;
        wbuff[4] = 0; // buff[4..5] ºóÐøÖ¡³¤ ×ܳ¤-6
        wbuff[5] = 6;
        wbuff[6] = this.mbstate; // Õ¾ºÅ
        wbuff[7] = command; // ¶Á¹¦ÄÜÂð
        wbuff[8] = (byte) ((addr & 0xff00) >> 8); // buff[8..9] µØÖ·
        wbuff[9] = (byte) (addr & 0xff);
        wbuff[10] = (byte) ((ct & 0xff00) >> 8); // buff[10..11] wordÊý¾ÝÊýÁ¿
        wbuff[11] = (byte) (ct & 0xff);
 
        try {
            // ÓÃÌ×½Ó×Ö·¢ËÍÊý¾Ý
            DataOutputStream out = new DataOutputStream(sock.getOutputStream());
            out.write(wbuff, 0, 12);
            out.flush();
            // ÓÃÌ×½Ó×Ö¶ÁÈ¡Êý¾Ý
            DataInputStream in = new DataInputStream(sock.getInputStream());
            int c = in.read(rbuff);
            boolean ok = false;
            if (c > 0) {
                // Åжϱ¨ÎÄÍ·ÓÐЧÐÔ
                String ret = this.compareHeader(wbuff, rbuff, c);
 
                if (ret != null)
                    return ret;
                switch (rbuff[7]) {
                    case 1:
 
                    case 3:
                        int size = Byte.toUnsignedInt(rbuff[8]);
                        if (rbuff[7]==3&&size != ct * 2) {
 
                            return "Êý¾Ý³¤¶ÈÓÐÎó";
                        }
                        for (int i = 0; i < size; i++) {
                            buffer[offset + i] = rbuff[9 + i];
                        }
                        return "ok";
                    case (byte) 0x83:
                        return "ÇëÇóµØÖ·³¬·¶Î§»ò¹¦ÄÜÂð²»Ö§³Ö";
                    default:
                        ;
                        return "ÊÕµ½Î´Öª¹¦ÄÜÂð";
                }
 
            }
 
        } catch (IOException e) {
            this.close();
            return "Io Error";
        }
        return "err";
    }
 
    //±È½ÏÖ¡Í·ÊÇ·ñÆ¥Åä
    String compareHeader(byte[] send,byte[] recv,int count){
        int step=0;
        if(count<9){
            return "½ÓÊÜÖ¡´íÎó(Ö¡Í·ÓÐÎó)";
        }
        if(send[0]!=recv[0] || send[1]!=recv[1]){
            return "Ö¡ºÅ²»¶ÔÓ¦";
        }
        if(send[2]!=0 || send[3]!=0 || recv[2]!=0 || send[3]!=0){
            return "½ÓÊÜÖ¡´íÎó(Ö¡Í·ÓÐÎó)";
        }
        
        return null;
    }
    
    
 
    public String inner_write(int Address, int count, byte[] buffer, int offset) {
        if(this.IsEnable()==false){
            return "ͨѶδÁ¬½Ó";
        }
        // TODO Auto-generated method stub
        int addr=Address/2;
        int ct=count/2;
        frame++;
        wbuff[0]=(byte)((frame&0xff00)>>8);  // buff[0..1]Ö¡ºÅ
        wbuff[1]=(byte)(frame&0xff);        
        wbuff[2]=0;                        //buff[2..3]   Ìî0
        wbuff[3]=0;
        wbuff[4]=0;                        //buff[4..5]  ºóÐøÖ¡³¤  ×ܳ¤-6
        wbuff[5]=(byte)(7+ct*2);
        wbuff[6]=this.mbstate;            //Õ¾ºÅ
        wbuff[7]=0x10;                   //¹¦ÄÜÂëд±£³Ö¼Ä´æÆ÷                    
        wbuff[8]=(byte)((addr&0xff00)>>8);            //buff[8..9] µØÖ·
        wbuff[9]=(byte)(addr&0xff);                    
        wbuff[10]=(byte)((ct&0xff00)>>8);            //buff[10..11]  wordÊý¾ÝÊýÁ¿
        wbuff[11]=(byte)(ct&0xff);
        wbuff[12]=(byte)(ct*2);                        //Êý¾ÝÓòbyteÊýÁ¿
        for(int i=0;i<ct*2;i++){                    //Ìî³äÊý¾ÝÓò
            wbuff[13+i]=buffer[i+offset];
        }
        try {
            //·¢ËÍ
            DataOutputStream out=new DataOutputStream(sock.getOutputStream());
            out.write(wbuff,0,13+ct*2);
            //¶ÁÓ¦´ðÖ¡
            DataInputStream in=new DataInputStream(sock.getInputStream());
            int c= in.read(rbuff);
            boolean ok=false;
            if(c>0){
                //Åжϱ¨ÎÄÍ·ÓÐЧÐÔ
                String ret=this.compareHeader(wbuff,rbuff,c);
                if(ret!=null)
                    return ret;
                
                    switch(rbuff[7]){
                    case 0x10:            
                        return "ok";        
                    case (byte)0x90:
                        return "дÊý¾Ý³ö´í:ͨ³£ÊǵØÖ·²»´æÔÚ";
                        default:;
                        return "δ֪ÃüÁîÂë";
                    }
                
            }
            else{
                //ÕâÖÖÇé¿ö²»»á´æÔÚ
                return "δ¶Áµ½ÈκÎÊý¾Ý";
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            this.close();
            //e.printStackTrace();
            return "Io Error";
        }
        
    }
    
 
    @Override
    public boolean IsEnable() {
        // TODO Auto-generated method stub
        return state==1;
    }
    
    //ÉèÖòÎÊý
    public void setConnectionParam(int MachineID,String IP,int Port,byte ModbusState,int Timeout){
        this.machineID=MachineID;
        this.mbstate=ModbusState;
         this.ip=IP;
         this.port=Port;
         this.mbstate=ModbusState;
         if(Timeout<10){
             Timeout=10;
         }
         this.timeout=Timeout;
    }
    //¹Ø±ÕÁ¬½Ó
    @Override
    public void close(){
        
        if(sock!=null){
            state=0;
            try {
                sock.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }
            sock=null;
        }
        state=0;
    }
 
    //´ò¿ªÁ¬½Ó£¬»òÖØÁ¬
    @Override
    public void connect(){
 
        close();
        DBSession sn=null;
        DBHelper.addHelper("mes","jdbc:mysql://127.0.0.1/gmms?serverTimezone=GMT%2B8","root","beibo.123/");
        try{
            this.sock = new Socket(ip,port);//´´½¨Á¬½Ó
            this.sock.setSoTimeout(timeout);            //ÉèÖó¬Ê±
            this.state=1;
            try {
 
                //String str1 = new String(yilianjie.getBytes("GBK"), "utf-8");
                //DBHelper db=DBHelper.getDBHelper("mes");
                sn=DBHelper.getDBHelper("mes").createSession(false);
                sn.createSql("update machine set connect_state='ÒÑÁ¬½Ó' where id="+machineID+"");
                int a=sn.update();
 
            } catch (SQLException e) {
                e.printStackTrace();
            }
 
        } catch (IOException e) {
            // TODO Auto-generated catch block
 
 
 
            try {
                //String str1 = new String(zhengzailianjie.getBytes("GBK"), "utf-8");
                sn=DBHelper.getDBHelper("mes").createSession(false);
                sn.createSql("update machine set connect_state='ÕýÔÚÁ¬½Ó' where id="+machineID+"");
                int a=sn.update();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
 
        }
    }
 
 
 
}