Qian.MES 1.0.0

dotnet add package Qian.MES --version 1.0.0
                    
NuGet\Install-Package Qian.MES -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Qian.MES" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Qian.MES" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Qian.MES" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Qian.MES --version 1.0.0
                    
#r "nuget: Qian.MES, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Qian.MES@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Qian.MES&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Qian.MES&version=1.0.0
                    
Install as a Cake Tool

Qian.MES

Qian.MES 是一个面向 WPF/.NET 多目标框架(net48、net6.0-windows、net8.0-windows)的 MES 接入组件库,提供统一接口与工厂模式,便于业务系统按 MES 类型进行解耦调用。

当前内置实现:

  • AptivAIMS(安波福 AIMS)
  • LiXunMES(立讯)

目录结构

  • IMES.cs:MES 统一接口定义
  • MESType.cs:MES 类型枚举
  • MESFactory.cs:MES 实例工厂
  • AptivAIMS.cs:安波福实现
  • LiXunMES.cs:立讯实现

目标框架

本项目支持:

  • .NET Framework 4.8
  • .NET 6 (Windows)
  • .NET 8 (Windows)

核心接口

IMES 统一约定如下能力:

  1. DataContext

    • 类型:ConcurrentDictionary<string, ConcurrentDictionary<string, string>>
    • 用于缓存从 MES 读取或处理中间数据。
  2. ResolverInfoFromMES(string mesInputPath, string? fileName = null)

    • 从输入目录(可选指定文件名)读取 MES 数据并解析。
  3. ValidateData(params object[] args)

    • 在上传前进行业务校验(字段合法性、网络联通、协议返回值等)。
  4. UploadResultToMES(string sn, string savePath, string? saveFileName = null, params object[] args)

    • 将测试结果按目标 MES 的要求落盘或上传。

返回类型约定:

  • Dictionary<bool, string>
    • true:成功,值通常为成功信息
    • false:失败,值通常为失败原因

工厂创建

通过 MESFactory 按类型创建实例:

  • MESType.AptivAIMS → AptivAIMS
  • MESType.LiXun → LiXunMES
using Qian.MES;

IMES? mes = MESFactory.CreateMES(MESType.LiXun);
if (mes == null)
{
    // 处理未支持类型
}

LiXunMES 使用说明

输入数据解析

LiXunMES.ResolverInfoFromMES 从 JSON 文件读取数据,并反序列化为 Dictionary<string, string>。

关键行为:

  • 当 fileName 为空时,默认取目录下第一个文件。
  • 使用 SPAREPARTS + SERIALNUMBER 计算 SN。
  • 将解析结果写入 DataContext,键为 SN。

示例输入 JSON:

{
  "TASK_ID": "T20240101001",
  "BATCH": "B001",
  "SERIALNUMBER": "1234567890",
  "SPAREPARTS": "PN001",
  "EQUIPMENT": "ST01",
  "QRCODE1": "QR-ABC"
}

结果上传(CSV)

LiXunMES.UploadResultToMES 会:

  • 根据 sn 从 DataContext 获取原始数据。
  • 生成一行 CSV(含表头)。
  • 使用 File.WriteAllText 写入文件(同名文件会覆盖)。
  • 写入成功后移除对应 sn 的 DataContext 缓存。

CSV 表头固定为:

  • Task_ID
  • BATCH
  • SERIALNUMBER
  • SPAREPARTS
  • EQUIPMENT
  • 时间日期
  • Result
  • QRCODE1
  • QRCODE2

Result 默认取 args[0]。

using Qian.MES;

IMES mes = new LiXunMES();
var info = mes.ResolverInfoFromMES(@"D:\MES\IN", "input.json");
if (info == null) return;

string sn = info["SN"];
var upload = mes.UploadResultToMES(sn, @"D:\MES\OUT", "result.csv", "PASS");

AptivAIMS 使用说明

数据校验(TCP)

AptivAIMS.ValidateData(params object[] args) 参数约定:

  • args[0]:sn
  • args[1]:ip
  • args[2]:port(int)

逻辑摘要:

  • 建立 TCP 连接到目标 MES。
  • 发送:3&127.0.0.1&{sn}&END
  • 接收响应并解析状态码,00 为成功。

结果上传(TXT)

AptivAIMS.UploadResultToMES 参数约定:

  • args[0]:状态(如 PASS / FAIL)
  • args[1]:开始时间
  • args[2]:结束时间

输出规则:

  • PASS 写入 OK 子目录,其他写入 NOK 子目录。
  • 使用 File.WriteAllText,同名文件会覆盖。
using Qian.MES;

IMES mes = new AptivAIMS();
var valid = mes.ValidateData("SN001", "192.168.1.10", 9000);
if (!valid.ContainsKey(true)) return;

var result = mes.UploadResultToMES(
    "SN001",
    @"D:\AIMS\OUT",
    null,
    "PASS", "2025-01-01 08:00:00", "2025-01-01 08:00:05");

通用调用流程

  1. 创建实例(MESFactory 或直接 new)。
  2. 调用 ResolverInfoFromMES 读取输入(如该实现支持)。
  3. 调用 ValidateData 做上传前校验。
  4. 调用 UploadResultToMES 写入结果。
  5. 根据 Dictionary<bool,string> 做日志与异常处理。

常见问题

  1. LiXunMES 为什么覆盖文件?

    • 因为使用 File.WriteAllText。
  2. AptivAIMS 为什么同名文件内容被替换?

    • 因为使用 File.WriteAllText 覆盖写入。
  3. 如何判断调用成功?

bool ok = result.ContainsKey(true);
string message = ok ? result[true] : result[false];

扩展开发

新增 MES 类型建议步骤:

  1. 新建类并实现 IMES。
  2. 在 MESType 中新增枚举项。
  3. 在 MESFactory.CreateMES 中注册新类型。
  4. 按目标 MES 协议实现解析、校验和上传。
Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 135 3/17/2026