yantian yue
2023-12-29 fdda48e034b2da82fb100ec04f5b51351477e2bb
YX/wt/Frmwt.cs
@@ -13,11 +13,11 @@
using System.Windows.Forms;
using System.Threading;
using System.Configuration;
using EasyModbus; // 引入EasyModbusTCP库 *****
using EasyModbus; // 引入EasyModbusTCP库
using System.IO.Ports;
using System.Net.NetworkInformation;
namespace YX.wt
namespace YX
{
    public partial class Frmwt : WindowParent
    {
@@ -34,6 +34,7 @@
        public string PSN = "";
        public string PSN2 = "";
        public string PSN3 = "";
        public bool t = true;
        public Frmwt(string ParentId)
        {
            InitializeComponent();
@@ -48,7 +49,7 @@
            {
                Console.WriteLine($"发生错误:{ex}");
            }
            dataGridView1.DataSource = System_Bll.GetSystemLog(FrmLogin.LocalIP);
            dataGridView1.DataSource = System_Bll.GetSystemLog(FrmLogin.LocalIP, "四码合一");
            macBox.Text = GetMacByNetworkInterface();
        }
@@ -58,16 +59,13 @@
            while (true)
            {
                ProcessDelegate showProcess = new ProcessDelegate(LabelShow);
                //string str = "1\n2\n3\n";
                //str = str.Replace("\n", string.Empty); // 将 \n 替换为空字符串
                string str = ModbusTCPRead(5, 1).Trim("\n".ToCharArray());
                if (ModbusTCPRead(5, 1).Trim("\n".ToCharArray()) == "1")
                {
                    try
                    {
                        //PSN = ModbusTCPRead(5, 20).Replace("\n", string.Empty);
                        PSN = ModbusTCPReadString(25, 30).Replace("\0", string.Empty);
                        PSN = ModbusTCPReadString(25, 30).Replace("\0", string.Empty); //将\0替换为空字符串
                        PSN2 = ModbusTCPReadString(45, 20).Replace("\0", string.Empty);
                        PSN3 = ModbusTCPReadString(65, 20).Replace("\0", string.Empty);
                        label1.Invoke(showProcess);
@@ -86,26 +84,24 @@
            ConnectServer(ipAddress, port1);
            int[] data = tcpClient.ReadHoldingRegisters(startingAddress, quantity); // 读取寄存器数据
            string strData = ""; // 存储字符串数据
            //string str = "";
            //string str1 = "";
            //string str2 = "";
            string str = "";
            string str1 = "";
            string str2 = "";
            for (int i = 0; i < quantity; i++)
            {
                //str= data[i].ToString("X");
                //if (str.Length == 4)
                //{
                //    str1 = str.Substring(2, 2);
                //    str2 = str.Substring(0, 2);
                //    strData += str1 + str2;
                //}
                //if (str.Length == 2)
                //{
                //    strData += str;
                //}
                strData += data[i].ToString("X");
                str = data[i].ToString("X");
                if (str.Length == 4)
                {
                    str1 = str.Substring(2, 2);
                    str2 = str.Substring(0, 2);
                    strData += str1 + str2;
                }
                if (str.Length == 2)
                {
                    strData += str;
                }
            }
            string str66 = hexStrToStr(strData);
            return hexStrToStr(strData); ;
        }
@@ -224,9 +220,8 @@
        private void LabelShow()
        {
            //string[] aaa = PSN.Split(',');
            //string bbbb = aaa[0];
            PSNBox.Text = PSN;
            string[] b_str = PSN.Split(',');
            PSNBox.Text = b_str[1];
            JObject jo = WI6027();
            if (jo == null)
@@ -237,7 +232,7 @@
                    CreateUserName = FrmLogin.loginUserName,
                    LocalIP = FrmLogin.LocalIP,
                    LogMessage = "WI6027调用失败!",
                    Type = "称重",
                    Type = "四码合一",
                    ClassName = typeof(FrmLogin).ToString()
                });
                return;
@@ -254,7 +249,7 @@
                JObject jo1032 = WI1032(PSNBox.Text, SNMBox.Text, KPNMBox.Text, KPValueBox.Text);
                if (jo1032 != null)
                {
                    if (WI6041(PSNBox.Text, SNMBox.Text, KPNMBox.Text, jo1032))
                    if (WI6041(PSNBox.Text, SNMBox.Text, KPNMBox.Text, b_str[0]))
                    {
                        System_Bll.WriteLogToDB(new Entity.Base_Log
                        {
@@ -268,7 +263,7 @@
                    }
                }
            }
            dataGridView1.DataSource = System_Bll.GetSystemLog(FrmLogin.LocalIP);
            dataGridView1.DataSource = System_Bll.GetSystemLog(FrmLogin.LocalIP, "四码合一");
            Refresh();
        }
@@ -309,9 +304,7 @@
        public JObject WI6027()
        {
            //string WI6027 = "{ \"user\":\"sys\",\"password\":\"sysuser\",\"method\":\"MES_BA_WI6027\",\"servername\":\"TEST_WTMES_Customer\",\"methodparameter\":{ \"mac\":\"6A:15:63:C3:CC:EA\",\"hostName\":\"liuxingliang-PC\"} }";
            string a = "D0:8E:79:02:CF:91";
            string a = "a0:36:9F:6c:0e:95";
            string WI6027 = "{";
            WI6027 += "\"user\":\"sys\",";
            WI6027 += "\"password\":\"sysuser\",";
@@ -319,17 +312,16 @@
            WI6027 += "\"servername\":\"London\",";
            WI6027 += "\"methodparameter\":{";
            WI6027 += "\"mac\":\"" + a + "\",";
            WI6027 += "\"hostName\":\"PC-20230103TQKE\"";
            WI6027 += "\"hostName\":\"DESKTOP-UV8I1I0\"";
            WI6027 += "}}";
            JObject jo = MES_Interface(WI6027, encryptionKey);
            return jo;
            return MES_Interface(WI6027, encryptionKey);
        }
        public bool WI601K(string BarCode)
        {
            //string WI601K = "{\"user\":\"sys\", \"password\":\"sysuser\",\"method\":\"MES_BA_WI601K\",\"servername\":\"TEST_WTMES_Customer\", \"methodparameter\":{\"BarCode\":\"123456789\", \"HostName\":\"liuxingliang-PC\", \"Mac\":\"6A:15:63:C3:CC:EA\" }}\n";
            string a = "a0:36:9F:6c:0e:95";
            string WI601K = "{";
            WI601K += "\"user\":\"sys\",";
            WI601K += "\"password\":\"sysuser\",";
@@ -337,21 +329,23 @@
            WI601K += "\"servername\":\"London\",";
            WI601K += "\"methodparameter\":{";
            WI601K += "\"BarCode\":\"" + BarCode + "\",";
            WI601K += "\"HostName\":\"liuxingliang-PC\",";
            WI601K += "\"Mac\":\"6A:15:63:C3:CC:EA\"";
            WI601K += "\"HostName\":\"DESKTOP-UV8I1I0\",";
            WI601K += "\"Mac\":\"" + a + "\"";
            WI601K += "}";
            WI601K += "}";
            JObject jo = MES_Interface(WI601K, encryptionKey);
            if (jo["state"].ToString() == "false")
            {
                t = false;
                System_Bll.WriteLogToDB(new Entity.Base_Log
                {
                    CreateUserID = FrmLogin.LoginUserID,
                    CreateUserName = FrmLogin.loginUserName,
                    LocalIP = FrmLogin.LocalIP,
                    LogMessage = "MES接口WI601K,条码比对失败!",
                    Type = "包装线",
                    LogMessage = "WI601K接口调用失败:" + jo["content"].ToString(),
                    Type = "四码合一",
                    ClassName = typeof(FrmLogin).ToString()
                });
                return false;
@@ -375,13 +369,14 @@
            JObject jo = MES_Interface(WI1032, encryptionKey);
            if (jo["state"].ToString() == "false")
            {
                t = false;
                System_Bll.WriteLogToDB(new Entity.Base_Log
                {
                    CreateUserID = FrmLogin.LoginUserID,
                    CreateUserName = FrmLogin.loginUserName,
                    LocalIP = FrmLogin.LocalIP,
                    LogMessage = "MES接口WI1032条码邦定失败",
                    Type = "包装线",
                    LogMessage = "WI1032接口调用失败:" + jo["content"].ToString(),
                    Type = "四码合一",
                    ClassName = typeof(FrmLogin).ToString()
                });
                return null;
@@ -389,61 +384,58 @@
            return jo;
        }
        public bool WI6041(string Line, string Station, string StationID, JObject outWI1032)
        public bool WI6041(string Line, string Station, string StationID, string PSN)
        {
            var contents = outWI1032["content"];
            string WI6041 = "{ " +
                "\n\"user\":\"sys\", " +
                "\n\"password\":\"sysuser\", " +
                "\n\"method\":\"MES_BA_WI6041\", " +
                "\n\"servername\":\"London\", " +
                "\n\"methodparameter\":{ " +
                "\n\"content\":{ " +
                "\n\"snresult\":{ " +
                "\n}, " +
                "\n\"testresult\":{ " +
                "\n\"psn\":\"" + (string)contents["PSN"] + "\", " +
                "\n\"workordernumber\":\"100000236815\", " +
                "\n\"testStationId\":\"" + StationID + "\", " +
                "\n\"testStationName\":\"" + Station + "\", " +
                "\n\"linename\":\"" + Line + "\", " +
                "\n\"hostname\":\"admin-PC-0930\", " +
                "\n\"testTime\":\"" + string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", DateTime.Now) + "\", " +
                "\n\"testModel\":\"S87567GB002_BLK1\", " +
                "\n\"testWorkID\":\"\", " +
                "\n\"Fixture\":\"\", " +
                "\n\"FixtureSN\":\"\", " +
                "\n\"Powersupply\":\"\", " +
                "\n\"PowersupplySN\":\"\", " +
                "\n\"Instrument\":\"\", " +
                "\n\"InstrumentSN\":\"\", " +
                "\n\"Operator\":\"\", " +
                "\n\"testVersion\":\"V1.0.1\" " +
                "\n}, " +
                "\n\"itemresult\":[ " +
                "\n]," +
                "\n\"specialitem\":[ " +
                "\n] " +
                "\n}, " +
                "\n\"state\":\"pass\", " +
                "\n\"errcode\":\"\", " +
                "\n\"errmessage\":\"\"} " +
                "\n}";
            string WI6041 = "{ ";
            WI6041 += "\n\"user\":\"sys\", ";
            WI6041 += "\n\"password\":\"sysuser\", ";
            WI6041 += "\n\"method\":\"MES_BA_WI6041\", ";
            WI6041 += "\n\"servername\":\"London\", ";
            WI6041 += "\n\"methodparameter\":{ ";
            WI6041 += "\n\"content\":{ ";
            WI6041 += "\n\"snresult\":{ ";
            WI6041 += "\n}, ";
            WI6041 += "\n\"testresult\":{ ";
            WI6041 += "\n\"psn\":\"" + PSN + "\", ";
            WI6041 += "\n\"workordernumber\":\"100000236815\", ";
            WI6041 += "\n\"testStationId\":\"" + StationID + "\", ";
            WI6041 += "\n\"testStationName\":\"" + Station + "\", ";
            WI6041 += "\n\"linename\":\"" + Line + "\", ";
            WI6041 += "\n\"hostname\":\"admin-PC-0930\", ";
            WI6041 += "\n\"testTime\":\"" + string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", DateTime.Now) + "\", ";
            WI6041 += "\n\"testModel\":\"S87567GB002_BLK1\", ";
            WI6041 += "\n\"testWorkID\":\"\", ";
            WI6041 += "\n\"Fixture\":\"\", ";
            WI6041 += "\n\"FixtureSN\":\"\", ";
            WI6041 += "\n\"Powersupply\":\"\", ";
            WI6041 += "\n\"PowersupplySN\":\"\", ";
            WI6041 += "\n\"Instrument\":\"\", ";
            WI6041 += "\n\"InstrumentSN\":\"\", ";
            WI6041 += "\n\"Operator\":\"\", ";
            WI6041 += "\n\"testVersion\":\"V1.0.1\" ";
            WI6041 += "\n}, ";
            WI6041 += "\n\"itemresult\":[ ";
            WI6041 += "\n],";
            WI6041 += "\n\"specialitem\":[ ";
            WI6041 += "\n] ";
            WI6041 += "\n}, ";
            WI6041 += "\n\"state\":\"pass\", ";
            WI6041 += "\n\"errcode\":\"\", ";
            WI6041 += "\n\"errmessage\":\"\"} ";
            WI6041 += "\n}";
            JObject jo = MES_Interface(WI6041, encryptionKey);
            if (jo["state"].ToString() == "false")
            {
                t = false;
                System_Bll.WriteLogToDB(new Entity.Base_Log
                {
                    CreateUserID = FrmLogin.LoginUserID,
                    CreateUserName = FrmLogin.loginUserName,
                    LocalIP = FrmLogin.LocalIP,
                    LogMessage = "MES接口WI6041数据提交失败",
                    Type = "包装线",
                    LogMessage = "WI6041接口调用失败:" + jo["content"].ToString(),
                    Type = "四码合一",
                    ClassName = typeof(FrmLogin).ToString()
                });
                string a = string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", DateTime.Now);
                return false;
            }
            return true;