瀏覽代碼

加入專案檔案。

Robert 1 年之前
父節點
當前提交
df968f8ec7

+ 23 - 0
MesAdaptor/ISajetConnect.cs

@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MesAdaptor
+{
+    public interface ISajetConnect
+    {
+        bool SajetTransStart();
+        bool SajetTransClose();
+        bool SajetTransSignIn(ref string data);
+        bool SajetTransWoCheck(ref string workOrder);
+        bool SajetTransSnCheck(ref string serialNumber);
+        bool SajetTransSnCheck(ref string sn, string model);
+        bool SajetTranFinishSuccess();
+        bool SajetTranFinishFail(MesErrorCode errorCode);
+        string SajetTransRegisterHeader(string model, string header);
+        bool SajetTransReport(ValueReportDatas reportPair);
+        bool SajetTransLog(string data);
+    }
+}

+ 69 - 0
MesAdaptor/MesAdaptor.csproj

@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{6B69CF23-270B-429B-A21D-AD98C2DFF678}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>MesAdaptor</RootNamespace>
+    <AssemblyName>MesAdaptor</AssemblyName>
+    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <Deterministic>true</Deterministic>
+    <TargetFrameworkProfile />
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Prefer32Bit>false</Prefer32Bit>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Prefer32Bit>false</Prefer32Bit>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="PhihongEv.Lib, Version=1.0.12.0, Culture=neutral, processorArchitecture=MSIL">
+      <HintPath>..\packages\PhihongEv.Lib.1.0.12\lib\net40\PhihongEv.Lib.dll</HintPath>
+    </Reference>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Net.Http" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="ISajetConnect.cs" />
+    <Compile Include="MesErrorCode.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="SajectConnectSajet2.cs" />
+    <Compile Include="SajectConnectSajet.cs" />
+    <Compile Include="SajectConnectTest.cs" />
+    <Compile Include="SajetConnect.cs" />
+    <Compile Include="SajetConnectAdapter.cs" />
+    <Compile Include="SajetConnectShinewave.cs" />
+    <Compile Include="ValueReportData.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <PropertyGroup>
+    <PreBuildEvent>
+    </PreBuildEvent>
+  </PropertyGroup>
+</Project>

+ 33 - 0
MesAdaptor/MesErrorCode.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MesAdaptor
+{
+    public enum MesErrorCode
+    {
+        None,
+        ChargerConnectFail,
+        ModelNameUpadateFail,
+        SerilaNumberUpadateFail,
+        UtcDatetimeUpdateFail,
+        FourthGenModelVersionMismatch,
+        FourthGenSimInstartionMismatch,
+        FourthGenSimInfoMismatch,
+        WifiModeNotClient,
+        WifiRssiLow,
+        FirmwareUploadFail,
+        FirmwareUpdateTimeout,
+        FirmwareVersionCheckFail,
+        EmergencyButtonTestFail,
+        GreenButtonTestFail,
+        BlueButtonTestFail,
+        FactoryResetFail,
+
+        EraseFail,
+        ProgramFail,
+        GetCheckSumFail,
+    }
+}

+ 37 - 0
MesAdaptor/Properties/AssemblyInfo.cs

@@ -0,0 +1,37 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("MesAdaptor")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("MesAdaptor")]
+[assembly: AssemblyCopyright("Copyright ©  2021")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components.  If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("6b69cf23-270b-429b-a21d-ad98c2dff678")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.7.0")]
+[assembly: AssemblyVersion("1.0.7.0")]
+[assembly: AssemblyFileVersion("1.0.7.0")]
+[assembly: AssemblyInformationalVersion("0db9ef0")]

+ 280 - 0
MesAdaptor/SajectConnectSajet.cs

@@ -0,0 +1,280 @@
+using PhihongEv.Lib;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MesAdaptor
+{
+    public class SajectConnectSajet : ISajetConnect
+    {
+        private enum CMD
+        {
+            Signin = 1,
+            SnCheck = 2,
+            WoCheck = -1,
+            Report = 3,
+            Log = 4,
+            HeaderRegister = 8,
+            ValueReport = 5,
+            StringValueReport = 6,
+        }
+
+        public bool SajetTransStart() => SajetConnectAdapter.SajetTransStart();
+        public bool SajetTransClose() => SajetConnectAdapter.SajetTransClose();
+
+        private string userId = "";
+        public bool SajetTransSignIn(ref string data)
+        {
+            var attemptId = data;
+            if (SajetTransData(CMD.Signin, ref data))
+            {
+                if (string.IsNullOrEmpty(data) || data.StartsWith("NG"))
+                {
+                    userId = "";
+                    return false;
+                }
+                else
+                {
+                    userId = attemptId;
+                    return true;
+                }
+            }
+            userId = "";
+            return false;
+        }
+
+        private string WorkOrder = "";
+        public bool SajetTransWoCheck(ref string workOrder)
+        {
+            return true;
+        }
+
+        private string SN;
+        public bool SajetTransSnCheck(ref string serialNumber)
+        {
+            var attemptSN = serialNumber;
+            var msg = serialNumber;
+
+            if (SajetTransData(CMD.SnCheck, ref msg))
+            {
+                if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
+                {
+                    SN = "";
+                    return false;
+                }
+                else
+                {
+                    SN = attemptSN;
+                    return true;
+                }
+            }
+            SN = "";
+            return false;
+        }
+
+        public bool SajetTransSnCheck(ref string sn, string model)
+        {
+            throw new NotImplementedException();
+        }
+
+        public bool SajetTranFinishSuccess()
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (string.IsNullOrEmpty(SN))
+                return false;
+            string msg = userId + ";" + SN + ";";
+
+            if (string.IsNullOrEmpty(msg))
+                return false;
+
+            msg += "OK";
+
+#if DEBUG
+            return true;
+#endif
+
+            return SajetTransData(CMD.Report, ref msg);
+        }
+
+        public bool SajetTranFinishFail(MesErrorCode errorCode)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (string.IsNullOrEmpty(SN))
+                return false;
+            string msg = userId + ";" + SN + ";";
+
+            if (string.IsNullOrEmpty(msg))
+                return false;
+
+            msg += $"NG;{errorCode};";  //E114
+
+#if DEBUG
+            return true;
+#endif
+
+            return SajetTransData(CMD.Report, ref msg);
+        }
+
+        public string SajetTransRegisterHeader(string model, string header)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return "";
+            var msg = userId + ";";
+            msg += model + ";" + header;
+            if (SajetTransData(CMD.HeaderRegister, ref msg))
+            {
+                if (msg.StartsWith("OK"))
+                {
+                    //get codename
+                    msg = msg.Substring(3);
+                    var spaceIndex = msg.IndexOf(";");
+                    if (spaceIndex > 0)
+                    {
+                        msg = msg.Substring(0, spaceIndex);
+                    }
+                    return msg;
+                }
+                return null;
+            }
+            return null;
+        }
+
+        public bool SajetTransReport(ValueReportDatas reportPair)
+        {
+            //build header
+            Dictionary<string, int> valuePairs = new Dictionary<string, int>();
+            foreach (var data in reportPair)
+            {
+                if (int.TryParse(data.Val, out int val))
+                {
+                    valuePairs.Add(data.Key, val);
+                }
+                else if (data.Val.ToLower().Contains("fail"))
+                {
+                    valuePairs.Add(data.Key, 0);
+                }
+                else if (data.Val.ToLower().Contains("success"))
+                {
+                    valuePairs.Add(data.Key, 1);
+                }
+                else
+                {
+                    valuePairs.Add(string.Format("{0}:{1}", data.Key, data.Val), 1);
+                }
+            }
+            //register Header
+            var codePair = new Dictionary<string, string>();
+
+            string model = "";
+            if (SystemID.TryParse(SN,out var systemID))
+            {
+                model = systemID.ModelName.ToString();
+            }
+
+            foreach (var key in valuePairs.Keys)
+            {
+                var code = SajetTransRegisterHeader(model, key);
+                if (string.IsNullOrEmpty(code))
+                    continue;
+                codePair.Add(key, code);
+            }
+            //report value
+            var reportResult = SajetTransReport(valuePairs, codePair);
+            return reportResult;
+        }
+
+        public bool SajetTransReport(Dictionary<string, int> resultPair, Dictionary<string, string> codePair)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (string.IsNullOrEmpty(SN))
+                return false;
+            string msgHeader = userId + ";" + SN;
+            string msg = "";
+
+            foreach (var result in resultPair)
+            {
+                if (codePair.Keys.Contains(result.Key))
+                {
+                    msg = string.Format("{0};{1}:{2};",msgHeader, codePair[result.Key], string.Format("{0}.00", result.Value));
+                    //msg += string.Format(";{0}:{1}", codePair[result.Key], string.Format("{0}.00", result.Value));
+                }
+                //msg += ";";
+                SajetTransData(CMD.ValueReport, ref msg);
+                //msg = userId + ";" + SN;
+            }
+            //msg += ";";
+            return true;
+        }
+
+        public bool SajetTransReport(Dictionary<string, string> resultPair, Dictionary<string, string> codePair)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (string.IsNullOrEmpty(SN))
+                return false;
+            string msg = userId + ";" + SN;
+            foreach (var result in resultPair)
+            {
+                if (codePair.Keys.Contains(result.Key))
+                {
+                    msg += string.Format(";{0}:{1}", codePair[result.Key], string.Format("{0}.00", result.Value));
+                }
+                msg += ";";
+                SajetTransData(CMD.StringValueReport, ref msg);
+                msg = userId + ";" + SN;
+            }
+            msg += ";";
+            return true;
+        }
+
+        public bool SajetTransLog(string data)
+        {
+            return true;
+
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (string.IsNullOrEmpty(SN))
+                return false;
+            string prefix = userId + ";" + SN + ";";
+            while (data.Length > 0)
+            {
+                var msg = prefix;
+                int sendLength = Math.Min(250 - msg.Length, data.Length);
+                var sendString = data.Substring(0, sendLength);
+                msg += sendString;
+                if (!SajetTransData(CMD.Log, ref msg))
+                {
+                    break;
+                }
+                data = data.Substring(sendLength);
+            }
+            return data.Length == 0;
+        }
+
+        private bool SajetTransData(CMD command, ref string data)
+        {
+            return SajetConnectAdapter.SajetTransData((int)command, ref data);
+        }
+
+        private string GetSendPrefix()
+        {
+            string msg = "";
+            if (string.IsNullOrEmpty(userId))
+            {
+                return null;
+            }
+            msg = userId + ";";
+            if (string.IsNullOrEmpty(WorkOrder))
+            {
+                return null;
+            }
+            msg += WorkOrder + ";";
+            return msg;
+        }
+    }
+}

+ 324 - 0
MesAdaptor/SajectConnectSajet2.cs

@@ -0,0 +1,324 @@
+using PhihongEv.Lib;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MesAdaptor
+{
+    public class SajectConnectSajet2 : ISajetConnect
+    {
+        private enum CMD
+        {
+            Signin = 1,
+            SnCheck = 2,
+            Report = 3,
+            ValueReport = 4,
+        }
+
+        private readonly string _MechineCode;
+        public SajectConnectSajet2(string MechineCode)
+        {
+            if (string.IsNullOrEmpty(MechineCode))
+            {
+                throw new Exception("MechineCode cannot not be empty");
+            }
+
+            _MechineCode = MechineCode;
+        }
+
+        public bool SajetTransStart() => SajetConnectAdapter.SajetTransStart();
+        public bool SajetTransClose() => SajetConnectAdapter.SajetTransClose();
+
+        private string userId = "";
+        public bool SajetTransSignIn(ref string data)
+        {
+            var attemptId = data;
+            var msg = data + ";";
+            if (SajetTransData(CMD.Signin, ref data))
+            {
+                if (string.IsNullOrEmpty(data) || data.StartsWith("NG"))
+                {
+                    userId = "";
+                    return false;
+                }
+                else
+                {
+                    userId = attemptId;
+                    return true;
+                }
+            }
+            userId = "";
+            return false;
+        }
+
+        [Obsolete]
+        public bool SajetTransWoCheck(ref string workOrder)
+        {
+            return true;
+        }
+
+        //private SystemID systemID;
+        string modelName;
+        string serialNumber;
+        public bool SajetTransSnCheck(ref string serialNumber)
+        {
+            if (!SystemID.TryLooseParse(serialNumber, out var id))
+            {
+                return false;
+            }
+
+            this.modelName = null;
+            this.serialNumber = null;
+            var result = SajetTransSnCheck(ref serialNumber, id.ModelName.ToString());
+            if (result)
+            {
+                this.modelName = id.ModelName.ToString();
+                this.serialNumber = serialNumber;
+            }
+            return result;
+
+            //var msg = string.Format("{0};{1};;", id.ModelName.ToString(), id.ToString());
+
+            //if (SajetTransData(CMD.SnCheck, ref msg))
+            //{
+            //    if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
+            //    {
+            //        systemID = null;
+            //        return false;
+            //    }
+            //    systemID = id;
+            //    return true;
+            //}
+            //systemID = null;
+            //return false;
+        }
+
+        public bool SajetTransSnCheck(ref string sn, string model)
+        {
+            var msg = string.Format("{0};{1};;", model, sn);
+
+            this.serialNumber = null;
+            this.modelName = null;
+            if (SajetTransData(CMD.SnCheck, ref msg))
+            {
+                if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
+                {
+                    return false;
+                }
+                this.serialNumber = sn;
+                this.modelName = model;
+                return true;
+            }
+            return false;
+        }
+
+        //public bool SajetTransSnCheck(string partNO,string sn)
+        //{
+        //    var msg = string.Format("{0};{1};;", partNO, sn);
+
+        //    if (SajetTransData(CMD.SnCheck, ref msg))
+        //    {
+        //        if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
+        //        {
+        //            return false;
+        //        }
+        //        return true;
+        //    }
+        //    return false;
+        //}
+
+        public bool SajetTranFinishSuccess()
+        {
+            //File.AppendAllText("debug.txt", string.Format("userId:{0}", userId));
+            //File.AppendAllText("debug.txt", string.Format("modelName:{0}", modelName));
+            //File.AppendAllText("debug.txt", string.Format("serialNumber:{0}", serialNumber));
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (modelName == null)
+                return false;
+            if (serialNumber == null)
+                return false;
+            //string msg = userId + ";" + systemID.ToString() + ";";
+            string msg = string.Format("{0};{1};{2};{3}:0;", userId, _MechineCode, modelName, serialNumber);
+
+            if (string.IsNullOrEmpty(msg))
+                return false;
+
+            return SajetTransData(CMD.Report, ref msg);
+        }
+
+        //public bool SajetTranFinishSuccess(string partNO, string sn)
+        //{
+        //    if (string.IsNullOrEmpty(userId))
+        //        return false;
+        //    //string msg = userId + ";" + systemID.ToString() + ";";
+        //    string msg = string.Format("{0};{1};{2};{3}:0;", userId, _MechineCode, partNO, sn);
+
+        //    if (string.IsNullOrEmpty(msg))
+        //        return false;
+
+        //    return SajetTransData(CMD.Report, ref msg);
+        //}
+
+        public bool SajetTranFinishFail(MesErrorCode errorCode)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (modelName == null)
+                return false;
+            if (serialNumber == null)
+                return false;
+            //string msg = userId + ";" + systemID.ToString() + ";";
+            string msg = string.Format("{0};{1};{2};{3}:1:", userId, _MechineCode, modelName, serialNumber);
+
+            if (string.IsNullOrEmpty(msg))
+                return false;
+
+            msg += $"{GetMesCodeString(errorCode)};";  //E114
+
+            return SajetTransData(CMD.Report, ref msg);
+        }
+
+        //public bool SajetTranFinishFail(string partNO, string sn, MesErrorCode errorCode)
+        //{
+        //    if (string.IsNullOrEmpty(userId))
+        //        return false;
+        //    //string msg = userId + ";" + systemID.ToString() + ";";
+        //    string msg = string.Format("{0};{1};{2};{3}:1:", userId, _MechineCode, partNO, sn);
+
+        //    if (string.IsNullOrEmpty(msg))
+        //        return false;
+
+        //    msg += $"{errorCode};";  //E114
+
+        //    return SajetTransData(CMD.Report, ref msg);
+        //}
+
+        [Obsolete]
+        public string SajetTransRegisterHeader(string model, string header)
+        {
+            return "";
+        }
+
+        public bool SajetTransReport(ValueReportDatas reportDatas)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (modelName == null)
+                return false;
+            if (serialNumber == null)
+                return false;
+
+            var totalResult = true;
+
+            foreach (var data in reportDatas)
+            {
+                var key = data.Key;
+                var value = data.Val;
+                var isSuccess = data.IsSuccess;
+
+                value = value.Trim();
+                var det = isSuccess ? "0" : "1";
+
+                if (string.IsNullOrEmpty(value))
+                {
+                    value = "empty";
+                }
+                else if (value.ToLower() == "fail")
+                {
+                    value = "0";
+                }
+                else if (value.ToLower() == "success")
+                {
+                    value = "1";
+                }
+
+                var msg = string.Format("{0};{1};{2};{3};{4}:{5}:{6};", userId, _MechineCode, modelName, serialNumber, key, value, det);
+                var result = SajetTransData(CMD.ValueReport, ref msg);
+                totalResult &= result;
+            }
+            return totalResult;
+        }
+
+        //public bool SajetTransReport(string partNO, string no, Dictionary<string, string> reportPair)
+        //{
+        //    if (string.IsNullOrEmpty(userId))
+        //        return false;
+
+        //    var totalResult = true;
+        //    foreach (var pair in reportPair)
+        //    {
+        //        var msg = string.Format("{0};{1};{2};{3};{4}:{5};", userId, _MechineCode, partNO, no, pair.Key, pair.Value);
+        //        var result = SajetTransData(CMD.ValueReport, ref msg);
+        //        totalResult &= result;
+        //    }
+        //    return totalResult;
+        //}
+
+        [Obsolete]
+        public bool SajetTransReport(Dictionary<string, int> resultPair, Dictionary<string, string> codePair)
+        {
+            return true;
+        }
+
+        [Obsolete]
+        public bool SajetTransReport(Dictionary<string, string> resultPair, Dictionary<string, string> codePair)
+        {
+            return true;
+        }
+
+        [Obsolete]
+        public bool SajetTransLog(string data)
+        {
+            return true;
+        }
+
+        private bool SajetTransData(CMD command, ref string data)
+        {
+            return SajetConnectAdapter.SajetTransData((int)command, ref data);
+        }
+
+        private string GetMesCodeString(MesErrorCode mesError)
+        {
+            switch (mesError)
+            {
+                case MesErrorCode.ChargerConnectFail:
+                    return "A081";
+                case MesErrorCode.ModelNameUpadateFail:
+                    return "A082";
+                case MesErrorCode.SerilaNumberUpadateFail:
+                    return "A083";
+                case MesErrorCode.UtcDatetimeUpdateFail:
+                    return "A084";
+                case MesErrorCode.FourthGenModelVersionMismatch:
+                    return "A085";
+                case MesErrorCode.FourthGenSimInstartionMismatch:
+                    return "A086";
+                case MesErrorCode.FourthGenSimInfoMismatch:
+                    return "A087";
+                case MesErrorCode.WifiModeNotClient:
+                    return "A088";
+                case MesErrorCode.WifiRssiLow:
+                    return "A089";
+                case MesErrorCode.FirmwareUploadFail:
+                    return "A090";
+                case MesErrorCode.FirmwareUpdateTimeout:
+                    return "A091";
+                case MesErrorCode.FirmwareVersionCheckFail:
+                    return "A092";
+                case MesErrorCode.EmergencyButtonTestFail:
+                    return "A093";
+                case MesErrorCode.GreenButtonTestFail:
+                    return "A094";
+                case MesErrorCode.BlueButtonTestFail:
+                    return "A095";
+                case MesErrorCode.FactoryResetFail:
+                    return "A096";
+            }
+            return "A081";
+        }
+    }
+}

+ 33 - 0
MesAdaptor/SajectConnectTest.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MesAdaptor
+{
+    public class SajectConnectTest : ISajetConnect
+    {
+        public bool SajetTranFinishFail(MesErrorCode errorCode) => true;
+
+        public bool SajetTranFinishSuccess() => true;
+
+        public bool SajetTransClose() => true;
+
+        public bool SajetTransLog(string data) => true;
+
+        public string SajetTransRegisterHeader(string model, string header) => "";
+
+        public bool SajetTransReport(ValueReportDatas reportPair) => true;
+
+        public bool SajetTransSignIn(ref string data) => true;
+
+        public bool SajetTransSnCheck(ref string serialNumber) => true;
+
+        public bool SajetTransSnCheck(ref string sn, string model) => true;
+
+        public bool SajetTransStart() => true;
+
+        public bool SajetTransWoCheck(ref string workOrder) => true;
+    }
+}

+ 64 - 0
MesAdaptor/SajetConnect.cs

@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MesAdaptor
+{
+    public enum MesType
+    {
+        none,
+        shinewave,
+        sajet,
+        sajet2,
+        test
+    }
+
+    public static class SajetConnect
+    {
+        public static ISajetConnect Instance { get; internal set; }
+
+        public static bool SajetTransStart() => Instance.SajetTransStart();
+        public static bool SajetTransClose() => Instance == null ? false : Instance.SajetTransClose();
+        public static bool SajetTransSignIn(ref string data) => Instance.SajetTransSignIn(ref data);
+        public static bool SajetTransWoCheck(ref string workOrder) => Instance.SajetTransWoCheck(ref workOrder);
+        public static bool SajetTransSnCheck(ref string serialNumber) => Instance.SajetTransSnCheck(ref serialNumber);
+        public static bool SajetTransSnCheck(ref string serialNumber, string model) => Instance.SajetTransSnCheck(ref serialNumber, model);
+        public static bool SajetTranFinishSuccess() => Instance.SajetTranFinishSuccess();
+        public static bool SajetTranFinishFail(MesErrorCode errorCode) => Instance.SajetTranFinishFail(errorCode);
+        public static string SajetTransRegisterHeader(string model, string header) => Instance.SajetTransRegisterHeader(model, header);
+        public static bool SajetTransReport(ValueReportDatas reportPair) => Instance.SajetTransReport(reportPair);
+        public static bool SajetTransLog(string data) => Instance.SajetTransLog(data);
+
+        public static void SetMes(string mesSetting = "",string mechineCode = "")
+        {
+            if (mesSetting is null)
+            {
+                SajetConnect.Instance = new SajectConnectTest();
+                return;
+            }
+            
+            mesSetting = mesSetting.ToLower();
+            switch (mesSetting)
+            {
+                case "php":
+                case "shinewave":
+                    SajetConnect.Instance = new SajetConnectShinewave();
+                    break;
+                //case "phv":
+                case "sajet":
+                    SajetConnect.Instance = new SajectConnectSajet();
+                    break;
+                case "phv":
+                case "sajet2":
+                    SajetConnect.Instance = new SajectConnectSajet2(mechineCode);
+                    break;
+                default:
+                    SajetConnect.Instance = new SajectConnectTest();
+                    break;
+            }
+        }
+    }
+}

+ 59 - 0
MesAdaptor/SajetConnectAdapter.cs

@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MesAdaptor
+{
+    public static class SajetConnectAdapter
+    {
+        [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
+        public static extern bool SajetTransStart();
+
+        [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
+        public static extern bool SajetTransClose();
+
+        //[DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
+        //public static extern bool SajetTransData(int command, IntPtr data, IntPtr length);
+
+        [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
+        public static extern bool SajetTransData(int command, IntPtr data, IntPtr length);
+
+        [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
+        public static extern bool SajetTransData_C(int command, out string data);
+
+        public static bool SajetTransData(int command, ref string data)
+        {
+            var idByte = Encoding.ASCII.GetBytes(data);
+            var length = idByte.Length;
+
+            var dataArrayLength = Math.Max(length, 200);
+            var dataArray = new byte[dataArrayLength];
+            Array.Copy(idByte, 0, dataArray, 0, length);
+
+            IntPtr dataIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(dataArrayLength);
+            System.Runtime.InteropServices.Marshal.Copy(dataArray, 0, dataIntPtr, dataArray.Length);
+
+            IntPtr lengthIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(10);
+            System.Runtime.InteropServices.Marshal.WriteInt32(lengthIntPtr, length);
+
+            var sendResult = SajetConnectAdapter.SajetTransData(command, dataIntPtr, lengthIntPtr);
+
+            var resultString1 = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(dataIntPtr);
+
+            System.Runtime.InteropServices.Marshal.Copy(dataIntPtr, dataArray, 0, dataArrayLength);
+            //System.Runtime.InteropServices.Marshal.Release(dataIntPtr);
+            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(dataIntPtr);
+
+            var readLength = System.Runtime.InteropServices.Marshal.ReadInt32(lengthIntPtr);
+            //System.Runtime.InteropServices.Marshal.Release(lengthIntPtr);
+
+            var resultString = Encoding.ASCII.GetString(dataArray);
+
+            data = resultString1.Substring(0, Math.Min(readLength, resultString1.Length));
+            return sendResult;
+        }
+    }
+}

+ 307 - 0
MesAdaptor/SajetConnectShinewave.cs

@@ -0,0 +1,307 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MesAdaptor
+{
+    public class SajetConnectShinewave : ISajetConnect
+    {
+        private enum CMD
+        {
+            Signin = 1,
+            Log = 4,
+            ValueReport = 5,
+            StringValueReport = 6,
+            HeaderRegister = 8,
+            WoCheck = 14,
+            SnCheck = 15,
+            Report = 16,
+        }
+
+        public bool SajetTransStart() => SajetConnectAdapter.SajetTransStart();
+        public bool SajetTransClose() => SajetConnectAdapter.SajetTransClose();
+
+        private string userId = "";
+        public bool SajetTransSignIn(ref string data)
+        {
+            var attemptId = data;
+            if (SajetTransData(CMD.Signin, ref data))
+            {
+                if (string.IsNullOrEmpty(data) || data.StartsWith("NG"))
+                {
+                    userId = "";
+                    return false;
+                }
+                else
+                {
+                    userId = attemptId;
+                    return true;
+                }
+            }
+            userId = "";
+            return false;
+        }
+
+        private string WorkOrder = "";
+        public bool SajetTransWoCheck(ref string workOrder)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (workOrder == null)
+                workOrder = "";
+            var msg = userId + "," + workOrder + ",";
+
+            if (SajetTransData(CMD.WoCheck, ref msg))
+            {
+                if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
+                {
+                    WorkOrder = "";
+                    return false;
+                }
+                else
+                {
+                    WorkOrder = workOrder;
+                    return true;
+                }
+            }
+            WorkOrder = "";
+            return false;
+        }
+
+        private string SN;
+        public bool SajetTransSnCheck(ref string serialNumber)
+        {
+            var attemptSN = serialNumber;
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (WorkOrder == null)
+                WorkOrder = "";
+            var msg = userId + "," + WorkOrder + "," + serialNumber + ",";
+
+            if (SajetTransData(CMD.SnCheck, ref msg))
+            {
+                if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
+                {
+                    SN = "";
+                    return false;
+                }
+                else
+                {
+                    SN = attemptSN;
+                    return true;
+                }
+            }
+            SN = "";
+            return false;
+        }
+
+        public bool SajetTransSnCheck(ref string sn, string model)
+        {
+            throw new NotImplementedException();
+        }
+
+        public bool SajetTranFinishSuccess()
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            //if (string.IsNullOrEmpty(WorkOrder))
+            //    return false;
+            if (string.IsNullOrEmpty(SN))
+                return false;
+            string msg = userId + "," + WorkOrder + "," + SN + ",";
+
+            if (string.IsNullOrEmpty(msg))
+                return false;
+
+            msg += "OK,";
+
+#if DEBUG
+            return true;
+#endif
+
+            return SajetTransData(CMD.Report, ref msg);
+        }
+
+        public bool SajetTranFinishFail(MesErrorCode errorCode)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            //if (string.IsNullOrEmpty(WorkOrder))
+            //    return false;
+            if (string.IsNullOrEmpty(SN))
+                return false;
+            string msg = userId + "," + WorkOrder + "," + SN + ",";
+
+            if (string.IsNullOrEmpty(msg))
+                return false;
+
+            msg += "NG," + GetMesCodeString(errorCode) + ",";
+
+#if DEBUG
+            return true;
+#endif
+
+            return SajetTransData(CMD.Report, ref msg);
+        }
+
+        public string SajetTransRegisterHeader(string model, string header)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return "";
+            var msg = userId + ",";
+            msg += model + "," + header;
+            if (SajetTransData(CMD.HeaderRegister, ref msg))
+            {
+                if (msg.StartsWith("OK"))
+                {
+                    //get codename
+                    msg = msg.Substring(3);
+                    var spaceIndex = msg.IndexOf(",");
+                    if (spaceIndex > 0)
+                    {
+                        msg = msg.Substring(0, spaceIndex);
+                    }
+                    return msg;
+                }
+                return null;
+            }
+            return null;
+        }
+
+        public bool SajetTransReport(ValueReportDatas reportPairs)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (string.IsNullOrEmpty(SN))
+                return false;
+            string msgHeader = userId + "," + SN;
+            string msg;
+            bool result = true;
+
+            foreach (var report in reportPairs)
+            {
+                msg = string.Format("{0},{1}:{2},", msgHeader, report.Key, report.Val);
+                var cmdResult = SajetTransData(CMD.StringValueReport, ref msg);
+                if (!cmdResult)
+                    result = false;
+            }
+            return result;
+        }
+
+        public bool SajetTransReport(Dictionary<string, int> resultPair, Dictionary<string, string> codePair)
+        {
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (string.IsNullOrEmpty(SN))
+                return false;
+            string msg = userId + "," + SN + ",";
+            foreach (var result in resultPair)
+            {
+                if (codePair.Keys.Contains(result.Key))
+                {
+                    msg += string.Format("{0}:{1},", codePair[result.Key], string.Format("{0}.00", result.Value));
+                }
+            }
+            return SajetTransData(CMD.ValueReport, ref msg);
+        }
+
+        public bool SajetTransLog(string data)
+        {
+            return true;
+
+            if (string.IsNullOrEmpty(userId))
+                return false;
+            if (string.IsNullOrEmpty(SN))
+                return false;
+            string prefix = userId + "," + SN + ",";
+            while (data.Length > 0)
+            {
+                var msg = prefix;
+                int sendLength = Math.Min(250 - msg.Length, data.Length);
+                var sendString = data.Substring(0, sendLength);
+                msg += sendString;
+                if (!SajetTransData(CMD.Log, ref msg))
+                {
+                    break;
+                }
+                data = data.Substring(sendLength);
+            }
+            return data.Length == 0;
+        }
+
+        private bool SajetTransData(CMD command, ref string data)
+        {
+            //return true;
+            if (!data.EndsWith(","))
+            {
+                data = data + ",";
+            }
+            return SajetConnectAdapter.SajetTransData((int)command, ref data);
+        }
+
+        private string GetSendPrefix()
+        {
+            string msg = "";
+            if (string.IsNullOrEmpty(userId))
+            {
+                return null;
+            }
+            msg = userId + ",";
+            if (string.IsNullOrEmpty(WorkOrder))
+            {
+                return null;
+            }
+            msg += WorkOrder + ",";
+            return msg;
+        }
+
+        private string GetMesCodeString(MesErrorCode mesError)
+        {
+            switch (mesError)
+            {
+                case MesErrorCode.ChargerConnectFail:
+                    return "EC001";
+                case MesErrorCode.ModelNameUpadateFail:
+                    return "EC002";
+                case MesErrorCode.SerilaNumberUpadateFail:
+                    return "EC003";
+                case MesErrorCode.UtcDatetimeUpdateFail:
+                    return "EC004";
+                case MesErrorCode.FourthGenModelVersionMismatch:
+                    return "EC005";
+                case MesErrorCode.FourthGenSimInstartionMismatch:
+                    return "EC006";
+                case MesErrorCode.FourthGenSimInfoMismatch:
+                    return "EC007";
+                case MesErrorCode.WifiModeNotClient:
+                    return "EC008";
+                case MesErrorCode.WifiRssiLow:
+                    return "EC009";
+                case MesErrorCode.FirmwareUploadFail:
+                    return "EC010";
+                case MesErrorCode.FirmwareUpdateTimeout:
+                    return "EC011";
+                case MesErrorCode.FirmwareVersionCheckFail:
+                    return "EC012";
+                case MesErrorCode.EmergencyButtonTestFail:
+                    return "EC013";
+                case MesErrorCode.GreenButtonTestFail:
+                    return "EC014";
+                case MesErrorCode.BlueButtonTestFail:
+                    return "EC015";
+                case MesErrorCode.FactoryResetFail:
+                    return "EC016";
+
+                case MesErrorCode.EraseFail:
+                    return "ST001";
+                case MesErrorCode.ProgramFail:
+                    return "ST002";
+                case MesErrorCode.GetCheckSumFail:
+                    return "ST003";
+            }
+            return "EC001";
+        }
+    }
+}

+ 74 - 0
MesAdaptor/ValueReportData.cs

@@ -0,0 +1,74 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MesAdaptor
+{
+    public class ValueReportDatas : IEnumerable<ValueReportData>
+    {
+        public ValueReportDatas()
+        {
+            _valueReportDatas = new List<ValueReportData>();
+        }
+
+        public void Add(string key,string val, bool success)
+        {
+            var data = new ValueReportData(key,val,success);
+            var sameKey = _valueReportDatas.FirstOrDefault(x => x.Key == data.Key);
+            _valueReportDatas.Remove(sameKey);
+            _valueReportDatas.Add(data);
+        }
+
+        public void Add(ValueReportData data)
+        {
+            var sameKey = _valueReportDatas.FirstOrDefault(x=>x.Key == data.Key);
+            _valueReportDatas.Remove(sameKey);
+            _valueReportDatas.Add(data);
+        }
+
+        public void Clear()
+        {
+            _valueReportDatas = new List<ValueReportData>();
+        }
+
+        public IEnumerator<ValueReportData> GetEnumerator()
+        {
+            return _valueReportDatas.GetEnumerator();
+        }
+
+        IEnumerator IEnumerable.GetEnumerator()
+        {
+            return _valueReportDatas.GetEnumerator();
+        }
+
+        public ValueReportData this[string index]
+        {
+            get
+            {
+                return _valueReportDatas.FirstOrDefault(x => x.Key == index);
+            }
+        }
+
+        private List<ValueReportData> _valueReportDatas;
+    }
+
+    public class ValueReportData
+    {
+        public ValueReportData(
+            string key,
+            string val,
+            bool isSuccess)
+        {
+            Key = key;
+            Val = val;
+            IsSuccess = isSuccess;
+        }
+
+        public string Key { get; private set; }
+        public string Val { get; private set; }
+        public bool IsSuccess { get; private set; }
+    }
+}

+ 4 - 0
MesAdaptor/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="PhihongEv.Lib" version="1.0.12" targetFramework="net48" />
+</packages>

+ 31 - 0
ST-CUBE_MES.sln

@@ -0,0 +1,31 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.7.34031.279
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ST-CUBE_MES", "ST-CUBE_MES\ST-CUBE_MES.csproj", "{A968FD3F-5374-442B-B087-3385764235AD}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MesAdaptor", "MesAdaptor\MesAdaptor.csproj", "{6B69CF23-270B-429B-A21D-AD98C2DFF678}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{A968FD3F-5374-442B-B087-3385764235AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{A968FD3F-5374-442B-B087-3385764235AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{A968FD3F-5374-442B-B087-3385764235AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{A968FD3F-5374-442B-B087-3385764235AD}.Release|Any CPU.Build.0 = Release|Any CPU
+		{6B69CF23-270B-429B-A21D-AD98C2DFF678}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{6B69CF23-270B-429B-A21D-AD98C2DFF678}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{6B69CF23-270B-429B-A21D-AD98C2DFF678}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{6B69CF23-270B-429B-A21D-AD98C2DFF678}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {58439537-D847-40B2-B1BE-04AA77401B05}
+	EndGlobalSection
+EndGlobal

+ 6 - 0
ST-CUBE_MES/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
+    </startup>
+</configuration>

+ 8 - 0
ST-CUBE_MES/App.xaml

@@ -0,0 +1,8 @@
+<Application
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:local="clr-namespace:ST_CUBE_MES"
+    StartupUri="LoginWindow.xaml"
+    x:Class="ST_CUBE_MES.App">
+    <Application.Resources />
+</Application>

+ 30 - 0
ST-CUBE_MES/App.xaml.cs

@@ -0,0 +1,30 @@
+using MesAdaptor;
+using ST_CUBE_MES.Service;
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Data;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace ST_CUBE_MES
+{
+    /// <summary>
+    /// App.xaml 的互動邏輯
+    /// </summary>
+    public partial class App : Application
+    {
+        public App()
+        {
+            AppSettingService.Instance.Load();
+        }
+
+        protected override void OnExit(ExitEventArgs e)
+        {
+            var stopResult = SajetConnect.SajetTransClose();
+
+            base.OnExit(e);
+        }
+    }
+}

+ 74 - 0
ST-CUBE_MES/LoginWindow.xaml

@@ -0,0 +1,74 @@
+<Window
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:local="clr-namespace:ST_CUBE_MES"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    Title="Login"
+    Width="400"
+    Height="250"
+    ResizeMode="NoResize"
+    WindowStartupLocation="CenterScreen"
+    WindowStyle="ToolWindow"
+    mc:Ignorable="d"
+    x:Class="ST_CUBE_MES.LoginWindow">
+    <Grid>
+        <Grid.RowDefinitions>
+            <RowDefinition />
+            <RowDefinition Height="50" />
+        </Grid.RowDefinitions>
+        <Grid Margin="10" Background="SkyBlue">
+            <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
+                <Grid.ColumnDefinitions>
+                    <ColumnDefinition Width="100" />
+                    <ColumnDefinition />
+                </Grid.ColumnDefinitions>
+                <Grid.RowDefinitions>
+                    <RowDefinition />
+                    <RowDefinition Height="10" />
+                    <RowDefinition />
+                </Grid.RowDefinitions>
+                <Label Content="員工代號" />
+                <Label Grid.Row="2" Content="工單號碼" />
+                <TextBox
+                    x:Name="uxIdBox"
+                    Grid.Column="1"
+                    Width="200"
+                    VerticalContentAlignment="Center" />
+                <TextBox
+                    x:Name="uxWorkOrderBox"
+                    Grid.Row="2"
+                    Grid.Column="1"
+                    Width="200"
+                    VerticalContentAlignment="Center" />
+            </Grid>
+            <Label
+                x:Name="uxErrmsg"
+                HorizontalAlignment="Center"
+                VerticalAlignment="Bottom"
+                Content="ErrorMsg"
+                Foreground="Red" />
+        </Grid>
+        <Grid Grid.Row="1">
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition />
+                <ColumnDefinition />
+            </Grid.ColumnDefinitions>
+            <Button
+                Width="100"
+                Height="30"
+                HorizontalAlignment="Center"
+                VerticalAlignment="Center"
+                Click="Connect_Click"
+                Content="連接" />
+            <Button
+                Grid.Column="1"
+                Width="100"
+                Height="30"
+                HorizontalAlignment="Center"
+                VerticalAlignment="Center"
+                Click="Exit_Click"
+                Content="離開" />
+        </Grid>
+    </Grid>
+</Window>

+ 149 - 0
ST-CUBE_MES/LoginWindow.xaml.cs

@@ -0,0 +1,149 @@
+using MesAdaptor;
+using ST_CUBE_MES.Service;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+
+namespace ST_CUBE_MES
+{
+    /// <summary>
+    /// LoginWindow.xaml 的互動邏輯
+    /// </summary>
+    public partial class LoginWindow : Window
+    {
+        public LoginWindow()
+        {
+            InitializeComponent();
+
+            uxErrmsg.Content = "";
+
+            SajetConnect.SetMes(AppSettingService.Instance.MES, AppSettingService.Instance.MechineCode);
+            SajetConnect.SajetTransStart();
+        }
+
+        public string UserId
+        {
+            get => uxIdBox.Text;
+            set => uxIdBox.Text = value;
+        }
+        public string WorkOrder
+        {
+            get => uxWorkOrderBox.Text;
+            set => uxWorkOrderBox.Text = value;
+        }
+
+        //protected override void OnClosing(CancelEventArgs e)
+        //{
+        //    if (DialogResult == null)
+        //    {
+        //        SajetConnect.SetMes(null);
+        //        Application.Current.Shutdown();
+        //    }
+        //    base.OnClosing(e);
+        //}
+
+        private void Exit_Click(object sender, RoutedEventArgs e)
+        {
+            this.Close();
+        }
+
+        private void Connect_Click(object sender, RoutedEventArgs e)
+        {
+            if (!CheckConnect())
+            {
+                return;
+            }
+
+            this.Hide();
+
+            var mainWindow = new MainWindow()
+            {
+                UserId = UserId,
+                WorkOrder = WorkOrder
+            };
+            mainWindow.Closed += MainWindow_Closed;
+            mainWindow.Owner = this;
+            mainWindow.ShowDialog();
+        }
+
+        private void MainWindow_Closed(object sender, EventArgs e)
+        {
+            this.Show();
+            WorkOrder = "";
+        }
+
+        private bool CheckConnect()
+        {
+            uxErrmsg.Content = "";
+
+            if (string.IsNullOrEmpty(uxIdBox.Text))
+            {
+                uxErrmsg.Content = "員工代號不可為空";
+                return false;
+            }
+
+            string id = uxIdBox.Text;
+            string idBackup = id;
+
+            if (CheckIsMesDisableAccount())
+            {
+                //SajetConnect.IsEmsEnabled = false;
+                SajetConnect.SetMes("test");
+                return true;
+            }
+
+            if (!SajetConnect.SajetTransSignIn(ref id))
+            {
+                if (id.StartsWith(idBackup))
+                {
+                    //data not changed
+                    uxErrmsg.Content = "MES 連線失敗";// "ID Error";
+                }
+                else
+                {
+                    uxErrmsg.Content = "員工代號不存在";// "ID Error";
+                }
+                return false;
+            }
+
+            if (string.IsNullOrEmpty(id) || id.StartsWith("NG"))
+            {
+                uxErrmsg.Content = "員工代號不存在";
+                return false;
+            }
+
+            if (!string.IsNullOrEmpty(uxWorkOrderBox.Text))
+            {
+                string workOrder = uxWorkOrderBox.Text;
+                if (!SajetConnect.SajetTransWoCheck(ref workOrder))
+                {
+                    uxErrmsg.Content = "工單號錯誤";
+                    return false;
+                }
+
+                if (string.IsNullOrEmpty(workOrder) || workOrder.StartsWith("NG"))
+                {
+                    uxErrmsg.Content = "工單號錯誤";
+                    return false;
+                }
+            }
+
+            return true;
+        }
+
+        private bool CheckIsMesDisableAccount()
+        {
+            return uxIdBox.Text == "Admin" && uxWorkOrderBox.Text == "Admin21896826";
+        }
+    }
+}

+ 101 - 0
ST-CUBE_MES/MainWindow.xaml

@@ -0,0 +1,101 @@
+<Window
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:local="clr-namespace:ST_CUBE_MES"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    Title="ST-CUBE_MES"
+    Width="800"
+    Height="450"
+    FontSize="12"
+    Background="#FF646464"
+    Foreground="White"
+    WindowStartupLocation="CenterScreen"
+    mc:Ignorable="d"
+    x:Class="ST_CUBE_MES.MainWindow">
+    <Grid Margin="10">
+        <Grid.RowDefinitions>
+            <RowDefinition Height="Auto" />
+            <RowDefinition Height="Auto" />
+            <RowDefinition Height="*" />
+        </Grid.RowDefinitions>
+        <GroupBox Grid.Row="0" Header="User Setting">
+            <Grid Margin="5">
+                <Grid.RowDefinitions>
+                    <RowDefinition />
+                    <RowDefinition Height="5" />
+                    <RowDefinition />
+                    <RowDefinition Height="5" />
+                    <RowDefinition />
+                </Grid.RowDefinitions>
+                <Grid.ColumnDefinitions>
+                    <ColumnDefinition Width="70" />
+                    <ColumnDefinition />
+                </Grid.ColumnDefinitions>
+                <TextBlock
+                    Grid.Row="2"
+                    VerticalAlignment="Center"
+                    Text="SN" />
+                <TextBlock
+                    Grid.Row="4"
+                    VerticalAlignment="Center"
+                    Text="Bin File" />
+                <TextBox
+                    x:Name="uxSN"
+                    Grid.Row="2"
+                    Grid.Column="1"
+                    Height="30"
+                    VerticalContentAlignment="Center" />
+                <Grid Grid.Row="4" Grid.Column="1">
+                    <Grid.ColumnDefinitions>
+                        <ColumnDefinition Width="29*" />
+                        <ColumnDefinition Width="287*" />
+                        <ColumnDefinition Width="50" />
+                    </Grid.ColumnDefinitions>
+                    <TextBox
+                        x:Name="uxBinFilePath"
+                        Grid.ColumnSpan="2"
+                        Height="30"
+                        Margin="0,5"
+                        VerticalContentAlignment="Center" />
+                    <Button
+                        x:Name="uxBinFileBtn"
+                        Grid.Column="2"
+                        Width="40"
+                        Height="40"
+                        Margin="5,0"
+                        Click="uxBinFileBtn_Click"
+                        Content="." />
+                </Grid>
+            </Grid>
+        </GroupBox>
+        <Grid
+            Grid.Row="1"
+            Height="30"
+            Margin="5">
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition />
+                <ColumnDefinition Width="30" />
+                <ColumnDefinition Width="100" />
+            </Grid.ColumnDefinitions>
+            <ProgressBar
+                x:Name="uxProgressBar"
+                Height="10"
+                VerticalAlignment="Center" />
+            <TextBlock
+                Grid.Column="1"
+                HorizontalAlignment="Center"
+                VerticalAlignment="Center"
+                Text="0%" />
+            <Button
+                x:Name="uxRunBtn"
+                Grid.Column="2"
+                Background="LightSkyBlue"
+                Click="uxRunBtn_Click"
+                Content="Run" />
+        </Grid>
+        <GroupBox Grid.Row="2" Header="Terminal">
+            <RichTextBox x:Name="uxTerminal" Background="Black" />
+        </GroupBox>
+    </Grid>
+</Window>

+ 357 - 0
ST-CUBE_MES/MainWindow.xaml.cs

@@ -0,0 +1,357 @@
+using MesAdaptor;
+using Microsoft.Win32;
+using ST_CUBE_MES.Service;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace ST_CUBE_MES
+{
+    /// <summary>
+    /// MainWindow.xaml 的互動邏輯
+    /// </summary>
+    public partial class MainWindow : Window
+    {
+        public MainWindow()
+        {
+            InitializeComponent();
+
+            Title = Title + string.Format(" V{0}", Assembly.GetEntryAssembly().GetName().Version);
+
+            this.stlinkService = new STLinkCliPrograrmService(
+                AppSettingService.Instance.STCubeCliPath,
+                AppSettingService.Instance.Port,
+                AppSettingService.Instance.CustomDefaultOptions
+                );
+            this.fileRrecordService = new FileRrecordService();
+            //this.loginWindow = new LoginWindow();
+            this.resultDialog = new ResultDialog();
+
+            Loaded += MainWindow_Loaded;
+        }
+
+        private delegate void GenericDelegate();
+        //private readonly LoginWindow loginWindow;
+        private ResultDialog resultDialog;
+        private readonly STLinkCliPrograrmService stlinkService;
+        private readonly FileRrecordService fileRrecordService;
+
+        public string UserId { get; internal set; }
+        public string WorkOrder { get; internal set; }
+
+        protected override void OnClosing(CancelEventArgs e)
+        {
+            base.OnClosing(e);
+
+            var currentSetting = AppSettingService.Instance;
+            var setting = new AppSetting()
+            {
+                LOCK = currentSetting.LOCK,
+                MES = currentSetting.MES,
+                MechineCode = currentSetting.MechineCode,
+                BinPath = uxBinFilePath.Text,
+                STCubeCliPath = currentSetting.STCubeCliPath,
+                Port = currentSetting.Port,
+                CustomDefaultOptions = currentSetting.CustomDefaultOptions,
+            };
+            currentSetting.Save(setting);
+        }
+
+        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
+        {
+            var stopResult = SajetConnect.SajetTransStart();
+
+            //loginWindow.Owner = this;
+            resultDialog.Owner = this;
+
+            //Reset();
+
+            uxBinFilePath.Text = AppSettingService.Instance.BinPath;
+            if (string.IsNullOrEmpty(AppSettingService.Instance.STCubeCliPath))
+            {
+                MessageBox.Show("STCubeCliPath should not be empty");
+            }
+
+            if (AppSettingService.Instance.LOCK)
+            {
+                uxBinFilePath.IsEnabled = false;
+                uxBinFileBtn.IsEnabled = false;
+            }
+        }
+
+        private void uxBinFileBtn_Click(object sender, RoutedEventArgs e)
+        {
+            OpenFileDialog dlg = new OpenFileDialog();
+            var dlgRsult = dlg.ShowDialog();
+
+            if (dlgRsult != true)
+            {
+                return;
+            }
+
+            uxBinFilePath.Text = dlg.FileName;
+        }
+
+        private async void uxRunBtn_Click(object sender, RoutedEventArgs e)
+        {
+            uxRunBtn.IsEnabled = false;
+            try
+            {
+                uxTerminal.Document.Blocks.Clear();
+
+                if (string.IsNullOrEmpty(uxSN.Text))
+                {
+                    AddTerminalMsg("ERROR: SN Should not be empty");
+                    return;
+                }
+
+                if (!ValidateSN())
+                {
+                    return;
+                }
+
+                string filePath = uxBinFilePath.Text;
+                if (!File.Exists(filePath))
+                {
+                    AddTerminalMsg("ERROR: Program file not found");
+                    return;
+                }
+
+                string cliPath = stlinkService.CliPath;
+                if (!File.Exists(filePath))
+                {
+                    AddTerminalMsg("ERROR: STM32_Programmer_CLI not found");
+                    return;
+                }
+
+                fileRrecordService.Start(uxSN.Text);
+                var task = new Task(() =>
+                {
+                    //GenericDelegate porgramFunc;
+                    //porgramFunc = () =>
+                    //{
+                    //    StartProgram(filePath);
+                    //};
+                    //this.Dispatcher.BeginInvoke(porgramFunc);
+                    StartProgram(filePath);
+                });
+                task.Start();
+                await task;
+            }
+            catch (Exception error)
+            {
+                MessageBox.Show(error.Message);
+            }
+            finally
+            {
+                uxRunBtn.IsEnabled = true;
+            }
+        }
+
+        private void StartProgram(string filePath)
+        {
+            UpdateProgressBar(0, 100);
+
+            stlinkService.OnMsgReceviced += StlinkService_OnMsgReceviced;
+            stlinkService.OnProgressChanged += StlinkService_OnProgressChanged;
+
+            var result = stlinkService.StartProgramProcess(filePath);
+
+            stlinkService.OnMsgReceviced -= StlinkService_OnMsgReceviced;
+            stlinkService.OnProgressChanged -= StlinkService_OnProgressChanged;
+
+            if (result is null)
+            {
+                //report empty error
+                SajetConnect.SajetTranFinishFail(MesErrorCode.ProgramFail);
+                return;
+            }
+
+            if (!string.IsNullOrEmpty(result.ErrorMsg))
+            {
+                fileRrecordService.Log(result.ErrorMsg);
+                AddTerminalMsg(result.ErrorMsg);
+            }
+
+            var reportDatas = new ValueReportDatas();
+            foreach (var logData in result.Data)
+            {
+                reportDatas.Add(logData.Key, logData.Value, true);
+            }
+            this.Dispatcher.Invoke(() => {
+                SajetConnect.SajetTransReport(reportDatas);
+            });
+
+            if (!result.IsSuccess)
+            {
+                var errCode = GetErrorCode(result.Step);
+                //report
+                this.Dispatcher.Invoke(() => {
+                    SajetConnect.SajetTranFinishFail(errCode);
+
+
+                    resultDialog = new ResultDialog();
+                    resultDialog.Owner = this;
+
+                    resultDialog.MouseDown += ResultDialog_MouseDown;
+                    resultDialog.ShowResult(false);
+                });
+            }
+
+            if (result.IsSuccess)
+            {
+                //report
+                this.Dispatcher.Invoke(() => {
+                    SajetConnect.SajetTranFinishSuccess();
+
+
+                    resultDialog = new ResultDialog();
+                    resultDialog.Owner = this;
+
+                    resultDialog.MouseDown += ResultDialog_MouseDown;
+                    resultDialog.ShowResult(true);
+                });
+            }
+
+            fileRrecordService.Complete();
+        }
+
+        private MesErrorCode GetErrorCode(int step)
+        {
+            switch (step)
+            {
+                case 0:
+                    return MesErrorCode.None;
+                case 1:
+                    return MesErrorCode.EraseFail;
+                case 2:
+                    return MesErrorCode.ProgramFail;
+                case 3:
+                    return MesErrorCode.GetCheckSumFail;
+                default:
+                    return MesErrorCode.None;
+            }
+        }
+
+        private void ResultDialog_MouseDown(object sender, MouseButtonEventArgs e)
+        {
+            resultDialog.MouseDown -= ResultDialog_MouseDown;
+            resultDialog.Close();
+            //this.Close();
+        }
+
+        private void ResultDialog_Closing(object sender, CancelEventArgs e)
+        {
+            resultDialog.Closing -= ResultDialog_Closing;
+            //this.Close();
+        }
+
+        private bool ValidateSN()
+        {
+            string tmpSN;
+            bool bResult = false;
+            tmpSN = string.Format("{0}", uxSN.Text);
+
+            var mesSetting = AppSettingService.Instance.MES.ToLower();
+            if (mesSetting == "php" || mesSetting == "shinewave")
+            {
+                bResult = SajetConnect.SajetTransSnCheck(ref tmpSN);
+                if (!bResult)
+                {
+                    MessageBox.Show("SN not found");
+                }
+                return bResult;
+            }
+            else if (mesSetting == "phv" || mesSetting == "sajet2")
+            {
+                bResult = SajetConnect.SajetTransSnCheck(ref tmpSN, "");
+                if (!bResult)
+                {
+                    MessageBox.Show("SN not found");
+                }
+                return bResult;
+            }
+            else if (mesSetting == "test")
+            {
+                return true;
+            }
+
+            MessageBox.Show("Not supported MES");
+            return false;
+        }
+
+        private void StlinkService_OnProgressChanged(int currentStep, int maxStep)
+        {
+            UpdateProgressBar(currentStep, maxStep);
+        }
+
+        private void StlinkService_OnMsgReceviced(string msg)
+        {
+            AddTerminalMsg(msg);
+            fileRrecordService.Log(msg);
+        }
+
+        private void AddTerminalMsg(string msg)
+        {
+            GenericDelegate onMsgReceviced;
+            onMsgReceviced = () =>
+            {
+                var paragraph = new Paragraph();
+                paragraph.Inlines.Add(GetRun(msg));
+                uxTerminal.Document.Blocks.Add(paragraph);
+                uxTerminal.ScrollToEnd();
+            };
+
+            this.Dispatcher.BeginInvoke(onMsgReceviced);
+        }
+
+        private void UpdateProgressBar(int currentStep, int maxStep)
+        {
+            GenericDelegate onUpdateProgressBar;
+            onUpdateProgressBar = () =>
+            {
+                uxProgressBar.Maximum = maxStep;
+                uxProgressBar.Value = currentStep;
+            };
+
+            this.Dispatcher.BeginInvoke(onUpdateProgressBar);
+        }
+
+        private Run GetRun(string msg)
+        {
+            if (msg is null)
+            {
+                msg = "";
+            }
+
+            Color textColor = Colors.White;
+            if (msg.StartsWith("ERROR:") ||
+                msg == "Unable to connect to ST-LINK!")
+            {
+                textColor = Colors.Red;
+            }
+            if (msg == "Programming Complete." ||
+                msg == "No difference found." ||
+                msg == "MCU Reset." ||
+                msg.StartsWith("checksum:"))
+            {
+                textColor = Colors.LightGreen;
+            }
+            return new Run() { Text = msg, Foreground = new SolidColorBrush(textColor) };
+        }
+    }
+}

+ 16 - 0
ST-CUBE_MES/Model/Result.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ST_CUBE_MES.Model
+{
+    public class Result
+    {
+        public int Step { get; set; }
+        public bool IsSuccess { get; set; }
+        public Dictionary<string, string> Data { get; set; } = new Dictionary<string, string>();
+        public string ErrorMsg { get; set; }
+    }
+}

+ 55 - 0
ST-CUBE_MES/Properties/AssemblyInfo.cs

@@ -0,0 +1,55 @@
+using System.Reflection;
+using System.Resources;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Windows;
+
+// 組件的一般資訊是由下列的屬性集控制。
+// 變更這些屬性的值即可修改組件的相關
+// 資訊。
+[assembly: AssemblyTitle("ST-CUBE_MES")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("ST-CUBE_MES")]
+[assembly: AssemblyCopyright("Copyright ©  2024")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// 將 ComVisible 設為 false 可對 COM 元件隱藏
+// 組件中的類型。若必須從 COM 存取此組件中的類型,
+// 的類型,請在該類型上將 ComVisible 屬性設定為 true。
+[assembly: ComVisible(false)]
+
+//若要開始建置可當地語系化的應用程式,請在
+//.csproj 檔案中的 <UICulture>CultureYouAreCodingWith</UICulture>
+//在 <PropertyGroup> 中。例如,如果原始程式檔使用美式英文, 
+//請將 <UICulture> 設為 en-US。然後取消註解下列
+//NeutralResourceLanguage 屬性。在下一行中更新 "en-US",
+//以符合專案檔中的 UICulture 設定。
+
+//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
+
+
+[assembly: ThemeInfo(
+    ResourceDictionaryLocation.None, //主題特定資源字典的位置
+                                     //(在頁面中找不到時使用,
+                                     // 或應用程式資源字典中找不到資源時)
+    ResourceDictionaryLocation.SourceAssembly //泛型資源字典的位置
+                                              //(在頁面中找不到時使用,
+                                              // 或是應用程式或任何主題特定資源字典中找不到資源時)
+)]
+
+
+// 組件的版本資訊由下列四個值所組成:
+//
+//      主要版本
+//      次要版本
+//      組建編號
+//      修訂
+//
+// 您可以指定所有的值,也可以使用 '*' 將組建和修訂編號
+// 設為預設,如下所示:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 71 - 0
ST-CUBE_MES/Properties/Resources.Designer.cs

@@ -0,0 +1,71 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+//     這段程式碼是由工具產生的。
+//     執行階段版本:4.0.30319.42000
+//
+//     變更這個檔案可能會導致不正確的行為,而且如果已重新產生
+//     程式碼,則會遺失變更。
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace ST_CUBE_MES.Properties
+{
+
+
+    /// <summary>
+    ///   用於查詢當地語系化字串等的強類型資源類別
+    /// </summary>
+    // 這個類別是自動產生的,是利用 StronglyTypedResourceBuilder
+    // 類別透過 ResGen 或 Visual Studio 這類工具產生。
+    // 若要加入或移除成員,請編輯您的 .ResX 檔,然後重新執行 ResGen
+    // (利用 /str 選項),或重建您的 VS 專案。
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
+    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+    internal class Resources
+    {
+
+        private static global::System.Resources.ResourceManager resourceMan;
+
+        private static global::System.Globalization.CultureInfo resourceCulture;
+
+        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
+        internal Resources()
+        {
+        }
+
+        /// <summary>
+        ///   傳回這個類別使用的快取的 ResourceManager 執行個體。
+        /// </summary>
+        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+        internal static global::System.Resources.ResourceManager ResourceManager
+        {
+            get
+            {
+                if ((resourceMan == null))
+                {
+                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ST_CUBE_MES.Properties.Resources", typeof(Resources).Assembly);
+                    resourceMan = temp;
+                }
+                return resourceMan;
+            }
+        }
+
+        /// <summary>
+        ///   覆寫目前執行緒的 CurrentUICulture 屬性,對象是所有
+        ///   使用這個強類型資源類別的資源查閱。
+        /// </summary>
+        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+        internal static global::System.Globalization.CultureInfo Culture
+        {
+            get
+            {
+                return resourceCulture;
+            }
+            set
+            {
+                resourceCulture = value;
+            }
+        }
+    }
+}

+ 117 - 0
ST-CUBE_MES/Properties/Resources.resx

@@ -0,0 +1,117 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+</root>

+ 30 - 0
ST-CUBE_MES/Properties/Settings.Designer.cs

@@ -0,0 +1,30 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+//     This code was generated by a tool.
+//     Runtime Version:4.0.30319.42000
+//
+//     Changes to this file may cause incorrect behavior and will be lost if
+//     the code is regenerated.
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace ST_CUBE_MES.Properties
+{
+
+
+    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
+    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
+    {
+
+        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
+
+        public static Settings Default
+        {
+            get
+            {
+                return defaultInstance;
+            }
+        }
+    }
+}

+ 7 - 0
ST-CUBE_MES/Properties/Settings.settings

@@ -0,0 +1,7 @@
+<?xml version='1.0' encoding='utf-8'?>
+<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
+  <Profiles>
+    <Profile Name="(Default)" />
+  </Profiles>
+  <Settings />
+</SettingsFile>

+ 25 - 0
ST-CUBE_MES/ResultDialog.xaml

@@ -0,0 +1,25 @@
+<Window
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:local="clr-namespace:ST_CUBE_MES"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    Title="ResultDialog"
+    Width="400"
+    Height="250"
+    ResizeMode="NoResize"
+    WindowStartupLocation="CenterOwner"
+    WindowStyle="None"
+    mc:Ignorable="d"
+    x:Class="ST_CUBE_MES.ResultDialog">
+    <Grid x:Name="uxBg" Background="Lime">
+        <Label
+            x:Name="uxResultText"
+            HorizontalAlignment="Center"
+            VerticalAlignment="Center"
+            FontSize="72"
+            Content="PASS"
+            FontWeight="Bold"
+            Foreground="White" />
+    </Grid>
+</Window>

+ 48 - 0
ST-CUBE_MES/ResultDialog.xaml.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+
+namespace ST_CUBE_MES
+{
+    /// <summary>
+    /// Window1.xaml 的互動邏輯
+    /// </summary>
+    public partial class ResultDialog : Window
+    {
+        public ResultDialog()
+        {
+            InitializeComponent();
+        }
+
+        private delegate void GenericDelegate();
+
+        public void ShowResult(bool isSuccess)
+        {
+            GenericDelegate ShowResultAct = () => {
+                if (isSuccess)
+                {
+                    uxBg.Background = Brushes.Lime;
+                    uxResultText.Content = "PASS";
+                }
+
+                if (!isSuccess)
+                {
+                    uxBg.Background = Brushes.Red;
+                    uxResultText.Content = "FAIL";
+                }
+                this.ShowDialog();
+            };
+            this.Dispatcher.BeginInvoke(ShowResultAct);
+        }
+    }
+}

+ 127 - 0
ST-CUBE_MES/ST-CUBE_MES.csproj

@@ -0,0 +1,127 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{A968FD3F-5374-442B-B087-3385764235AD}</ProjectGuid>
+    <OutputType>WinExe</OutputType>
+    <RootNamespace>ST_CUBE_MES</RootNamespace>
+    <AssemblyName>ST-CUBE_MES</AssemblyName>
+    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <WarningLevel>4</WarningLevel>
+    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+    <Deterministic>true</Deterministic>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
+      <HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
+    </Reference>
+    <Reference Include="System" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="System.Net.Http" />
+    <Reference Include="System.Xaml">
+      <RequiredTargetFramework>4.0</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="WindowsBase" />
+    <Reference Include="PresentationCore" />
+    <Reference Include="PresentationFramework" />
+  </ItemGroup>
+  <ItemGroup>
+    <ApplicationDefinition Include="App.xaml">
+      <Generator>MSBuild:Compile</Generator>
+      <SubType>Designer</SubType>
+    </ApplicationDefinition>
+    <Compile Include="LoginWindow.xaml.cs">
+      <DependentUpon>LoginWindow.xaml</DependentUpon>
+    </Compile>
+    <Compile Include="Model\Result.cs" />
+    <Compile Include="Service\AppSettingService.cs" />
+    <Compile Include="Service\FileRrecordService.cs" />
+    <Compile Include="Service\STLinkCliPrograrmService.cs" />
+    <Compile Include="Service\STLinkCliWrapService.cs" />
+    <Compile Include="ResultDialog.xaml.cs">
+      <DependentUpon>ResultDialog.xaml</DependentUpon>
+    </Compile>
+    <Page Include="LoginWindow.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
+    <Page Include="MainWindow.xaml">
+      <Generator>MSBuild:Compile</Generator>
+      <SubType>Designer</SubType>
+    </Page>
+    <Compile Include="App.xaml.cs">
+      <DependentUpon>App.xaml</DependentUpon>
+      <SubType>Code</SubType>
+    </Compile>
+    <Compile Include="MainWindow.xaml.cs">
+      <DependentUpon>MainWindow.xaml</DependentUpon>
+      <SubType>Code</SubType>
+    </Compile>
+    <Page Include="ResultDialog.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Properties\AssemblyInfo.cs">
+      <SubType>Code</SubType>
+    </Compile>
+    <Compile Include="Properties\Resources.Designer.cs">
+      <AutoGen>True</AutoGen>
+      <DesignTime>True</DesignTime>
+      <DependentUpon>Resources.resx</DependentUpon>
+    </Compile>
+    <Compile Include="Properties\Settings.Designer.cs">
+      <AutoGen>True</AutoGen>
+      <DependentUpon>Settings.settings</DependentUpon>
+      <DesignTimeSharedInput>True</DesignTimeSharedInput>
+    </Compile>
+    <EmbeddedResource Include="Properties\Resources.resx">
+      <Generator>ResXFileCodeGenerator</Generator>
+      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
+    </EmbeddedResource>
+    <None Include="packages.config" />
+    <None Include="Properties\Settings.settings">
+      <Generator>SettingsSingleFileGenerator</Generator>
+      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
+    </None>
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\MesAdaptor\MesAdaptor.csproj">
+      <Project>{6b69cf23-270b-429b-a21d-ad98c2dff678}</Project>
+      <Name>MesAdaptor</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project>

+ 87 - 0
ST-CUBE_MES/Service/AppSettingService.cs

@@ -0,0 +1,87 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace ST_CUBE_MES.Service
+{
+    public class AppSettingService
+    {
+        private static AppSettingService _Instance;
+        public static AppSettingService Instance
+        {
+            get
+            {
+                if (_Instance == null)
+                {
+                    _Instance = new AppSettingService();
+                }
+                return _Instance;
+            }
+        }
+
+        private AppSettingService()
+        {
+
+        }
+
+        public string MES => appSetting?.MES;
+        public string MechineCode => appSetting?.MechineCode;
+        public bool LOCK => appSetting == null ? false : appSetting.LOCK;
+        public string STCubeCliPath => appSetting?.STCubeCliPath;
+        public string BinPath => appSetting?.BinPath;
+        public string Port => appSetting?.Port;
+        public string CustomDefaultOptions => appSetting?.CustomDefaultOptions;
+
+        private const string settingFileName = "Settings.ini";
+        private AppSetting appSetting;
+
+        public void Load()
+        {
+            if (!File.Exists(settingFileName))
+            {
+                appSetting = new AppSetting()
+                {
+                    MES = "phv",
+                };
+                return;
+            }
+
+            try
+            {
+                appSetting = JsonConvert.DeserializeObject<AppSetting>(File.ReadAllText(settingFileName));
+            }
+            catch
+            {
+                MessageBox.Show($"{settingFileName} format ERROR");
+            }
+        }
+
+        public void Save(AppSetting setting)
+        {
+            if (File.Exists(settingFileName))
+            {
+                File.Delete(settingFileName);
+            }
+
+            File.WriteAllText(settingFileName, JsonConvert.SerializeObject(setting));
+
+            Load();
+        }
+    }
+
+    public class AppSetting
+    {
+        public string MES { get; set; }
+        public string MechineCode { get; set; }
+        public bool LOCK { get; set; }
+        public string STCubeCliPath { get; set; }
+        public string BinPath { get; set; }
+        public string Port { get; set; }
+        public string CustomDefaultOptions { get; set; }
+    }
+}

+ 51 - 0
ST-CUBE_MES/Service/FileRrecordService.cs

@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ST_CUBE_MES.Service
+{
+    public class FileRrecordService
+    {
+        private static string LogFolder = "Log";
+        //private string serialNumber;
+        private string fileName;
+
+        private string FilePath
+        {
+            get
+            {
+                if (string.IsNullOrEmpty(fileName))
+                {
+                    return null;
+                }
+
+                return Path.Combine(Directory.GetCurrentDirectory(), LogFolder, fileName);
+            }
+        }
+
+        public void Start(string serialNumber)
+        {
+            fileName = String.Format("{0}_{1}.txt", serialNumber, DateTime.Now.ToString("yyyyMMddHHmmss"));
+            //MessageBox.Show(FilePath);
+            //File.Create(FilePath);
+        }
+
+        public void Log(string logString)
+        {
+            if (string.IsNullOrEmpty(fileName))
+            {
+                return;
+            }
+            logString += Environment.NewLine;
+            File.AppendAllText(FilePath, logString);
+        }
+
+        public void Complete()
+        {
+            fileName = null;
+        }
+    }
+}

+ 263 - 0
ST-CUBE_MES/Service/STLinkCliPrograrmService.cs

@@ -0,0 +1,263 @@
+using ST_CUBE_MES.Model;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+
+namespace ST_CUBE_MES.Service
+{
+    public class STLinkCliPrograrmService
+    {
+        public STLinkCliPrograrmService(string stlCliPath = "", string port = "", string customDefaultOption = null)
+        {
+            this._cli = new STLinkCliWrapService(stlCliPath, port, customDefaultOption);
+        }
+
+        public delegate void OnMsgRecevicedEvent(string msg);
+        public event OnMsgRecevicedEvent OnMsgReceviced;
+
+        public delegate void OnProgressChangedEvent(int currentStep, int maxStep);
+        public event OnProgressChangedEvent OnProgressChanged;
+        public string CliPath => _cli.CliPath;
+
+        private readonly STLinkCliWrapService _cli;
+
+        public Result StartProgramProcess(string filePath)
+        {
+            Result result = new Result() { IsSuccess = false };
+
+            result.Step = 1;
+            var setOptionByteResult = SetOptionByte();
+            if (setOptionByteResult != null)
+            {
+                result.ErrorMsg = setOptionByteResult.ErrorMsg;
+                foreach (var pair in setOptionByteResult.Data)
+                {
+                    result.Data[pair.Key] = pair.Value;
+                }
+            }
+            if (setOptionByteResult == null || !setOptionByteResult.IsSuccess)
+            {
+                return result;
+            }
+            OnProgressChanged?.Invoke(1, 4);
+
+            result.Step = 2;
+            var eraseResult = Erase();
+            if (eraseResult != null)
+            {
+                result.ErrorMsg = eraseResult.ErrorMsg;
+                foreach (var pair in eraseResult.Data)
+                {
+                    result.Data[pair.Key] = pair.Value;
+                }
+            }
+            if (eraseResult == null || !eraseResult.IsSuccess)
+            {
+                return result;
+            }
+            OnProgressChanged?.Invoke(2, 4);
+
+            result.Step = 3;
+            var startProgramResult = StartProgram(filePath);
+            if (startProgramResult != null)
+            {
+                result.ErrorMsg = startProgramResult.ErrorMsg;
+                foreach (var pair in startProgramResult.Data)
+                {
+                    result.Data[pair.Key] = pair.Value;
+                }
+            }
+            if (startProgramResult == null || !startProgramResult.IsSuccess)
+            {
+                return result;
+            }
+            OnProgressChanged?.Invoke(3, 4);
+
+            result.Step = 4;
+            var getCheckSumResult = GetCheckSum(filePath);
+            if (getCheckSumResult != null)
+            {
+                result.ErrorMsg = getCheckSumResult.ErrorMsg;
+                foreach (var pair in getCheckSumResult.Data)
+                {
+                    result.Data[pair.Key] = pair.Value;
+                }
+            }
+            if (getCheckSumResult == null || !getCheckSumResult.IsSuccess)
+            {
+                return result;
+            }
+            OnProgressChanged?.Invoke(4, 4);
+
+            result.IsSuccess = true;
+            return result;
+        }
+
+        public Result SetOptionByte()
+        {
+            var result = new Result() { IsSuccess = true };
+
+            _cli.OnMsgReceviced += stlCli_OnMsgReceviced_SetDefaultOptions;
+            var resultInt = _cli.SetOptionByte();
+            _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_SetDefaultOptions;
+
+            result.IsSuccess = resultInt == 0;
+            return result;
+
+            void stlCli_OnMsgReceviced_SetDefaultOptions(string msg)
+            {
+                if (msg is null)
+                    return;
+
+                if (msg.Contains("Error: No debug probe detected"))
+                {
+                    result.ErrorMsg = "Error: No debug probe detected";
+                }
+                OnMsgReceviced?.Invoke(msg);
+            }
+        }
+
+        public Result Erase()
+        {
+            var result = new Result() { IsSuccess = true };
+
+            _cli.OnMsgReceviced += stlCli_OnMsgReceviced_SetDefaultOptions;
+            var resultInt = _cli.Erase();
+            _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_SetDefaultOptions;
+
+            result.IsSuccess = resultInt == 0;
+            return result;
+
+            void stlCli_OnMsgReceviced_SetDefaultOptions(string msg)
+            {
+                if (msg is null)
+                    return;
+
+                if (msg.Contains("Error: No debug probe detected"))
+                {
+                    result.ErrorMsg = "Error: No debug probe detected";
+                }
+                OnMsgReceviced?.Invoke(msg);
+            }
+        }
+
+        public Result GetCheckSum(string filePath)
+        {
+            if (!File.Exists(filePath))
+            {
+                return new Result()
+                {
+                    IsSuccess = false,
+                    ErrorMsg = "ERROR: Check file not found",
+                };
+            }
+
+            var result = new Result() { IsSuccess = true };
+
+            _cli.OnMsgReceviced += stlCli_OnMsgReceviced_StartProgram;
+            var resultInt = _cli.GetCheckSum(filePath);
+            _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_StartProgram;
+
+            result.IsSuccess = resultInt == 0;
+            return result;
+
+            void stlCli_OnMsgReceviced_StartProgram(string msg)
+            {
+                if (msg is null)
+                    return;
+
+                Regex checksumRegex = new Regex("^(.*)Checksum = (0x.*)$");
+                var match = checksumRegex.Match(msg);
+                if (match.Success &&
+                    match.Groups.Count > 2 &&
+                    match.Groups[2].Value.StartsWith("0x"))
+                {
+                    var checksum = match.Groups[2].Value;
+                    result.Data.Add("checksum", checksum);
+                    result.Data.Add("fileName", Path.GetFileName(filePath));
+
+                    OnMsgReceviced?.Invoke($"checksum: {checksum}");
+                }
+
+                if (msg.Contains("Error: No debug probe detected"))
+                {
+                    result.ErrorMsg = "Error: No debug probe detected";
+                }
+                OnMsgReceviced?.Invoke(msg);
+            }
+        }
+
+        public Result StartProgram(string filePath)
+        {
+            if (!File.Exists(filePath))
+            {
+                return new Result()
+                {
+                    IsSuccess = false,
+                    ErrorMsg = "ERROR: Program file not found",
+                };
+            }
+
+            var result = new Result() { IsSuccess = true };
+
+            _cli.OnMsgReceviced += stlCli_OnMsgReceviced_StartProgram;
+            var resultInt = _cli.StartProgram(filePath);
+            _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_StartProgram;
+
+            result.IsSuccess = resultInt == 0;
+            return result;
+
+            void stlCli_OnMsgReceviced_StartProgram(string msg)
+            {
+                if (msg is null)
+                    return;
+
+                if (msg.Contains("Error: No debug probe detected"))
+                {
+                    result.ErrorMsg = "Error: No debug probe detected";
+                }
+                OnMsgReceviced?.Invoke(msg);
+            }
+        }
+
+        public Result GetMemCheckSum()
+        {
+            var result = new Result() { IsSuccess = true };
+
+            _cli.OnMsgReceviced += stlCli_OnMsgReceviced_SetDefaultOptions;
+            var resultInt = _cli.GetMemCheckSum();
+            _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_SetDefaultOptions;
+
+            result.IsSuccess = resultInt == 0;
+            return result;
+
+            void stlCli_OnMsgReceviced_SetDefaultOptions(string msg)
+            {
+                if (msg is null)
+                    return;
+
+                Regex checksumRegex = new Regex("^(.*)Checksum : (0x.*)$");
+                var match = checksumRegex.Match(msg);
+                if (match.Success &&
+                    match.Groups.Count > 2 &&
+                    match.Groups[2].Value.StartsWith("0x"))
+                {
+                    var checksum = match.Groups[2].Value;
+                    //result.Data.Add("checksum", checksum);
+
+                    OnMsgReceviced?.Invoke($"memory checksum: {checksum}");
+                }
+
+                if (msg.Contains("Error: No debug probe detected"))
+                {
+                    result.ErrorMsg = "Error: No debug probe detected";
+                }
+                OnMsgReceviced?.Invoke(msg);
+            }
+        }
+    }
+}

+ 109 - 0
ST-CUBE_MES/Service/STLinkCliWrapService.cs

@@ -0,0 +1,109 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ST_CUBE_MES.Service
+{
+    public class STLinkCliWrapService
+    {
+        public STLinkCliWrapService(string cliPath, string port = "", string customDefaultOption = null)
+        {
+            if (string.IsNullOrEmpty(cliPath))
+            {
+                cliPath = "C:\\Program Files (x86)\\STMicroelectronics\\STM32 ST-LINK Utility\\ST-LINK Utility\\ST-LINK_CLI.exe";
+            }
+            this.cliPath = cliPath;
+
+            SetPortStringParam(port);
+
+            SetCustomDefaultOptionParam(customDefaultOption);
+        }
+
+        public delegate void OnMsgRecevicedEvent(string msg);
+        public event OnMsgRecevicedEvent OnMsgReceviced;
+
+        public string CliPath => cliPath;
+
+        private readonly string cliPath;
+        private string portParamString;
+        private string defaultOptionString;
+
+        public int SetOptionByte()
+        {
+            return RunConsole(cliPath, $"-c {portParamString} {defaultOptionString}");
+        }
+
+        public int Erase()
+        {
+            return RunConsole(cliPath, $"-c {portParamString} -e all");
+        }
+
+        public int StartProgram(string filePath)
+        {
+            return RunConsole(cliPath, $"-c {portParamString} -w \"{filePath}\" -v");
+        }
+
+        public int GetMemCheckSum()
+        {
+            return RunConsole(cliPath, $"-c {portParamString} -checksum");
+        }
+
+        public int GetCheckSum(string filePath)
+        {
+            return RunConsole(cliPath, $"-fchecksum \"{filePath}\"");
+        }
+
+        private int RunConsole(string exePath, string param)
+        {
+            var pInfo = new ProcessStartInfo()
+            {
+                FileName = exePath,
+                Arguments = param,
+                RedirectStandardOutput = true,
+                CreateNoWindow = true,
+                UseShellExecute = false,
+
+            };
+
+            var p = Process.Start(pInfo);
+            p.OutputDataReceived += P_OutputDataReceived;
+            p.BeginOutputReadLine();
+            p.WaitForExit();
+            p.Exited += P_Exited;
+            return p.ExitCode;
+        }
+
+        private void P_Exited(object sender, EventArgs e)
+        {
+
+        }
+
+        private void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
+        {
+            OnMsgReceviced?.Invoke(e.Data);
+        }
+
+        private void SetPortStringParam(string port)
+        {
+            if (string.IsNullOrEmpty(port))
+            {
+                portParamString = string.Empty;
+                return;
+            }
+            portParamString = $"port={port}";
+        }
+
+        private void SetCustomDefaultOptionParam(string customDefaultOption)
+        {
+            if (customDefaultOption != null)
+            {
+                defaultOptionString = customDefaultOption;
+                return;
+            }
+            defaultOptionString = "";
+        }
+    }
+}

+ 4 - 0
ST-CUBE_MES/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
+</packages>