C# 查询科星继电器的输入端状态以及控制输出

C# 查询科星继电器的输入端状态以及控制输出_第1张图片点击查询,就能通过TCP的模式,查询到继电器的输入状态

代码如下:代码中包含对继电器的操作逻辑,其中包含一些业务逻辑的代码,不需要的话注释掉就行了

public partial class FrmJdq : Form
    {
        public FrmJdq()
        {
            InitializeComponent();

            Init();

            //txtLocalPort.Text = ConfigurationManager.AppSettings["LocalPort"].ToString();
            txtZkIp.Text = ConfigurationManager.AppSettings["ZkIp"].ToString();
            txtZkPort.Text = ConfigurationManager.AppSettings["ZkPort"].ToString();
            txtZkMsg.Text = ConfigurationManager.AppSettings["ZkMsg"].ToString();
            txtRelayIp.Text = ConfigurationManager.AppSettings["RelayIp"].ToString();
            txtRelayPort.Text = ConfigurationManager.AppSettings["RelayPort"].ToString();
            txtCloseRelayTime.Text = ConfigurationManager.AppSettings["CloseRelayTime"].ToString();

            btnDevice1Conn_Click(null, null);

            for (int i = 0; i < cmbRelay.Items.Count; i++)
            {
                if (cmbRelay.Items[i].ToString() == ConfigurationManager.AppSettings["RelayNum"].ToString())
                {
                    cmbRelay.SelectedIndex = i;
                    break;
                }
            }
        }

        class StateObject
        {
            public byte[] Buffer = new byte[1024];
            public Socket mSocket;
            public Button JoinBtn;
            public int No;
        }

        private void Init()
        {
            StateObject state = new StateObject();
            state.No = 1;
            state.JoinBtn = btnDevice1Conn;
            btnDevice1Conn.Tag = state;
        }

        /// 
        /// 发送命令到设备
        /// 
        /// 
        /// 
        private bool SendCommand(StateObject state, byte[] sBytes, bool Crc16 = false)
        {
            bool success = false;
            if (sBytes.Length == 0)
                return false;
            lock (state)
            {
                try
                {
                    byte[] m = new byte[0];
                    if (Crc16)
                    {
                        m = new byte[sBytes.Length + 2];
                        byte[] crc = Helper.CalculateCrc16(sBytes, 0, sBytes.Length);

                        sBytes.CopyTo(m, 0);
                        crc.CopyTo(m, sBytes.Length);
                        Console.WriteLine();
                    }
                    else
                    {
                        m = sBytes;
                    }
                    state.mSocket.Send(m);
                    success = true;

                    string finalStr = "";
                    foreach (var item in m)
                    {
                        finalStr += item.ToString("X2") + " ";
                    }
                    ShowInfo("发送消息:" + finalStr);
                }
                catch (Exception)
                {
                    if (state.JoinBtn.Text == "断开")
                    {
                        SetControlText(state.JoinBtn, "连接");
                        SetControlEnabled(state.JoinBtn, true);
                    }

                }
            }
            return success;
        }

        /// 
        /// 设置控件的文本.
        /// 
        /// 
        /// 
        private void SetControlText(Control ctl, string text)
        {
            if (ctl.InvokeRequired)
            {
                Invoke((EventHandler)delegate
                {
                    ctl.Text = text;
                });
            }
            else
            {
                ctl.Text = text;
            }
        }

        /// 
        /// 设置控件是否启用
        /// 
        /// 
        /// 
        private void SetControlEnabled(Control ctl, bool enabled)
        {
            if (ctl.InvokeRequired)
            {
                Invoke((EventHandler)delegate
                {
                    ctl.Enabled = Enabled;
                });
            }
            else
            {
                ctl.Enabled = Enabled;
            }
        }

        public static void SaveAppConfig(string Key, string Value)
        {
            string strFilePath = System.Windows.Forms.Application.ExecutablePath;
            Configuration objConfig = ConfigurationManager.OpenExeConfiguration(strFilePath);
            bool bolExist = false;
            foreach (string Item in objConfig.AppSettings.Settings.AllKeys)
            {
                if (Item == Key)
                {
                    bolExist = true;
                    break;
                }
            }
            if (bolExist)
            {
                objConfig.AppSettings.Settings.Remove(Key);
            }
            objConfig.AppSettings.Settings.Add(Key, Value);
            objConfig.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// 
        /// 将消息显示在界面上
        /// 
        /// 
        private void ShowInfo(string msg)
        {
            if (txtInfo.InvokeRequired)
            {
                Invoke((EventHandler)delegate
                {
                    txtInfo.Text += DateTime.Now.ToString() + " " + msg + "\r\n";
                });
            }
            else
            {
                txtInfo.Text += DateTime.Now.ToString() + " " + msg + "\r\n";
            }
        }

        private void btnDevice1Conn_Click(object sender, EventArgs e)
        {
            StateObject state = btnDevice1Conn.Tag as StateObject;
            if (btnDevice1Conn.Text.Trim() == "连接")
            {
                if (state.mSocket != null)
                {
                    state.mSocket.Close();
                }
                SaveAppConfig("Device1IP", txtRelayIp.Text.Trim());
                SaveAppConfig("Device1Port", txtRelayPort.Text.Trim());

                btnDevice1Conn.Enabled = false;
                btnDevice1Conn.Text = "正在连接";
                Socket mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                state.mSocket = mSocket;

                mSocket.BeginConnect(new IPEndPoint(IPAddress.Parse(txtRelayIp.Text.Trim()), int.Parse(txtRelayPort.Text.Trim())), new AsyncCallback(ConnectCallBack), state);
            }
            else if (btnDevice1Conn.Text.Trim() == "断开")
            {
                if (state.mSocket != null)
                {
                    state.mSocket.Close();
                }
                btnDevice1Conn.Text = "连接";
            }
        }

        /// 
        /// 连接到服务端
        /// 
        /// 
        private void ConnectCallBack(IAsyncResult ar)
        {
            StateObject state = ar.AsyncState as StateObject;
            try
            {
                state.mSocket.EndConnect(ar);
                SetControlText(state.JoinBtn, "断开");
                SetControlEnabled(state.JoinBtn, true);
                state.mSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, new AsyncCallback(Recieve), state);
                ShowInfo("TCP连接成功");
            }
            catch (Exception ex)
            {
                Helper.ErrorLog(ex.ToString());
                state.mSocket.Close();
                SetControlText(state.JoinBtn, "连接");
                SetControlEnabled(state.JoinBtn, true);
                ShowInfo("TCP连接失败");
            }
        }

        private void Recieve(IAsyncResult ar)
        {
            EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
            StateObject state = (StateObject)(ar.AsyncState);
            try
            {

                int len = state.mSocket.EndReceive(ar);
                if (len > 0)
                {
                    byte[] rcvBytes = state.Buffer.Take(len).ToArray();
                    string finalStr = "";
                    foreach (var item in rcvBytes)
                    {
                        finalStr += item.ToString("X2") + " ";
                    }
                    ShowInfo("收到消息:" + finalStr);

                    string[] info = finalStr.Split(' ');

                    //判断是不是返回开关量的状态
                    if (info[0] == "EE" && info[1] == "FF" && info[2] == "C0" && info[3] == "01")
                    {
                        string str = HexString2BinString(info[4] + info[5]);
                        str = Reversal(str).Replace(" ", "");
                        //接下来判断第几个开关是打开的
                        ShowInfo("" + str);
                        DealWithMsg();
                    }
                }
                state.mSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, new AsyncCallback(Recieve), state);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        /// 
        /// //16转2方法
        /// 
        /// 
        /// 
        static string HexString2BinString(string hexString)
        {
            try
            {
                string result = string.Empty;
                foreach (char c in hexString)
                {
                    int v = Convert.ToInt32(c.ToString(), 16);
                    int v2 = int.Parse(Convert.ToString(v, 2));
                    // 去掉格式串中的空格,即可去掉每个4位二进制数之间的空格,
                    result += string.Format("{0:d4} ", v2);
                }
                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }

        /// 
        /// 反转字符串
        /// 
        /// 
        public string Reversal(string input)
        {
            string result = "";
            for (int i = input.Length - 1; i >= 0; i--)
            {
                result += input[i];
            }
            return result;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            byte[] comm = new byte[9];
            comm[0] = 0xCC;     //帧头
            comm[1] = 0xDD;     //帧头
            comm[2] = 0xC0;     //功能码
            comm[3] = 0x01;     //地址
            comm[4] = 0x00;
            comm[5] = 0x00;
            comm[6] = 0x0D;
            //comm[7] = 0xCE;
            //comm[8] = 0x9C;

            int num = (comm[2] + comm[3] + comm[4] + comm[5] + comm[6]);
            string ch = (num & (0XFF)).ToString("X2");
            comm[7] = (byte)(num & (0XFF));

            num = (comm[2] + comm[3] + comm[4] + comm[5] + comm[6] + comm[7]);
            string cl = (num & (0XFF)).ToString("X2");
            comm[8] = (byte)(num & (0XFF));

            SendCommand(btnDevice1Conn.Tag as StateObject, comm, false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtInfo.Clear();
        }

        /// 
        /// 操作继电器
        /// 
        /// 表示打开还是关闭
        /// 操作第几路继电器(从1到16)
        private byte[] OperationRelay(bool isOpenOrClose, int Ch)
        {
            byte[] sBytes = new byte[] { 0xCC, 0xDD, 0xA1, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x9E, 0x3C };
            if (Ch <= 8)
            {
                sBytes[4] = 0;
                sBytes[6] = 0;
                sBytes[5] = (byte)(0x01 << (Ch - 1));
                sBytes[7] = (byte)(0x01 << (Ch - 1));
            }
            else
            {
                sBytes[4] = (byte)(0x01 << (Ch - 9));
                sBytes[6] = (byte)(0x01 << (Ch - 9));
                sBytes[5] = 0;
                sBytes[7] = 0;
            }
            if (isOpenOrClose == false)
            {
                sBytes[4] = 0;
                sBytes[5] = 0;
            }
            sBytes[8] = SumCheck(sBytes, 2, 6);
            sBytes[9] = SumCheck(sBytes, 2, 7);
            string str = BitConverter.ToString(sBytes).Replace("-", " ");

            return sBytes;
        }

        /// 
        /// 和校验
        /// 
        /// 
        /// 
        /// 
        /// 
        private byte SumCheck(byte[] cBytes, int startIndex, int cLenth)
        {
            byte check = 0;
            if (cBytes.Length > 1)
            {
                for (int i = startIndex; i < startIndex + cLenth; i++)
                {
                    check = (byte)((cBytes[i] + check) & 0xFF);
                }
            }
            return check;
        }

        /// 
        /// 表示按下按钮以后,是否执行逻辑
        /// 
        public bool IsCanDo { get; set; } = true;

        public int RelayCloseNum { get; set; }

        private delegate void DoSomethingDelegate();

        private void DealWithMsg()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new DoSomethingDelegate(DealWithMsg));
            }
            else
            {
                if (IsCanDo == false)
                {
                    return;
                }
                IsCanDo = false;
                //给中控发指令,让中控播片
                string zkIp = ConfigurationManager.AppSettings["ZkIp"].ToString();
                int zkPort = Convert.ToInt32(ConfigurationManager.AppSettings["ZkPort"]);
                string zkMsg = ConfigurationManager.AppSettings["ZkMsg"].ToString();
                SendUdpStrMsg(zkIp, zkPort, zkMsg);

                //给继电器发送指令
                string relayIp = ConfigurationManager.AppSettings["RelayIp"].ToString();
                int relayPort = Convert.ToInt32(ConfigurationManager.AppSettings["RelayPort"]);
                int relayNum = Convert.ToInt32(ConfigurationManager.AppSettings["RelayNum"]);
                byte[] relayByte = OperationRelay(true, relayNum);
                //SendUdpByteMsg(relayIp, relayPort, relayByte);
                SendCommand(btnDevice1Conn.Tag as StateObject, relayByte, false);
                Thread.Sleep(300);
                //打开继电器后,马上关掉
                relayByte = OperationRelay(false, relayNum);
                //SendUdpByteMsg(relayIp, relayPort, relayByte);
                SendCommand(btnDevice1Conn.Tag as StateObject, relayByte, false);

                //打开定时器
                RelayCloseNum = Convert.ToInt32(ConfigurationManager.AppSettings["CloseRelayTime"]);
                timer1.Enabled = true;
                timer1.Start();
            }
        }

        private void SendUdpStrMsg(string ip, int port, string info)
        {
            UdpClient udpClient = new UdpClient();
            try
            {
                udpClient.Connect(ip, port);
                Byte[] sendBytes = Encoding.Default.GetBytes(info);
                udpClient.Send(sendBytes, sendBytes.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            RelayCloseNum--;
            lblInfo.Text = $"{RelayCloseNum}:秒后才可以重新按下按钮";
            if (RelayCloseNum <= 0)
            {
                timer1.Stop();
                timer1.Enabled = false;
                
                IsCanDo = true;

                lblInfo.Text = "";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //SaveAppConfig("LocalPort", txtLocalPort.Text);
            SaveAppConfig("ZkIp", txtZkIp.Text);
            SaveAppConfig("ZkPort", txtZkPort.Text);
            SaveAppConfig("ZkMsg", txtZkMsg.Text);
            SaveAppConfig("RelayIp", txtRelayIp.Text);
            SaveAppConfig("RelayPort", txtRelayPort.Text);
            SaveAppConfig("RelayNum", cmbRelay.SelectedItem.ToString());
            SaveAppConfig("CloseRelayTime", txtCloseRelayTime.Text);

            MessageBox.Show("设置完成");
        }

        private void timAutoCheck_Tick(object sender, EventArgs e)
        {
            if (IsCanDo)
            {
                btnSelect_Click(null, null);
            }
        }
    }

 设计代码:

partial class FrmJdq
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label5 = new System.Windows.Forms.Label();
            this.txtRelayIp = new System.Windows.Forms.TextBox();
            this.txtRelayPort = new System.Windows.Forms.TextBox();
            this.btnDevice1Conn = new System.Windows.Forms.Button();
            this.label8 = new System.Windows.Forms.Label();
            this.txtInfo = new System.Windows.Forms.TextBox();
            this.btnSelect = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.txtZkPort = new System.Windows.Forms.TextBox();
            this.txtZkIp = new System.Windows.Forms.TextBox();
            this.txtZkMsg = new System.Windows.Forms.TextBox();
            this.lblInfo = new System.Windows.Forms.Label();
            this.btnSave = new System.Windows.Forms.Button();
            this.cmbRelay = new System.Windows.Forms.ComboBox();
            this.label7 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.txtCloseRelayTime = new System.Windows.Forms.TextBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.timAutoCheck = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(41, 22);
            this.label5.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(59, 12);
            this.label5.TabIndex = 197;
            this.label5.Text = "继电器IP:";
            // 
            // txtRelayIp
            // 
            this.txtRelayIp.Location = new System.Drawing.Point(108, 19);
            this.txtRelayIp.Margin = new System.Windows.Forms.Padding(4);
            this.txtRelayIp.Name = "txtRelayIp";
            this.txtRelayIp.Size = new System.Drawing.Size(148, 21);
            this.txtRelayIp.TabIndex = 194;
            this.txtRelayIp.Text = "192.168.1.149";
            // 
            // txtRelayPort
            // 
            this.txtRelayPort.Location = new System.Drawing.Point(400, 19);
            this.txtRelayPort.Margin = new System.Windows.Forms.Padding(4);
            this.txtRelayPort.Name = "txtRelayPort";
            this.txtRelayPort.Size = new System.Drawing.Size(76, 21);
            this.txtRelayPort.TabIndex = 195;
            this.txtRelayPort.Text = "50000";
            // 
            // btnDevice1Conn
            // 
            this.btnDevice1Conn.Location = new System.Drawing.Point(528, 13);
            this.btnDevice1Conn.Margin = new System.Windows.Forms.Padding(4);
            this.btnDevice1Conn.Name = "btnDevice1Conn";
            this.btnDevice1Conn.Size = new System.Drawing.Size(110, 34);
            this.btnDevice1Conn.TabIndex = 193;
            this.btnDevice1Conn.Text = "连接";
            this.btnDevice1Conn.UseVisualStyleBackColor = true;
            this.btnDevice1Conn.Click += new System.EventHandler(this.btnDevice1Conn_Click);
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(309, 22);
            this.label8.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(83, 12);
            this.label8.TabIndex = 196;
            this.label8.Text = "继电器端口号:";
            // 
            // txtInfo
            // 
            this.txtInfo.Location = new System.Drawing.Point(38, 294);
            this.txtInfo.Multiline = true;
            this.txtInfo.Name = "txtInfo";
            this.txtInfo.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtInfo.Size = new System.Drawing.Size(705, 337);
            this.txtInfo.TabIndex = 198;
            // 
            // btnSelect
            // 
            this.btnSelect.Location = new System.Drawing.Point(669, 13);
            this.btnSelect.Margin = new System.Windows.Forms.Padding(4);
            this.btnSelect.Name = "btnSelect";
            this.btnSelect.Size = new System.Drawing.Size(110, 34);
            this.btnSelect.TabIndex = 193;
            this.btnSelect.Text = "查询";
            this.btnSelect.UseVisualStyleBackColor = true;
            this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(774, 597);
            this.button1.Margin = new System.Windows.Forms.Padding(4);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(110, 34);
            this.button1.TabIndex = 193;
            this.button1.Text = "清除";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(270, 66);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(65, 12);
            this.label3.TabIndex = 202;
            this.label3.Text = "中控的端口";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(44, 66);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 12);
            this.label2.TabIndex = 203;
            this.label2.Text = "中控的IP";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(36, 108);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(101, 12);
            this.label1.TabIndex = 204;
            this.label1.Text = "给中控发送的指令";
            // 
            // txtZkPort
            // 
            this.txtZkPort.Location = new System.Drawing.Point(338, 63);
            this.txtZkPort.Name = "txtZkPort";
            this.txtZkPort.Size = new System.Drawing.Size(112, 21);
            this.txtZkPort.TabIndex = 199;
            // 
            // txtZkIp
            // 
            this.txtZkIp.Location = new System.Drawing.Point(104, 63);
            this.txtZkIp.Name = "txtZkIp";
            this.txtZkIp.Size = new System.Drawing.Size(138, 21);
            this.txtZkIp.TabIndex = 200;
            // 
            // txtZkMsg
            // 
            this.txtZkMsg.Location = new System.Drawing.Point(143, 105);
            this.txtZkMsg.Name = "txtZkMsg";
            this.txtZkMsg.Size = new System.Drawing.Size(307, 21);
            this.txtZkMsg.TabIndex = 201;
            // 
            // lblInfo
            // 
            this.lblInfo.AutoSize = true;
            this.lblInfo.Location = new System.Drawing.Point(110, 257);
            this.lblInfo.Name = "lblInfo";
            this.lblInfo.Size = new System.Drawing.Size(53, 12);
            this.lblInfo.TabIndex = 210;
            this.lblInfo.Text = "提示信息";
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(276, 246);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(75, 23);
            this.btnSave.TabIndex = 209;
            this.btnSave.Text = "保存配置";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // cmbRelay
            // 
            this.cmbRelay.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbRelay.FormattingEnabled = true;
            this.cmbRelay.Items.AddRange(new object[] {
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10",
            "11",
            "12",
            "13",
            "14",
            "15",
            "16"});
            this.cmbRelay.Location = new System.Drawing.Point(143, 150);
            this.cmbRelay.Name = "cmbRelay";
            this.cmbRelay.Size = new System.Drawing.Size(138, 20);
            this.cmbRelay.TabIndex = 208;
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(297, 205);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(77, 12);
            this.label7.TabIndex = 206;
            this.label7.Text = "秒内不可操作";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(59, 153);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(77, 12);
            this.label4.TabIndex = 207;
            this.label4.Text = "继电器的路数";
            // 
            // txtCloseRelayTime
            // 
            this.txtCloseRelayTime.Location = new System.Drawing.Point(143, 202);
            this.txtCloseRelayTime.Name = "txtCloseRelayTime";
            this.txtCloseRelayTime.Size = new System.Drawing.Size(138, 21);
            this.txtCloseRelayTime.TabIndex = 205;
            // 
            // timer1
            // 
            this.timer1.Interval = 1000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // timAutoCheck
            // 
            this.timAutoCheck.Enabled = true;
            this.timAutoCheck.Interval = 1000;
            this.timAutoCheck.Tick += new System.EventHandler(this.timAutoCheck_Tick);
            // 
            // FrmJdq
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(897, 643);
            this.Controls.Add(this.lblInfo);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.cmbRelay);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.txtCloseRelayTime);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtZkPort);
            this.Controls.Add(this.txtZkIp);
            this.Controls.Add(this.txtZkMsg);
            this.Controls.Add(this.txtInfo);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.txtRelayIp);
            this.Controls.Add(this.txtRelayPort);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.btnSelect);
            this.Controls.Add(this.btnDevice1Conn);
            this.Controls.Add(this.label8);
            this.Name = "FrmJdq";
            this.Text = "FrmJdq";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox txtRelayIp;
        private System.Windows.Forms.TextBox txtRelayPort;
        private System.Windows.Forms.Button btnDevice1Conn;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.TextBox txtInfo;
        private System.Windows.Forms.Button btnSelect;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txtZkPort;
        private System.Windows.Forms.TextBox txtZkIp;
        private System.Windows.Forms.TextBox txtZkMsg;
        private System.Windows.Forms.Label lblInfo;
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.ComboBox cmbRelay;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox txtCloseRelayTime;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.Timer timAutoCheck;
    }

Helper 类:

 public class Helper
    {
        #region CRH
        public static byte[] _auchCRCHi = new byte[]
        {
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
            0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
            0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
            0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
            0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
            0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
            0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
            0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
            0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
            0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
            0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
            0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
            0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
            0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
            0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
            0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
            0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
        };
        public static byte[] _auchCRCLo = new byte[]
        {
            0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
            0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
            0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
            0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
            0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
            0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
            0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
            0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
            0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
            0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
            0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
            0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
            0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
            0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
            0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
            0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
            0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
            0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
            0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
            0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
            0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
            0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
            0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
            0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
            0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
            0x43, 0x83, 0x41, 0x81, 0x80, 0x40
        };
        public static byte[] CalculateCrc16(byte[] buffer, int startIndex, int length)
        {
            byte crcHi = 0xff;  // high crc byte initialized
            byte crcLo = 0xff;  // low crc byte initialized 

            for (int i = startIndex; i < length; i++)
            {
                int crcIndex = crcHi ^ buffer[i]; // calculate the crc lookup index

                crcHi = (byte)(crcLo ^ _auchCRCHi[crcIndex]);
                crcLo = _auchCRCLo[crcIndex];
            }
            return new byte[] { crcHi, crcLo };
        }
        #endregion

        /// 
        /// 用来做软件的日常信息保存
        /// 
        /// 
        public static void InfoLog(string strLog)
        {
            try
            {
                string basepath = AppDomain.CurrentDomain.BaseDirectory + "Log//Info//";
                if (!Directory.Exists(basepath))
                {
                    Directory.CreateDirectory(basepath);
                }

                string path = basepath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                StreamWriter sw = File.AppendText(path);
                sw.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + strLog);
                sw.Close();
            }
            catch
            {
            }
        }

        /// 
        /// 用来做软件的调试信息保存
        /// 
        /// 
        public static void DebugLog(string strLog)
        {
            try
            {
                string basepath = AppDomain.CurrentDomain.BaseDirectory + "Log//Debug//";
                if (!Directory.Exists(basepath))
                {
                    Directory.CreateDirectory(basepath);
                }

                string path = basepath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                StreamWriter sw = File.AppendText(path);
                sw.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + strLog);
                sw.Close();
            }
            catch
            {
            }
        }

        /// 
        /// 用来做软件的调试信息保存
        /// 
        /// 
        public static void ErrorLog(string strLog)
        {
            try
            {
                string basepath = AppDomain.CurrentDomain.BaseDirectory + "Log//Errow//";
                if (!Directory.Exists(basepath))
                {
                    Directory.CreateDirectory(basepath);
                }

                string path = basepath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                StreamWriter sw = File.AppendText(path);
                sw.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + strLog);
                sw.Close();
            }
            catch
            {
            }
        }
    }

config 文件



     
        
    
  
    
    
    
    
    
    
    
    
  

你可能感兴趣的:(C#大全,c#)