/************************************************************************************
*      All contents © copyright HighWay IOT. All rights reserved
*      File:
*				DeviceAppNewDemo.cs
*      Description:
*				测试中间件Demo
*      Author:
*				Baogq
*				Baogq@MESNAC.COM
*				HTTP://WWW.MESNAC.COM
*      Create DateTime:
*				2019.11.20
*      History:
*      
***********************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using Mesnac.DeviceAdapter;
using System.Reflection;
using Mesnac.DataChange;
using System.Threading;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Net.NetworkInformation;

namespace DeviceAppDemo
{
    public partial class DeviceAppNewDemo : Form
    {
        //private readonly string m_AdapterType = ConfigurationManager.AppSettings["AdapterType"];
        //private readonly string m_AdapterClassName = ConfigurationManager.AppSettings["AdapterClassName"];
        //private readonly string m_DeviceType = ConfigurationManager.AppSettings["DeviceType"];
        //private readonly string m_DeviceIp = ConfigurationManager.AppSettings["DeviceIp"];
        //private readonly string m_DevicePort = ConfigurationManager.AppSettings["DevicePort"];
        //private readonly string m_ReadCounts = ConfigurationManager.AppSettings["ReadCounts"];
        //private readonly string m_isReload = ConfigurationManager.AppSettings["isReload"];
        private readonly string m_AdapterType = "Mesnac.DeviceAdapter.Fuchs";
        private readonly string m_AdapterClassName = "FuchsAdapter";
        private readonly string m_DeviceType = ConfigurationManager.AppSettings["DeviceType"];
        private string m_DeviceIp;
        private string m_DevicePort;
        private readonly string m_ReadCounts = ConfigurationManager.AppSettings["ReadCounts"];
        private readonly string m_isReload = ConfigurationManager.AppSettings["isReload"];
        private IDeviceAdapter m_IDeviceAdapter = null;
        byte m_iAntenna = 0;   //天线号
        int iAnt = 0;
        public DeviceAppNewDemo()
        {
            InitializeComponent();
            ConfigurationManager.RefreshSection("appSettings"); //强制重新载入  
            string sSkinName = ConfigurationManager.AppSettings.Get("ConfigSkinName");
            this.skinEngine1.SkinFile = Application.StartupPath + "\\Skins\\" + sSkinName;
            //cbReadFilterNo.SelectedIndex = 0;
            cb_Ant.SelectedIndex = 0;
            cb_SenorType.SelectedIndex = 0;
            cbReadType.SelectedIndex = 0;
            cbAntenna.SelectedIndex = 0;
            cbTagNo.SelectedIndex = 0;
            for (int i = 2;i < 14 ;i+=2)
            {
                cb_DataLength.Items.Add(i);
            }
            cb_DataLength.Text = "12";
            //cbWriteFilterNo.SelectedIndex = 0;
            //cbWriteNo.SelectedIndex = 0;
            m_IDeviceAdapter = CreateDeviceAdapter();  //动态赋值要测试的类
        }

        public void UpdateUIText(object o, System.EventArgs e)
        {
            ShowMessage((string)o.ToString());
            //this.tbText.Items.Insert(0, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + (string)o.ToString() + "\r\n");
        }
        public void RecvIdentifyDataTrue(ushort iLen, byte[] pData, byte Antenna, UInt16 iDeviceId, object pDevice)
        {
            Console.Write(" 接收到识别数据：" + StringChange.bytesToHexStr(pData) + "\r\n");
            tbLog.BeginInvoke(new System.EventHandler(UpdateUIText), StringChange.bytesToHexStr(pData));
        }

        //动态获取类
        public IDeviceAdapter CreateDeviceAdapter()
        {
            IDeviceAdapter IAdapter = null;
            //Load参数为要被实例化的动态库名称
            Assembly assembly = Assembly.Load(m_AdapterType);
            //"AlienAdapter"为要实例化的类的名称
            string fullName = m_AdapterType + "." + m_AdapterClassName;
            IAdapter = (IDeviceAdapter)assembly.CreateInstance(fullName);
            return IAdapter;
        }

        #region log
        private void Timer_Tick(object sender, EventArgs e)
        {
            this.label_Time.Text ="系统当前时间：" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss dddd");
        }
        public void ShowMessage(string strMessage)
        {
            string tempstr = DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss.fff") + ": " + strMessage + "\r\n";
            tbLog.AppendText(tempstr);
            tbLog.ScrollToCaret();
        }
        private void btnClearLog_Click(object sender, EventArgs e)
        {
            tbLog.Text = "";
        }
        #endregion
        //检查设备连接字符串的IP的格式是否正确  baogq add 2019年11月18日
        protected bool checkDeviceIP(string message)
        {
            Regex rex = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");

            if (rex.IsMatch(message))
            {
                return true;
            }
            else
                return false;
        }
        //检查适配软件端口号的格式是否正确  baogq add 2019年11月18日
        protected bool checkDevicePort(string message)
        {
            try
            {
                if (message.IndexOf(".") > 0)
                {
                    return false;
                }
                if (Convert.ToInt32(message) >= 0 && Convert.ToInt32(message) <= 65535)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        private void btnInit_Click(object sender, EventArgs e)
        {
            if (m_IDeviceAdapter != null)
            {
                m_DeviceIp = tb_IPaddr.Text.Trim();
                m_DevicePort = tb_Port.Text.Trim();
                if (!checkDeviceIP(m_DeviceIp))     //判断IP地址格式是否正确
                {
                    ShowMessage("设备的IP地址 格式输入错误。IP地址 的正确格式是：(0-255.0-255.0-255.0-255)。");
                    return;
                }
                if (!checkDevicePort(m_DevicePort))
                {
                    ShowMessage("设备端口号 格式输入错误。端口号 的正确格式是：0-65535的整数。");
                    return;
                }
                if (m_DeviceIp.IndexOf("COM") >= 0)
                {
                    if (m_IDeviceAdapter.Device_Init(CommType.RS232, m_DeviceIp + ":" + m_DevicePort, (DeviceType)(Convert.ToInt32(m_DeviceType))))
                    {
                        m_IDeviceAdapter.RecvIdentifyDataEvent += new RecvIdentifyData(RecvIdentifyDataTrue);
                        ShowMessage("初始化成功");
                    }
                    else
                    {
                        ShowMessage("初始化失败");
                    }
                }
                else
                {
                    if (m_IDeviceAdapter.Device_Init(CommType.RJ45, m_DeviceIp + ":" + m_DevicePort, (DeviceType)(Convert.ToInt32(m_DeviceType))))
                    {
                        m_IDeviceAdapter.RecvIdentifyDataEvent += new RecvIdentifyData(RecvIdentifyDataTrue);
                        ShowMessage("初始化成功");
                    }
                    else
                    {
                        ShowMessage("初始化失败");
                    }
                }

            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (m_IDeviceAdapter != null)
            {
                m_DeviceIp = tb_IPaddr.Text.Trim();
                m_DevicePort = tb_Port.Text.Trim();
                if (!checkDeviceIP(m_DeviceIp))     //判断IP地址格式是否正确
                {
                    ShowMessage("设备的IP地址 格式输入错误。IP地址 的正确格式是：(0-255.0-255.0-255.0-255)。");
                    return;
                }
                if (!checkDevicePort(m_DevicePort))
                {
                    ShowMessage("设备端口号 格式输入错误。端口号 的正确格式是：0-65535的整数。");
                    return;
                }
                if (m_DeviceIp.IndexOf("COM") >= 0)
                {
                    if (m_IDeviceAdapter.Device_Init(CommType.RS232, m_DeviceIp + ":" + m_DevicePort, (DeviceType)(Convert.ToInt32(m_DeviceType))))
                    {
                        m_IDeviceAdapter.RecvIdentifyDataEvent += new RecvIdentifyData(RecvIdentifyDataTrue);
                        ShowMessage("初始化成功");
                    }
                    else
                    {
                        ShowMessage("初始化失败");
                    }
                }
                else
                {
                    if (m_IDeviceAdapter.Device_Init(CommType.RJ45, m_DeviceIp + ":" + m_DevicePort, (DeviceType)(Convert.ToInt32(m_DeviceType))))
                    {
                        m_IDeviceAdapter.RecvIdentifyDataEvent += new RecvIdentifyData(RecvIdentifyDataTrue);
                        //ShowMessage("初始化成功");
                    }
                    else
                    {
                        ShowMessage("初始化失败");
                    }
                }
                if (m_IDeviceAdapter.Device_Connect())
                {
                    ShowMessage("连接成功");
                }
                else
                {
                    ShowMessage("连接失败");
                }
            }
        }

        private void btnConStatus_Click(object sender, EventArgs e)
        {
            try {
                if (m_IDeviceAdapter != null)
                {
                    if (m_IDeviceAdapter.Device_GetState())
                    {
                        ShowMessage(" 当前状态：已连接");
                    }
                    else
                    {
                        ShowMessage(" 当前状态：未连接");
                    }
                }
            }
            catch (Exception ex)
            {
                ShowMessage("当前状态：未连接");
            }
        }

        private void btnDisConnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_IDeviceAdapter != null)
                {
                    m_IDeviceAdapter.Device_Destroy();
                    ShowMessage(" 断开连接成功" + "\r\n");
                }
            }
            catch (Exception ex)
            {
                ShowMessage("当前状态：未连接");
            }
        }

        private void btnOneIdentify_Click(object sender, EventArgs e)
        {
            if (m_IDeviceAdapter != null)
            {
                Byte iReadLen = 0;
                //UInt16 TimedOut = Convert.ToUInt16(tbIdentifyTimeOut.Text.Trim());
                //m_iAntenna = Convert.ToByte(tbIdentifyAnt.Text.Trim());
                Byte[] pReadData = null;
                //string iEpc = m_IDeviceAdapter.Device_GetOneIdentifyData(m_iAntenna, TimedOut);
                //ShowMessage(" 单次识别到的数据(测试返回String的函数)为: " + iEpc + "\r\n");

                //iReadLen = m_IDeviceAdapter.Device_GetOneIdentifyData(ref pReadData, m_iAntenna, TimedOut,0);  //如果读取到数据
                
                //if (iReadLen > 0)
                //{
                //    ShowMessage(" 单次识别到的数据(测试返回btye长度的函数)为: " + StringChange.bytesToHexStr(pReadData) + "\r\n");
                //}
                //else
                //{
                //    ShowMessage(" 未识别到数据" + "\r\n");
                //}
            }
        }

        private void btnLoopIdentify_Click(object sender, EventArgs e)
        {
            //if (m_IDeviceAdapter != null)
            //{
            //    int iLoop = 5;
            //    int iLoopTime = 1000;
            //    iLoop = int.Parse(tbIdentifyCount.Text.Trim());
            //    iLoopTime = int.Parse(tbIdentifyTime.Text.Trim());
            //    Byte iReadLen = 0;
            //    UInt16 TimedOut = Convert.ToUInt16(tbIdentifyTimeOut.Text.Trim());
            //    m_iAntenna = Convert.ToByte(tbIdentifyAnt.Text.Trim());
            //    Byte[] pReadData = null;
            //    ShowMessage("循环测试开始：");
            //    for (int i = 0; i < iLoop; i++)
            //    {
            //        string iEpc = m_IDeviceAdapter.Device_GetOneIdentifyData(m_iAntenna, TimedOut);
            //        ShowMessage(" 单次识别到的数据(测试返回String的函数)为: " + iEpc + "\r\n");

            //        iReadLen = m_IDeviceAdapter.Device_GetOneIdentifyData(ref pReadData, m_iAntenna, TimedOut,0);  //如果读取到数据

            //        if (iReadLen > 0)
            //        {
            //            ShowMessage(" 单次识别到的数据(测试返回btye长度的函数)为: " + StringChange.bytesToHexStr(pReadData) + "\r\n");
            //        }
            //        else
            //        {
            //            ShowMessage(" 未识别到数据" + "\r\n");
            //        }
            //        Thread.Sleep(iLoopTime);
            //    }
            //    ShowMessage("循环测试结束");
            //}
            //else
            //{
            //    ShowMessage(" m_IDeviceAdapter is null" + "\r\n");
            //}

        }

        private void btnSendHeartPack_Click(object sender, EventArgs e)
        {
            if (m_IDeviceAdapter != null)
            {
                byte iResult = 0;
                iResult = m_IDeviceAdapter.Device_SendHeartPack();

                ShowMessage(" 获得心跳包成功：" + iResult + "\r\n");
            }
        }

        private void btnReadData_Click(object sender, EventArgs e)
        {
            #region init data
            G2MemBank iG2bk = G2MemBank.EPC;
            UInt16 iReadLen = 0;
            Byte[] pReadData = null;
            Byte[] pFilterData = null;
            UInt16 filterOffset = 0;  //过滤偏移
            string strFilterData = "";
            UInt16 iFilterDataLength = 1;//过滤长度
            pFilterData = new byte[iFilterDataLength];
            pFilterData[0] = 0;
            UInt16 readOffset = 0;  //读取偏移
            UInt16 readLength = Convert.ToUInt16(cb_DataLength.Text);  //读取长度
            int iAnt = 0;
            //for (int i = 0; i < iFilterDataLength; i++)
            //{
            //    pFilterData[i] = Convert.ToByte(strFilterData.Substring(i * 2, 2), 16); //过滤数据
            //}
            if (!m_IDeviceAdapter.Device_GetState())
            {
                ShowMessage("请检查设备连接状态！");
                return;
            }
            if (cbReadType.SelectedIndex == 1)  //判断读头类型
            {
                iAnt = cbAntenna.SelectedIndex + 1 + 4;
            }
            else
            {
                iAnt = cbAntenna.SelectedIndex + 1;
            }
            m_iAntenna = Convert.ToByte(iAnt);
            #endregion
            if (cbTagNo.SelectedIndex.ToString() == "1")
            {
                iG2bk = G2MemBank.TID;
            }
            if (m_IDeviceAdapter != null)
            {
                iReadLen = m_IDeviceAdapter.Device_Read(iG2bk, filterOffset, iFilterDataLength, pFilterData, iG2bk, readOffset, readLength, ref pReadData, m_iAntenna, 1);  //如果读取到数据
                if (iReadLen > 0)
                {
                    if (iG2bk == G2MemBank.TID)
                    {
                        ShowMessage(" 读取原始数据：" + StringChange.bytesToHexStr(pReadData) + "\r\n");
                        tbShowReadData.Text = StringChange.bytesToHexStr(pReadData);
                    }
                    else
                    {
                        ShowMessage("标签数据：" + Encoding.ASCII.GetString(pReadData, 0, pReadData.Length) + " 读取原始数据：" + StringChange.bytesToHexStr(pReadData) + "\r\n");
                        tbShowReadData.Text = Encoding.ASCII.GetString(pReadData, 0, readLength);
                    }
                }
                else
                {
                    ShowMessage(" 未读取到数据" + "\r\n");
                    tbShowReadData.Text = "";
                }
            }
        }

        private void btnLoopReadData_Click(object sender, EventArgs e)
        {
            #region init data
            //int iLoop = 5;
            //iLoop = Convert.ToInt32(tbReadLoop.Text.Trim());
            //int iLoopTime = 1000;
            //iLoopTime = Convert.ToInt32(tbReadLoopTime.Text.Trim());
            //UInt16 iReadLen = 0;
            //Byte[] pReadData = null;
            //Byte[] pFilterData = null;
            //UInt16 filterOffset = Convert.ToUInt16(tbReadFilterOffset.Text.Trim());  //过滤偏移
            //string strFilterData = tbFilterData.Text.Trim();
            //UInt16 iFilterDataLength = Convert.ToUInt16(strFilterData.Length / 2);//过滤长度
            //pFilterData = new byte[iFilterDataLength];
            //for (int i = 0; i < iFilterDataLength; i++)
            //{
            //    pFilterData[i] = Convert.ToByte(strFilterData.Substring(i * 2, 2), 16); //过滤数据
            //}
            //UInt16 readOffset = Convert.ToUInt16(tbReadOffset.Text.Trim());  //读取偏移
            //UInt16 readLength = Convert.ToUInt16(tbReadLength.Text.Trim());  //读取长度
            //m_iAntenna = Convert.ToByte(tbReadAnt.Text.Trim()); //天线号
            #endregion
            //if (m_IDeviceAdapter != null)
            //{
            //    ShowMessage("循环读取数据开始：");
            //    for (int i = 0; i < iLoop; i++)
            //    {
            //        iReadLen = m_IDeviceAdapter.Device_Read((G2MemBank)(int.Parse(cbReadFilterNo.SelectedIndex.ToString()) + 1), filterOffset, iFilterDataLength, pFilterData, (G2MemBank)(int.Parse(cbReadNo.SelectedIndex.ToString()) + 1), readOffset, readLength, ref pReadData, m_iAntenna, int.Parse(m_ReadCounts));  //如果读取到数据
            //        if (iReadLen > 0)
            //        {
            //            ShowMessage(" 读取数据：" + StringChange.bytesToHexStr(pReadData) + "\r\n");
            //            tbShowReadData.Text = StringChange.bytesToHexStr(pReadData);
            //        }
            //        else
            //        {
            //            ShowMessage(" 未读取到数据");
            //        }

            //        Thread.Sleep(iLoopTime);
            //    }
            //}
            //else
            //{
            //    ShowMessage(" m_IDeviceAdapter 为空");
            //}

            //ShowMessage("循环读取数据结束。");
        }

        private void btnWriteData_Click(object sender, EventArgs e)
        {
            if (m_IDeviceAdapter != null)
            {
                UInt16 iReadLen = 0;
                Byte[] pWriteData = null;
                Byte[] pFilterData = null;
                //UInt16 filterOffset = Convert.ToUInt16(tbWriteFilterOffsetAdd.Text.Trim());  //过滤偏移
                //string strFilterData = tbWriteFilterData.Text.Trim();
                //UInt16 iFilterDataLength = Convert.ToUInt16(strFilterData.Length / 2);//过滤长度
                //pFilterData = new byte[iFilterDataLength];
                //for (int i = 0; i < iFilterDataLength; i++)
                //{
                //    pFilterData[i] = Convert.ToByte(strFilterData.Substring(i * 2, 2), 16); //过滤数据
                //}
                UInt16 writeOffset = 0;
                string strWriteData = tbWriteData.Text.Trim();//写入的数据
                byte iWriteDataLength = Convert.ToByte(strWriteData.Length);//写入长度
                pWriteData = new byte[iWriteDataLength];
                //判断输入数据是否为9位
                if (iWriteDataLength < 12)
                {
                    strWriteData = strWriteData.PadRight((12), '0');
                    iWriteDataLength = Convert.ToByte(strWriteData.Length);
                    pWriteData = new byte[strWriteData.Length];
                    pWriteData = Encoding.Default.GetBytes(strWriteData);
                }
                else if (iWriteDataLength > 12)
                {
                    ShowMessage("请确认标签数据位数为12位！");
                    return;
                }
                else
                {
                    pWriteData = Encoding.Default.GetBytes(strWriteData);
                }
                if (cbReadType.SelectedIndex == 1)  //判断读头类型
                {
                    iAnt = cbAntenna.SelectedIndex + 1 + 4;
                }
                else
                {
                    iAnt = cbAntenna.SelectedIndex + 1;
                }
                m_iAntenna = Convert.ToByte(iAnt);//天线号
                #region 判断天线号是否符合范围
                //if (Convert.ToInt32(m_iAntenna) > 16 || Convert.ToInt32(m_iAntenna) <= 0)
                //{
                //    ShowMessage("天线号必须是1-4的整数！ " + "\r\n");
                //    MessageBox.Show("天线号必须是1-4的整数！", "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    return;
                //}
                #endregion
                iReadLen = m_IDeviceAdapter.Device_Write((G2MemBank)(int.Parse(cb_SenorType.SelectedIndex.ToString()) + 1), 0, 0, pFilterData,
                    (G2MemBank)(int.Parse(cb_SenorType.SelectedIndex.ToString()) + 1), writeOffset, iWriteDataLength, pWriteData, m_iAntenna);
                if (iReadLen == 1)
                {
                    ShowMessage(" 写入成功");
                    tbWriteResult.Text = " 写入成功";
                }
                else if (iReadLen == 0)
                {
                    ShowMessage(" 写入失败");
                    tbWriteResult.Text = " 写入失败";
                }
                else if (iReadLen == 2)
                {
                    ShowMessage(" 写入跟读取不一致，写入失败，读取成功");
                    tbWriteResult.Text = " 写入跟读取，写入失败，读取成功";
                }
            }
        }
        public static void Restart()
        {
            //开启新的实例
            Process.Start(Application.ExecutablePath);
        }

        private void btnbeginIdentify_Click(object sender, EventArgs e)
        {
            if (m_IDeviceAdapter != null)
            {
                try {
                    //m_iAntenna = Convert.ToByte(tbIdentifyAnt.Text.Trim());
                    bool bResult = false;
                    bResult = m_IDeviceAdapter.Device_BeginIdentify(1, 10000,true);
                    if (bResult)
                    {
                        ShowMessage(" 发送连续识别指令成功");
                    }
                    else
                    {
                        //ShowMessage(" 发送连续识别指令失败" + "\r\n");
                    }

                }
                catch (Exception ex)
                {
                    ShowMessage(" 发送连续识别指令失败：" + "\r\n");
                }
            }
        }

        private void btnstopIdentify_Click(object sender, EventArgs e)
        {
            if (m_IDeviceAdapter != null)
            {
                try
                {
                    bool bResult = false;
                    //m_iAntenna = Convert.ToByte(tbIdentifyAnt.Text.Trim());
                    bResult = m_IDeviceAdapter.Device_StopIdentify(1);


                    ShowMessage(" 发送停止连续识别指令成功" + "\r\n");
                }
                catch (Exception ex)
                {
                    ShowMessage(" 发送停止连续识别指令失败" + "\r\n");
                }
            }
        }

        private void btnSetAntCT_Click(object sender, EventArgs e)
        {
            try {
                if (m_IDeviceAdapter != null)
                {
                    if (!m_IDeviceAdapter.Device_GetState())
                    {
                        ShowMessage("读头工作模式设置失败，请检查设备连接状态！");
                        return;
                    }
                    int iDbi = 0;
                    bool bResult = false;
                    int iAnt = 0;
                    if (cb_SenorType.SelectedIndex == 1)  //判断读头类型
                    {
                        iAnt = cb_Ant.SelectedIndex + 1 + 4;
                    }
                    else
                    {
                        iAnt = cb_Ant.SelectedIndex + 1;
                    }
                    m_iAntenna = Convert.ToByte(iAnt);
                    bResult = m_IDeviceAdapter.Device_SetRf(iDbi, m_iAntenna, WriteOrRead.Read);
                    if (bResult)
                    {
                        ShowMessage("读头工作模式设置成功");
                    }
                    else
                    {
                        ShowMessage("读头工作模式设置失败");
                    }

                }
            }
            catch (Exception ex)
            {
                ShowMessage("请连接读写器！再进行其他操作");
            }
        }

        private void btSetSignleRead_Click(object sender, EventArgs e)
        {
            try {
                if (!m_IDeviceAdapter.Device_GetState())
                {
                    ShowMessage("读头工作模式设置失败，请检查设备连接状态！");
                    return;
                }
                if (m_IDeviceAdapter != null)
                {
                    string strResult = "";
                    int iAnt = 0;
                    if (cb_SenorType.SelectedIndex != 1)
                    {
                        ShowMessage("请更改读头类型为超高频！");
                        return;
                    }
                    iAnt = cb_Ant.SelectedIndex + 1;
                    m_iAntenna = Convert.ToByte(iAnt);
                    strResult = m_IDeviceAdapter.Device_GetOneIdentifyData(m_iAntenna, 2000);
                    if (strResult == "成功")
                    {
                        ShowMessage("单标签模式设置成功");
                    }
                    else if (strResult == "超时") 
                    {
                        ShowMessage("单标签模式设置超时，请重新设置！");
                    }
                    else
                    {
                        ShowMessage("单标签模式设置失败，请重新设置！");
                    }

                }
            }
            catch (Exception ex)
            {
                ShowMessage("请连接读写器！再进行其他操作");
            }
        }

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutSystem aboutForm = new AboutSystem();
            aboutForm.ShowDialog();
        }

        private void 更换系统皮肤ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangeSkinForm csForm = new ChangeSkinForm();
            csForm.ShowDialog();
        }

        private void 系统重启ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要重启该系统吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                //系统重启
                Application.Exit();
                //开启新的实例
                Application.Restart();
            }
            else
            {
                return;
            }
        }

        private void DeviceAppNewDemo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (m_isReload == "1")  //是否自动重启；1为重启
            {
                Restart();
            }
            else
            {
                if (e.CloseReason == CloseReason.UserClosing)
                {
                    if (MessageBox.Show("您确定要关闭该系统吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        //关闭当前实例
                        Process.GetCurrentProcess().Kill();
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
            }
        }

        private void BtnPing_Click(object sender, EventArgs e)
        {
            if (BtnPing.Text == "取消")
            {
                BtnPing.Text = "Ping";
                TimerPing.Enabled = false;
                TimerPing.Stop();
            }
            else if (BtnPing.Text == "Ping")
            {
                TimerPing.Enabled = true;
                TimerPing.Interval = 1000;
                TimerPing.Start();
                BtnPing.Text = "取消";
            }
        }

        private void TimerPing_Tick(object sender, EventArgs e)
        {
            string ip = tb_IPaddr.Text.Trim();
            Thread M_Deal = new Thread(() => PingTF(ip, new Ping()));
            M_Deal.IsBackground = true;
            M_Deal.Start();
            tbLog.Text += log;
            tbLog.Select(tbLog.TextLength, 0);
            tbLog.ScrollToCaret();
        }
        string log = null;
        private void PingTF(string ip, Ping ping)
        {
            if (!string.IsNullOrEmpty(ip))
            {
                PingReply reply = ping.Send(ip);

                if (reply.Status == IPStatus.Success)
                {
                    string time = DateTime.Now.ToString();
                    string hour = DateTime.Now.Hour.ToString();
                    string patime = time.Replace(":", "").Replace("/", "");
                    log = time + ":来自" + reply.Address.ToString() + "的回复：字节=" + reply.Buffer.Length + "  回复时间=" + reply.RoundtripTime.ToString() + "   TTL=" + reply.Options.Ttl + "\r\n";
                    //if (hour.Equals("00"))
                    //{

                    //}
                }
                else
                {
                    log = DateTime.Now.ToString() + ":Request timed out.\r\n";
                    //  Console.WriteLine("继续ping");
                    //PingTF(ip, ping);
                }
                Thread.Sleep(1000);

            }
        }

        private void cbTagNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTagNo.SelectedIndex.ToString() == "1")
            {
                this.cb_DataLength.Enabled = false;
                this.btnWriteData.Enabled = false;
                this.tbWriteData.Enabled = false;
            }
            else
            {
                this.cb_DataLength.Enabled = true;
                this.btnWriteData.Enabled = true;
                this.tbWriteData.Enabled = true;
            }
        }
    }
}
