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() { }
|
}
|
}
|