Tjiny
2025-06-24 4c7cbee3e351f289fac1400e976bc8f10dc7e169
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using AxActUtlTypeLib;
 
public static class Copy
{
    private static Dictionary<string, AxActUtlType> plcDic = new Dictionary<string, AxActUtlType>();
    private static Form form = new Form(); // 创建一个隐藏的窗体
 
    static Copy()
    {
        // 确保窗体已创建并运行消息泵
        var thread = new Thread(() => Application.Run(form));
        thread.SetApartmentState(ApartmentState.STA); // 设置线程为 STA
        thread.Start();
    }
 
    public static int GetDevice(string ip, string szDeviceName)
    {
        int result = -1;
        var thread = new Thread(() =>
        {
            try
            {
                if (plcDic.ContainsKey(ip))
                {
                    plcDic[ip].Open();
                    int deviceValue;
                    var iReturnCode = plcDic[ip].GetDevice(szDeviceName, out deviceValue);
                    if (iReturnCode == 0)
                    {
                        result = deviceValue;
                    }
                    else
                    {
                        plcDic[ip].Close();
                        plcDic[ip].Open();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in STA thread: " + ex.Message);
            }
        });
 
        thread.SetApartmentState(ApartmentState.STA); // 设置线程为 STA
        thread.Start();
        thread.Join(); // 等待线程完成
 
        return result;
    }
 
    public static bool SetDevice(string ip, string szDeviceName, int arrDeviceValue)
    {
        bool result = false;
        var thread = new Thread(() =>
        {
            try
            {
                if (plcDic.ContainsKey(ip))
                {
                    plcDic[ip].Open();
                    var iReturnCode = plcDic[ip].SetDevice(szDeviceName, arrDeviceValue);
                    if (iReturnCode == 0)
                    {
                        result = true;
                    }
                    else
                    {
                        plcDic[ip].Close();
                        plcDic[ip].Open();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in STA thread: " + ex.Message);
            }
        });
 
        thread.SetApartmentState(ApartmentState.STA); // 设置线程为 STA
        thread.Start();
        thread.Join(); // 等待线程完成
 
        return result;
    }
 
    private static void Init(string ip)
    {
        var thread = new Thread(() =>
        {
            try
            {
                if (!plcDic.ContainsKey(ip))
                {
                    var axActUtlType = new AxActUtlType();
                    ((ISupportInitialize)axActUtlType).BeginInit();
                    form.Controls.Add(axActUtlType); // 将控件添加到窗体中
                    ((ISupportInitialize)axActUtlType).EndInit();
                    axActUtlType.ActLogicalStationNumber = 1; // 设置逻辑站号
                    var res = axActUtlType.Open();
                    if (res == 0)
                    {
                        plcDic.Add(ip, axActUtlType);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in STA thread: " + ex.Message);
            }
        });
 
        thread.SetApartmentState(ApartmentState.STA); // 设置线程为 STA
        thread.Start();
        thread.Join(); // 等待线程完成
    }
}