yangys
2024-02-02 b82c71a3e3a97a78bd18ff598d27f3062600d22a
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
package com.qianwen.mdc.service.lsv2;
 
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.qianwen.mdc.utils.Lsv2Util;
import com.qianwen.mdc.utils.MdcException;
/**
 * Lsv2协议的数据采集客户端,用于Heidenhain系统。LSV-2用于仍然使用16位总线的设备
 */
public class Lsv2Client {
  private Shell shell;
  
  private OleFrame oleFrame;
  
  private OleControlSite oleControlSite;
  
  private OleAutomation oleAutomation;
  
  private final String ipAddr;
  
  private boolean connected;
  
  private final Logger logger;
  
  private final String activeXClsid = "{F9EE051D-C65D-4480-8E35-76F10FAB15C6}";
  
  public Lsv2Client(String ipAddr) {
    this.connected = false;
    this.logger = LoggerFactory.getLogger(getClass());
    this.ipAddr = ipAddr;
  }
  
  public boolean addUiControls() {
    Display display = Display.getDefault();
    if (display == null)
      return false; 
    if (!display.isDisposed())
      display.syncExec(() -> {
            this.shell = new Shell(display, 1264);
            this.shell.setLayout(null);
            this.oleFrame = new OleFrame((Composite)this.shell, 0);
            this.oleControlSite = new OleControlSite((Composite)this.oleFrame, 0, activeXClsid);
            this.oleAutomation = new OleAutomation((OleClientSite)this.oleControlSite);
            this.oleControlSite.doVerb(-3);
          }); 
    return true;
  }
  
  public boolean removeUiControls() {
    Display display = Display.getDefault();
    if (display == null)
      return false; 
    if (!display.isDisposed())
      display.syncExec(() -> {
            if (this.oleAutomation != null) {
              this.oleAutomation.dispose();
              this.oleAutomation = null;
            } 
            if (this.oleControlSite != null) {
              this.oleControlSite.doVerb(-6);
              this.oleControlSite.deactivateInPlaceClient();
              this.oleControlSite.dispose();
              this.oleControlSite = null;
            } 
            if (this.oleFrame != null) {
              this.oleFrame.dispose();
              this.oleFrame = null;
            } 
            if (this.shell != null) {
              this.shell.dispose();
              this.shell = null;
            } 
          }); 
    return true;
  }
  
  public void reAddUiControls() {
    removeUiControls();
    addUiControls();
  }
  
  public boolean testConnection() {
    this.connected = Lsv2Util.testConnection(this.oleAutomation);
    if (!this.connected)
      throw new MdcException(); 
    return true;
  }
  
  public boolean connect() {
    Lsv2Util.setMedium(this.oleAutomation, 1);
    Lsv2Util.setIPAddress(this.oleAutomation, this.ipAddr);
    Lsv2Util.setHostFunction(this.oleAutomation, Boolean.valueOf(false));
    return this.connected = Lsv2Util.connect(this.oleAutomation);
  }
  
  public boolean disconnect() {
    this.connected = false;
    return Lsv2Util.disConnect(this.oleAutomation);
  }
  
  public boolean reConnect() {
    return Lsv2Util.reConnect(this.oleAutomation);
  }
  
  public String readLastErrorString() {
    return Lsv2Util.getLastErrorString(this.oleAutomation);
  }
  
  public String readVersionTNC() {
    return Lsv2Util.getVersionTNC(this.oleAutomation);
  }
  
  public String readOpModeTest() {
    short type = 0;
    short address = 6019;
    short count = 1;
    return Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
  }
  
  public String readOpMode() {
    short type = 6;
    short address = 272;
    short count = 1;
    return Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
  }
  
  public String readNCState() {
    short type = 0;
    short address = 4176;
    short count = 1;
    return Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
  }
  
  public String readAlarmState() {
    short type = 0;
    short address = 4177;
    short count = 1;
    return Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
  }
  
  public String readProgName() {
    return Lsv2Util.getProgramName(this.oleAutomation);
  }
  
  public Integer readSpRate() {
    short type = 6;
    short address = 492;
    short count = 1;
    String spRate = Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
    if ("".equals(spRate))
      return Integer.valueOf(0); 
    return Integer.valueOf(Integer.parseInt(spRate, 16) / 100);
  }
  
  public String readSpState() {
    short type = 0;
    short address = 4005;
    short count = 2;
    return Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
  }
  
  public Integer readSpLoad() {
    short type = 7;
    short address = 4648;
    short count = 1;
    String spLoad = Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
    if ("".equals(spLoad))
      return Integer.valueOf(0); 
    int load = Integer.parseInt(spLoad, 16);
    if (load > 100)
      load = 0; 
    return Integer.valueOf(load);
  }
  
  public Integer readActualSpeed() {
    short type = 6;
    short address = 322;
    short count = 1;
    String actualSpeed = Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
    if ("".equals(actualSpeed))
      return Integer.valueOf(0); 
    return Integer.valueOf(Integer.parseInt(actualSpeed, 16));
  }
  
  public Integer readActualFeed() {
    short type = 7;
    short address = 388;
    short count = 1;
    String actualFeed = Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
    if ("".equals(actualFeed))
      return Integer.valueOf(0); 
    return Integer.valueOf(Integer.parseInt(actualFeed, 16));
  }
  
  public Integer readFeedRate() {
    short type = 6;
    short address = 494;
    short count = 1;
    String feedRate = Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
    if ("".equals(feedRate))
      return Integer.valueOf(0); 
    return Integer.valueOf(Integer.parseInt(feedRate, 16) / 100);
  }
  
  public Integer readToolNo() {
    short type = 6;
    short address = 264;
    short count = 1;
    String toolNo = Lsv2Util.receiveMemBlock(this.oleAutomation, type, address, count);
    if ("".equals(toolNo))
      return Integer.valueOf(0); 
    return Integer.valueOf(Integer.parseInt(toolNo, 16));
  }
  
  public boolean isConnected() {
    return this.connected;
  }
}