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; } }