public void LoggingOnUiForIncoming(ComPortProcessor p, MessageTemplateBase incomingMsg) { this.Invoke(new Action(() => { if (incomingMsg is PumpNotifyTransactionDoneRequest) { var incoming = incomingMsg as PumpNotifyTransactionDoneRequest; this.textBox1.AppendText("A Trx is done, vol: " + incoming.VOL_升数 + ", amnt:" + incoming.AMN数额 + System.Environment.NewLine); } else if (incomingMsg is PumpAskDataDownloadRequest) { var incoming = incomingMsg as PumpAskDataDownloadRequest; this.textBox1.AppendText("Pump asking for download: " + incoming.Content_内容 + System.Environment.NewLine); } else if (incomingMsg is PumpDataDownloadRequest) { var incoming = incomingMsg as PumpDataDownloadRequest; this.textBox1.AppendText("Pump start for downloading: " + incoming.Content_内容 + System.Environment.NewLine); } else if (incomingMsg is PcAskReadPumpAccumulatorResponse) { var incoming = incomingMsg as PcAskReadPumpAccumulatorResponse; foreach (var incomingNozzleInfo in incoming.MZN_单个油枪) { this.textBox1.AppendText("Pump returned the accumualtor data, nozzle: " + incomingNozzleInfo.NZN_枪号 + ", acc(vol): " + incomingNozzleInfo.V_TOT_升累计 + System.Environment.NewLine); } } else if (incomingMsg is PumpInquiryBlackAndWhiteListRequest) { var incoming = incomingMsg as PumpInquiryBlackAndWhiteListRequest; this.textBox1.AppendText("Pump ask for BlackAndWhiteList check for card: " + incoming.ASN卡应用号 + System.Environment.NewLine); } })); }
private void Btn_Connect_Click(object sender, EventArgs e) { var checkedComPorts = this.CheckedListBox_availableComPorts.CheckedItems.Cast <string>(); if (!checkedComPorts.Any()) { return; } this.Btn_Connect.Enabled = false; this.Btn_Disconnect.Enabled = true; logger.Info("Connecting Com Ports: " + checkedComPorts.Aggregate((acc, n) => acc + ", " + n)); int baud = 9600; Parity parity = Parity.None; int databit = 8; StopBits stopbit = StopBits.One; int iconHorizentalOffset = 0; foreach (var comPortName in checkedComPorts) { var processor = new ComPortProcessor(new SerialPort(comPortName, baud, parity, databit, stopbit), this.activeMode); this.processors.Add(processor); processor.OnMessageReceiving += (_, arg) => { var p = _ as ComPortProcessor; this.LoggingOnUiForIncoming(p, arg.Message); // OnMessageReceiving event fired indicates the COM Port have a real pump connected at other peer, // so create the LogicalPump object, and draw the ICON. if (p.Pump == null) { var pumpIcon = new PictureBox { Location = new Point(iconHorizentalOffset, 0) }; pumpIcon.Paint += (c, d) => { using (var myFont = new Font("Arial", 12)) { d.Graphics.DrawString(p.SerialPort.PortName, myFont, Brushes.Black, new Point(0, 0)); } }; pumpIcon.BackColor = Color.Transparent; pumpIcon.SizeMode = PictureBoxSizeMode.AutoSize; pumpIcon.ImageLocation = @"Images/pump_group_disabled.png";//@"Images/pump_group_newdesign.png"; pumpIcon.Click += (__, ___) => { new PumpSettings(p.Pump).Show(); }; this.panel2.Controls.Add(pumpIcon); iconHorizentalOffset += 70; p.Pump = new LogicalPump { ComPortName = p.SerialPort.PortName, PumpIcon = pumpIcon }; p.Pump.PropertyChanged += (a, b) => { if (b.PropertyName == "PumpState") { this.textBox1.AppendText("Pump state changed to " + p.Pump.PumpState + System.Environment.NewLine); if (p.Pump.PumpState == LogicalPump.LogicalPumpState.CardInserted) { p.Pump.PumpIcon.ImageLocation = @"Images/pump_group_card_insert.png"; } else if (p.Pump.PumpState == LogicalPump.LogicalPumpState.Idle) { p.Pump.PumpIcon.ImageLocation = @"Images/pump_group_newdesign.png"; } } }; System.Timers.Timer timelyCheck = new Timer(5000); timelyCheck.Elapsed += (___, __) => { var timelyQueryAcc = new PcAskReadPumpAccumulator(); timelyQueryAcc.SetMessageSequenceNumber(p.Pump.LastSendMsgSequenceNumber++); p.Write(timelyQueryAcc); }; timelyCheck.Start(); } }; processor.Start(); this.textBox1.AppendText("Connected to ComPort: " + comPortName + System.Environment.NewLine); logger.Info("Connected to ComPort: " + comPortName); } }