public void SerialPortHandle(bool IsOpen) { try { lock (locker) { if (serialPort == null) { serialPort = serialPortUtils.OpenPort(textBoxCom.Text.Trim(), logHandleUtils); } if (serialPort != null) { if (IsOpen) { if (!serialPort.IsOpen) { serialPort.Open(); } } else if (serialPort.IsOpen) { serialPort.Close(); } } } } catch (Exception ex) { RfidInfo info = new RfidInfo() { InfoType = RFIDInfoEnum.PortError, Content = $"RFID串口操作失败{ex.Message}" }; SendRfidOperateEvent(info); } }
protected virtual void SendRfidOperateEvent(RfidInfo info) { if (this.RFIDInfoEvent != null) { this.RFIDInfoEvent(info); } }
//单次读取RFID public void button_read_filter_data_Click(object sender, RoutedEventArgs e) { IsReadSuccess = false; recieveData = ""; string showResult = ""; string result = getFilterData(); string[] data = dataUtils.getRecieveFilterData(recieveData);//data为结果.data[0]:工厂编号,data[1]:产品编号,data[2]:生产日期,data[3]:流水号 int filterCode = dataUtils.getResultCode(recieveData); if (filterCode == 84) { showResult = result + "\n" + dataUtils.showFilterData(data); } else { showResult = result;//错误在此显示,data里面全为空 } result = getFilterSN(); string[] sn = dataUtils.getRecieveSN(recieveData);//sn[0]为sn序列号,sn[1]为滤芯使用时长 int snCode = dataUtils.getResultCode(recieveData); if (snCode == 85) { showResult += "\n" + "sn:" + sn[0] + "\n" + "滤芯使用时长(秒):" + sn[1]; } else { showResult = result;//错误走此分支 } if ((filterCode == 84) && (snCode == 85)) { textBox_info_box.Background = new SolidColorBrush(Constans.successColor); soundPlay.playSuccess(); IsReadSuccess = true; RfidInfo info = new RfidInfo() { InfoType = RFIDInfoEnum.ReadSuccess, Sn = sn[0], Content = $"RFID读取到信息:{showResult}" }; SendRfidOperateEvent(info); } else { textBox_info_box.Background = new SolidColorBrush(Constans.errorColor); soundPlay.playFail(); RfidInfo info = new RfidInfo() { InfoType = RFIDInfoEnum.ReadError, Content = $"RFID读取失败" }; SendRfidOperateEvent(info); } textBox_info_box.Text = showResult; logHandleUtils.writeLog(textBox_info_box.Text); }
//连续读取的定时器 private void Timer_TimesUp(object sender, EventArgs e) { recieveData = ""; string showResult = ""; ++count; if (count >= 20)//间隔一定时间自动检测串口是否打开 { count = 10; SerialPortHandle(true); } string result = getFilterData(); string[] data = dataUtils.getRecieveFilterData(recieveData); int filterCode = dataUtils.getResultCode(recieveData); if (filterCode == 84) { showResult = result + "\n" + dataUtils.showFilterData(data); } result = getFilterSN(); string[] sn = dataUtils.getRecieveSN(recieveData); int snCode = dataUtils.getResultCode(recieveData); if (snCode == 85) { showResult += "\n" + "sn:" + sn[0] + "\n" + "滤芯使用时长(秒):" + sn[1]; } if ((filterCode == 84) && (snCode == 85)) { textBox_info_box.Background = new SolidColorBrush(Constans.successColor); textBox_info_box.Text = showResult; logHandleUtils.writeLog(textBox_info_box.Text); soundPlay.playSuccess(); try { RfidInfo info = new RfidInfo() { InfoType = RFIDInfoEnum.ReadSuccess, Sn = sn[0], Content = $"RFID读取成功,信息为:{showResult}" }; SendRfidOperateEvent(info); } catch (Exception ex) { logHandleUtils.writeLog($"连续读取错误:{ex.Message}"); RfidInfo info = new RfidInfo() { InfoType = RFIDInfoEnum.ReadError, Content = $"连续读取错误:{ex.Message}" }; SendRfidOperateEvent(info); } } }
//重新烧录 public void button_reburn_Click(object sender, RoutedEventArgs e) { IsBurnSuccess = false; recieveData = ""; textBox_info_box.Background = new SolidColorBrush(Constans.whtieColor); SerialPortHandle(true); if (serialPort != null && serialPort.IsOpen) { if (Regex.IsMatch(textBox_serial_NO.Text, @"^[+-]?\d*[.]?\d*$")) { string result = reburnFilterData(factory_num, product_num, product_date, textBox_serial_NO.Text); int code = dataUtils.getResultCode(recieveData); if (code == 80) { soundPlay.playSuccess(); serial_num_int = int.Parse(serial_num); result = getFilterSN(); string[] sn = dataUtils.getRecieveSN(recieveData); //textBox_info_box.Text = result + "\n" + "sn:" + sn[0] + "\n" + "滤芯使用时长(秒):" + sn[1]; textBox_info_box.Text = "重新烧写成功!\n" + "sn:" + sn[0] + "\n" + "滤芯使用时长(秒):" + sn[1]; textBox_info_box.Background = new SolidColorBrush(Constans.successColor); string data = factory_num + "\t" + product_num + "\t" + textBox_serial_NO.Text + "\t" + product_date + "\t" + sn[0]; if (Check_WorkCommand.IsChecked == true) { data += "\t" + work_command_num; } dataHandleUtils.writeData(data); log = "重新写入RFID:" + factory_num + "," + product_num + "," + textBox_serial_NO.Text + "," + product_date + "," + sn[0]; logHandleUtils.writeLog(log); serial_num_int++; textBox_serial_NO.Text = serial_num_int.ToString(); string config = factory_num + "-" + product_num + "-" + textBox_serial_NO.Text + "-" + Check_WorkCommand.IsChecked.ToString() + "-" + work_command_num + "-"; configHandleUtils.writeConfig(config); RfidInfo info = new RfidInfo() { InfoType = RFIDInfoEnum.WriteSuccess, Sn = sn[0], Content = $"重新写入RFID成功{factory_num},{ product_num },{textBox_serial_NO.Text},{product_date},{sn[0]}" }; SendRfidOperateEvent(info); IsBurnSuccess = true; } else { soundPlay.playFail(); textBox_info_box.Background = new SolidColorBrush(Constans.errorColor); textBox_info_box.Text = dataUtils.codeAnalyse(code); RfidInfo info = new RfidInfo() { InfoType = RFIDInfoEnum.WriteError, Content = $"重新写入RFID失败{factory_num},{ product_num },{textBox_serial_NO.Text},{product_date}" }; SendRfidOperateEvent(info); } } else { textBox_serial_NO.Background = new SolidColorBrush(Constans.successColor); textBox_info_box.Text = "流水号需为数字。"; RfidInfo info = new RfidInfo() { InfoType = RFIDInfoEnum.WriteError, Content = $"重新写入RFID失败。流水号需为数字{factory_num},{ product_num },{textBox_serial_NO.Text},{product_date}" }; SendRfidOperateEvent(info); } } else { textBox_info_box.Background = new SolidColorBrush(Constans.errorColor); soundPlay.playFail(); textBox_info_box.Text = "请选择串口。"; RfidInfo info = new RfidInfo() { InfoType = RFIDInfoEnum.WriteError, Content = $"重新写入RFID失败。串口未打开。{factory_num},{ product_num },{textBox_serial_NO.Text},{product_date}" }; SendRfidOperateEvent(info); } logHandleUtils.writeLog(textBox_info_box.Text); }
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 = "当前模式为写入"; } }