C3032
2026-03-18 32304b0a0a2e4af174c0feea96d2387bb75a2556
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using LB_VisionProcesses.BarcodeReaders;
 
namespace LB_VisionProcesses.Processes
{
    [Process("读码工具", Category = "取像工具", Description = "通过读码器获取条码数据")]
    public class BarcodeReaderProcess : IProcess
    {
        /// <summary>
        /// 读码器实例集合 (由主程序管理并传入)
        /// </summary>
        [JsonIgnore]
        public ConcurrentDictionary<string, BarcodeReaderBase> dicBarcodeReaders { get; set; }
 
        /// <summary>
        /// 当前关联的读码器
        /// </summary>
        [JsonIgnore]
        public BarcodeReaderBase Reader { get; set; }
 
        public BarcodeReaderProcess()
        {
            strProcessName = "读码工具";
            strProcessClass = "LB_VisionProcesses.Processes.BarcodeReaderProcess";
 
            Params.Inputs.Add("设备品牌", "Huayray");
            Params.Inputs.Add("设备SN", "");
            Params.Inputs.Add("触发模式", "软触发"); 
            Params.Inputs.Add("超时时间", 2000);
            
            Params.Outputs.Add("条码结果", "");
            Params.Outputs.Add("码数量", 0);
        }
 
        public override void InitRunParams()
        {
            Result = true;
            Msg = "准备运行";
            bCompleted = false;
        }
 
        public override bool Run()
        {
            DateTime StartTime = DateTime.Now;
            try
            {
                InitRunParams();
 
                string sn = Params.Inputs["设备SN"]?.ToString();
                if (string.IsNullOrEmpty(sn))
                {
                    Msg = "设备SN未配置";
                    Result = false;
                    return false;
                }
 
                // 从全局变量或传入字典获取
                if (dicBarcodeReaders == null)
                {
                    // 尝试从全局 dicGlobalVars 获取 (假设主程序已放入)
                    if (dicGlobalVars.ContainsKey("dicBarcodeReaders"))
                    {
                        dicBarcodeReaders = dicGlobalVars["dicBarcodeReaders"] as ConcurrentDictionary<string, BarcodeReaderBase>;
                    }
                }
 
                if (dicBarcodeReaders != null && dicBarcodeReaders.ContainsKey(sn))
                {
                    Reader = dicBarcodeReaders[sn];
                }
 
                if (Reader == null)
                {
                    Msg = $"读码器[{sn}]未初始化";
                    Result = false;
                    return false;
                }
 
                string triggerMode = Params.Inputs["触发模式"]?.ToString();
                if (triggerMode == "软触发")
                {
                    bool success = Reader.SoftTrigger();
                    Msg = success ? "触发成功" : "触发失败";
                    Result = success;
                }
            }
            catch (Exception ex)
            {
                Msg = "运行异常: " + ex.Message;
                Result = false;
            }
            finally
            {
                RunTime = (DateTime.Now - StartTime).TotalMilliseconds;
                bCompleted = true;
            }
            return Result;
        }
 
        public override bool Load(string fullPath)
        {
            try
            {
                if (string.IsNullOrEmpty(fullPath) || !File.Exists(fullPath)) return false;
                string json = File.ReadAllText(fullPath, Encoding.UTF8);
                Params = JsonConvert.DeserializeObject<ProcessParams>(json);
                Params?.FixDeserializedData();
                return true;
            }
            catch { return false; }
        }
 
        public override bool Save(string filePath)
        {
            try
            {
                if (string.IsNullOrEmpty(filePath)) return false;
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
                string fullPath = Path.Combine(filePath, strProcessName + ".json");
                string json = JsonConvert.SerializeObject(Params, Formatting.Indented);
                File.WriteAllText(fullPath, json, Encoding.UTF8);
                return true;
            }
            catch { return false; }
        }
 
        public override object Clone()
        {
            try
            {
                var obj = (BarcodeReaderProcess)MemberwiseClone();
                string json = JsonConvert.SerializeObject(this.Params);
                obj.Params = JsonConvert.DeserializeObject<ProcessParams>(json);
                return obj;
            }
            catch { return (BarcodeReaderProcess)MemberwiseClone(); }
        }
 
        public override void Dispose() { }
    }
}