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
package com.qianwen.mdc.utils;
 
 
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.Variant;
 
public class Lsv2Util {
  public static final String lsv2Ctrl3ProgId = "{F9EE051D-C65D-4480-8E35-76F10FAB15C6}";
  
  private static Variant execute(OleAutomation oleAutomation, String methodName) {
    return execute(oleAutomation, methodName, null);
  }
  
  private static Variant execute(OleAutomation oleAutomation, String methodName, Variant[] args) {
    int mid = getID(oleAutomation, methodName);
    return execute(oleAutomation, mid, args);
  }
  
  private static Variant execute(OleAutomation oleAutomation, int mid, Variant[] args) {
    Variant rtnv;
    if (mid < 0)
      return null; 
    if (args == null) {
      rtnv = oleAutomation.invoke(mid);
    } else {
      rtnv = oleAutomation.invoke(mid, args);
    } 
    return rtnv;
  }
  
  private static int getID(OleAutomation oleAutomation, String name) {
    try {
      int[] ids = oleAutomation.getIDsOfNames(new String[] { name });
      if (ids == null)
        return -1; 
      return ids[0];
    } catch (RuntimeException e) {
      e.printStackTrace();
      return -1;
    } 
  }
  
  public static String getLastError(OleAutomation oleAutomation) {
    String methodName = "LastError";
    return execute(oleAutomation, methodName).getString();
  }
  
  public static String getLastErrorString(OleAutomation oleAutomation) {
    String methodName = "LastErrorString";
    return execute(oleAutomation, methodName).getString();
  }
  
  public static boolean connect(OleAutomation oleAutomation) {
    String methodName = "Connect";
    return execute(oleAutomation, methodName).getBoolean();
  }
  
  public static boolean testConnection(OleAutomation oleAutomation) {
    String methodName = "TestConnection";
    Variant[] args = { new Variant(2) };
    return execute(oleAutomation, methodName, args).getBoolean();
  }
  
  public static boolean disConnect(OleAutomation oleAutomation) {
    String methodName = "DisConnect";
    return execute(oleAutomation, methodName).getBoolean();
  }
  
  public static boolean reConnect(OleAutomation oleAutomation) {
    String methodName = "ReConnect";
    return execute(oleAutomation, methodName).getBoolean();
  }
  
  public static void setMedium(OleAutomation oleAutomation, int medium) {
    String methodName = "Medium";
    Variant[] args = { new Variant(medium) };
    execute(oleAutomation, methodName, args);
  }
  
  public static void setIPAddress(OleAutomation oleAutomation, String ipAddress) {
    String methodName = "IPAddress";
    Variant[] args = { new Variant(ipAddress) };
    execute(oleAutomation, methodName, args);
  }
  
  public static void setPort(OleAutomation oleAutomation) {
    String methodName = "Port";
    execute(oleAutomation, methodName);
  }
  
  public static void setTransferMode(OleAutomation oleAutomation, Integer mode) {
    String methodName = "TransferMode";
    Variant[] args = { new Variant(mode.intValue()) };
    execute(oleAutomation, methodName, args);
  }
  
  public static void setHostFunction(OleAutomation oleAutomation, Boolean hostFunction) {
    String methodName = "HostFunction";
    Variant[] args = { new Variant(hostFunction.booleanValue()) };
    execute(oleAutomation, methodName, args);
  }
  
  public static String getVersionTNC(OleAutomation oleAutomation) {
    String methodName = "VersionTNC";
    return execute(oleAutomation, methodName).getString();
  }
  
  public static String getProgramName(OleAutomation oleAutomation) {
    String methodName = "ReceiveDNCInfo";
    short type = 3;
    int InfoPara = 0;
    Variant[] args = { new Variant(type), new Variant(InfoPara) };
    return execute(oleAutomation, methodName, args).getString();
  }
  
  public static String receiveMemBlock(OleAutomation oleAutomation, short type, short address, short count) {
    String methodName = "ReceiveMemBlock";
    Variant[] args = { new Variant(type), new Variant(address), new Variant(count) };
    return execute(oleAutomation, methodName, args).getString();
  }
}