public void ProcessMessage(MessageTemplateBase incomingMsg) { var safe = this.OnMessageReceiving; var arg = new ComPortEventArg() { Continue = true, Message = incomingMsg }; safe?.Invoke(this, arg); var parser = new Parser(); // skip the logging for PumpGenericInquiryRequest msg since it's too much. if (!(incomingMsg is PumpGenericInquiryRequest)) { logger.Debug(port.PortName + " " + incomingMsg.ToLogString()); } // Null LogicalPump object indicates comm is not ready yet. if (this.Pump == null) { return; } this.Pump.Pulse(); if (!this.Pump.AllowCommWithRealPhsicalPump) { return; } if (incomingMsg is PumpGenericInquiryRequest) { if (this.tryReadPumpGenericInfoAtBeginning) { /* here we send the same versions for all downloadable content for simplify */ var outgoing = new PcGenericInquiryWithRichInfoResponse(); outgoing.SetPcTime(DateTime.Now); outgoing.BL_VER = this.Pump.BL_VER; outgoing.ADD_BL_VER = this.Pump.ADD_BL_VER; outgoing.DEL_BL_VER = this.Pump.DEL_BL_VER; outgoing.WH_VER = this.Pump.WH_VER; outgoing.PRC_VER = this.Pump.PRC_VER; outgoing.Sta_VER = this.Pump.Sta_VER; outgoing.SELF_D_VER = PcGenericInquiryWithRichInfoResponse.更新标志.无数据; outgoing.SOFT_FLAG = PcGenericInquiryWithRichInfoResponse.更新标志.无数据; outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++); this.Write(outgoing); } else { /* here we send the confirmed (by cashier, since `AllowCommWithRealPhsicalPump` is true now) versions for all downloadable content*/ var outgoing = new PcGenericInquiryWithRichInfoResponse(); outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++); outgoing.SetPcTime(DateTime.Now); outgoing.BL_VER = LogicalPump.GetFundamentalBlackList().Version; outgoing.ADD_BL_VER = (byte)LogicalPump.GetIncrementalBlackList().Version; outgoing.DEL_BL_VER = (byte)LogicalPump.GetDeletionBlackList().Version; outgoing.WH_VER = (byte)LogicalPump.GetWhiteList().Version; // below use the latest one. outgoing.PRC_VER = this.Pump.FulePriceList.VER_版本; outgoing.Sta_VER = this.Pump.PumpStationInfo.Ver; outgoing.SELF_D_VER = PcGenericInquiryWithRichInfoResponse.更新标志.无数据; outgoing.SOFT_FLAG = PcGenericInquiryWithRichInfoResponse.更新标志.无数据; this.Write(outgoing); } } else if (incomingMsg is PcReadPumpGenericInfoResponse) { var incoming = incomingMsg as PcReadPumpGenericInfoResponse; this.Pump.ReloadFrom(incoming); } else if (incomingMsg is PumpNotifyTransactionDoneRequest) { var incoming = incomingMsg as PumpNotifyTransactionDoneRequest; this.Pump.StackTransaction(LogicalTransaction.LoadFrom(incoming)); if (this.Pump.Transactions.Count() > this.Pump.KeepMaxTransactionNumber) { this.Pump.ClearTransaction(0); } var outgoing = new PumpNotifyTransactionDoneResponse(); outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++); outgoing.Result = PumpNotifyTransactionDoneResponse.PumpNotifyTransactionDoneResponseResult.正确; this.Write(outgoing); if (this.Pump.ReadPumpAccumualtorAfterTrxDone) { var request = new PcAskReadPumpAccumulator(); request.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++); this.Write(request); } } else if (incomingMsg is PumpAskDataDownloadRequest) { var incoming = incomingMsg as PumpAskDataDownloadRequest; var outgoing = new PumpAskDataDownloadResponse(); outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++); int downloadingContentLength = 0; switch (incoming.Content_内容) { case PumpAskDataDownloadRequest.PumpAskDataDownloadType.基础黑名单: downloadingContentLength = parser.Serialize(LogicalPump.GetWhiteList()).Length; break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.新增黑名单: break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.新删黑名单: break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.白名单: downloadingContentLength = parser.Serialize(LogicalPump.GetWhiteList()).Length; break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.油品油价表: downloadingContentLength = parser.Serialize(LogicalPump.GetDefaultFuelPriceList()).Length; break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.油站通用信息: downloadingContentLength = parser.Serialize(LogicalPump.GetDefaultPumpStationInfo()).Length; break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.私有数据: break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.载程序: break; default: throw new ArgumentOutOfRangeException(); } // tell pump this length of content will start downloading outgoing.BL_LEN_长度 = downloadingContentLength; outgoing.Content_内容 = incoming.Content_内容; this.Write(outgoing); } else if (incomingMsg is PumpDataDownloadRequest) { var incoming = incomingMsg as PumpDataDownloadRequest; var outgoing = new PumpDataDownloadResponse(); outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++); byte[] fullDownloadingBytes = null; switch (incoming.Content_内容) { case PumpAskDataDownloadRequest.PumpAskDataDownloadType.基础黑名单: fullDownloadingBytes = parser.Serialize(LogicalPump.GetWhiteList()); break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.新增黑名单: fullDownloadingBytes = parser.Serialize(LogicalPump.GetIncrementalBlackList()); break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.新删黑名单: fullDownloadingBytes = parser.Serialize(LogicalPump.GetDeletionBlackList()); break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.白名单: fullDownloadingBytes = parser.Serialize(LogicalPump.GetWhiteList()); break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.油品油价表: fullDownloadingBytes = parser.Serialize(LogicalPump.GetDefaultFuelPriceList()); break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.油站通用信息: fullDownloadingBytes = parser.Serialize(LogicalPump.GetDefaultPumpStationInfo()); break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.私有数据: break; case PumpAskDataDownloadRequest.PumpAskDataDownloadType.载程序: break; default: throw new ArgumentOutOfRangeException(); } outgoing.Content_内容 = incoming.Content_内容; outgoing.S_OFFSET_段偏移 = incoming.S_OFFSET_段偏移; outgoing.SEG_段数_segs = incoming.SEG_段数_segs; outgoing.DATA_Content_数据内容 = fullDownloadingBytes.Skip(16 * incoming.S_OFFSET_段偏移).Take(incoming.SEG_段数_segs * 16).ToList(); this.Write(outgoing); } else if (incomingMsg is PumpStateChangeRequest) { //var previousState = Pump.PumpState; var incoming = incomingMsg as PumpStateChangeRequest; Pump.UpdatePumpStateByMessage(incoming); } else if (incomingMsg is PcAskReadPumpAccumulatorResponse) { var incoming = incomingMsg as PcAskReadPumpAccumulatorResponse; foreach (var incomingNozzleInfo in incoming.MZN_单个油枪) { if (Pump.Nozzles_油枪组.Any(p => p.NozzleNumber == incomingNozzleInfo.NZN_枪号)) { Pump.Nozzles_油枪组.First(p => p.NozzleNumber == incomingNozzleInfo.NZN_枪号).VolumnAccumulator = incomingNozzleInfo.V_TOT_升累计; } else { Pump.AddNozzle(new LogicalNozzle() { NozzleNumber = incomingNozzleInfo.NZN_枪号, VolumnAccumulator = incomingNozzleInfo.V_TOT_升累计 }); } } } else if (incomingMsg is PumpInquiryBlackAndWhiteListRequest) { var incoming = incomingMsg as PumpInquiryBlackAndWhiteListRequest; var outgoing = new PumpInquiryBlackAndWhiteListResponse(); outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++); outgoing.ASN卡应用号 = incoming.ASN卡应用号; outgoing.M_FLAG_匹配标志 = PumpInquiryBlackAndWhiteListResponse.PumpInquiryBlackAndWhiteListResult.匹配; this.Write(outgoing); } var safe1 = this.OnMessageProcessed; var arg1 = new ComPortEventArg() { Continue = true, Message = incomingMsg }; safe1?.Invoke(this, arg1); }
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); } }