Beispiel #1
0
        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="portName"></param>
        /// <returns></returns>
        public SerialPort OpenPort(string portName, LogHandleUtils logHandleUtils)
        {
            SerialPort sp = new SerialPort();

            sp.PortName  = portName;
            sp.BaudRate  = 115200;
            sp.DataBits  = 8;
            sp.DtrEnable = true;
            try
            {
                if (!sp.IsOpen)
                {
                    sp.Open();
                }
            }
            catch (Exception ex)
            {
                logHandleUtils.writeLog("串口打开出错:" + ex.Message);
            }
            return(sp);
        }
Beispiel #2
0
        public RFIDWindow(string ComPortName, bool Readmode = true)
        {
            InitializeComponent();
            logHandleUtils.createLogPath();
            dataHandleUtils.createDataPath();
            configHandleUtils.createConfigPath();

            //Check serial ports
            string[] portList = serialPortUtils.getPortNameList();
            textBoxCom.Text = ComPortName;

            serialPort = serialPortUtils.OpenPort(ComPortName, logHandleUtils);
            if (serialPort.IsOpen)
            {
                textBox_info_box.Text = ComPortName + "已打开";
                logHandleUtils.writeLog(ComPortName + "已打开");
            }
            else
            {
                textBox_info_box.Text = ComPortName + "打开失败";
                logHandleUtils.writeLog(ComPortName + "打开失败");
                RfidInfo info = new RfidInfo()
                {
                    InfoType = RFIDInfoEnum.PortError, Content = $"RFID端口{ComPortName}打开失败"
                };
                SendRfidOperateEvent(info);
            }

            string configPath   = configHandleUtils.getConfigFilePath();
            string configString = File.ReadAllText(configPath, Encoding.UTF8);

            if (1 < configString.Length)
            {
                string[] configArray = configString.Split('-');
                textBox_factory_NO.Text     = configArray[0];
                textBox_product_NO.Text     = configArray[1];
                textBox_serial_NO.Text      = configArray[2];
                Check_WorkCommand.IsChecked = bool.Parse(configArray[3]);
                if (Check_WorkCommand.IsChecked == true)
                {
                    textBox_work_command_NO.IsEnabled = true;
                    textBox_work_command_NO.Text      = configArray[4];
                    work_command_num = textBox_work_command_NO.Text;
                }

                factory_num = textBox_factory_NO.Text;
                product_num = textBox_product_NO.Text;
                serial_num  = textBox_serial_NO.Text;
            }

            DateTime dt       = DateTime.Now;
            string   monthDay = dt.ToString("MMdd");
            string   year     = dt.ToString("yyyy").Substring(2);

            textBox_product_date.Text = year + monthDay;

            readTimer.Interval = 200;
            readTimer.Enabled  = false;
            readTimer.Tick    += new EventHandler(Timer_TimesUp);

            if (Readmode)
            {
                labelMode.Content = "当前模式为连续读取";
            }
            else
            {
                labelMode.Content = "当前模式为写入";
            }
        }