/// <summary> /// 表格添加行后的事件触发 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { try { dataGridView1.Rows[e.RowIndex].Selected = true; } catch (Exception ex) { ELogger.Error("Received error:" + ex.Message + ex.StackTrace); } }
static void Main() { try { StartMainWindow(); //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); } catch (Exception ex) { ELogger.Error("Main error:" + ex.Message + ex.StackTrace); } }
/// <summary> /// 异步接收触发函数 /// </summary> /// <param name="ar"></param> private void Received(IAsyncResult ar) { try { if (ReadEnable) { int lenth = socket.EndReceive(ar); backgroundWorker1.ReportProgress(lenth, ar.AsyncState as byte[]); ReceiveOnce(); } } catch (Exception ex) { ELogger.Error("Received error:" + ex.Message + ex.StackTrace); } }
/// <summary> /// 后台线程完成工作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { try { ReadEnable = false; tm.Stop(); tm.Dispose(); System.Threading.Thread.Sleep(1000); socket.Close(); sendTCPClient.Close(); UDPPushClient.Close(); } catch (Exception ex) { ELogger.Error("backgroundWorker1_RunWorkerCompleted error:" + ex.Message + ex.StackTrace); } }
/// <summary> /// 定时发送的定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { ASDUClass ymBuffer = new ASDUClass(); ymBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalEnergyPulse); SendList.Enqueue(new APDUClass(new APCIClassIFormat(++sr, nr), ymBuffer)); if (SendList.Count > 0) { byte[] SendBuffer = SendList.Dequeue().ToArray(); backgroundWorker1.ReportProgress(-1, SendBuffer); socket.Send(SendBuffer); } } catch (Exception ex) { ELogger.Error("tm_Elapsed error:" + ex.Message + ex.StackTrace); } }
/// <summary> /// 后台线程提交数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { try { if (e.ProgressPercentage == -1) { APDUClass temp = new APDUClass(e.UserState as byte[]); APCIClass.UISFormat dataFormat = temp.GetApciType(); if (temp != null) { table.Rows.Add("TX", temp.ApciToString(), temp.AsduToString(), DateTime.Now, temp.GetSR(), temp.GetNR()); } } else if (e.ProgressPercentage > 0) { byte[] receive = new byte[e.ProgressPercentage]; Array.Copy(e.UserState as byte[], receive, e.ProgressPercentage); if (receive.Length > 6 && receive[8] == 0x14 && receive[6] == 0x01) { System.Diagnostics.Debug.WriteLine("??"); } APDUClass temp = new APDUClass(receive); APCIClass.UISFormat dataFormat = temp.GetApciType(); if (dataFormat == APCIClass.UISFormat.I) { //if (nr > short.MaxValue) //{ // nr = 0; //} SendList.Enqueue(new APDUClass(new APCIClassSFormat(++nr), null)); //sr++; } else if (dataFormat == APCIClass.UISFormat.S) { //if (nr > short.MaxValue) //{ // nr = 0; //} SendList.Enqueue(new APDUClass(new APCIClassSFormat(++nr), null)); //sr++; } if (temp != null) { string sSendMessage = string.Empty; string sTagName = string.Empty; TagInfo ATagInfo; table.Rows.Add("RX", temp.ApciToString(), temp.AsduToString(), DateTime.Now, temp.GetSR(), temp.GetNR()); if ((temp.Res.Equals(ASDUClass.TransRes.ResAll)) || (temp.Res.Equals(ASDUClass.TransRes.EnergyCall))) { var datas = temp.GetData(); foreach (var data in datas) { ELogger.Trace("接收:" + data.Addr.ToString() + "=" + data.Data.ToString()); // find.Add(data.Addr,table2.Rows.Add(data.Addr,data.Data,data.Time)); //向网闸外侧服务发送实时数据 if (lstTagInfo.TryGetValue(data.Addr, out ATagInfo)) { //listTgas.Add(ATagInfo); lock (objlock) { if (dicTag.ContainsKey(ATagInfo.TAGNAME)) { dicTag.Remove(ATagInfo.TAGNAME); dicTag.Add(ATagInfo.TAGNAME, data.Data); } else { dicTag.Add(ATagInfo.TAGNAME, data.Data); } } } } } else if (temp.Res == ASDUClass.TransRes.AutoSend) { var datas = temp.GetData(); if (datas == null) { return; } foreach (var data in datas) { ELogger.Trace("接收:" + data.Addr.ToString() + "=" + data.Data.ToString()); try { //向网闸外侧服务发送实时数据 if (lstTagInfo.TryGetValue(data.Addr, out ATagInfo)) { //listTgas.Add(ATagInfo); lock (objlock) { if (dicTag.ContainsKey(ATagInfo.TAGNAME)) { dicTag.Remove(ATagInfo.TAGNAME); dicTag.Add(ATagInfo.TAGNAME, data.Data); } else { dicTag.Add(ATagInfo.TAGNAME, data.Data); } } } else { System.Diagnostics.Debug.WriteLine(""); } } catch (Exception ex) { Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":backgroundWorker1_ProgressChanged send error:" + ex.Message + ex.StackTrace); ELogger.Error("backgroundWorker1_ProgressChanged send error:" + ex.Message + ex.StackTrace); } } } } } if (table.Rows.Count > 20) { table.Rows.RemoveAt(0); } } catch (Exception ex) { ELogger.Error("backgroundWorker1_ProgressChanged error:" + ex.Message + ex.StackTrace); } }
/// <summary> /// 后台线程启动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { try { UDPPushClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint send = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9020); Remote = (EndPoint)(send); APDUClass myBuffer = new APDUClass(new APCIClassUFormat(APCIClassUFormat.UFormatType.StartSet), null); tm = new System.Timers.Timer(); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(IPAddress.Parse(textBox1.Text), 2404); socket.SendTimeout = 500; socket.ReceiveTimeout = 500; sendTCPClient = new TcpClientEx(SENDTCPIP, SENDTCPPORT); SendList.Enqueue(new APDUClass(new APCIClassUFormat(APCIClassUFormat.UFormatType.StartSet), null)); sr = 0; nr = 0; ASDUClass clockBuffer = new ASDUClass(); clockBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.ClockConfirm); ASDUClass calBuffer = new ASDUClass(); calBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalAll); ASDUClass ymBuffer = new ASDUClass(); ymBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalEnergyPulse); // SendList.Enqueue(new APDUClass(new APCIClassIFormat(++sr, nr), clockBuffer)); SendList.Enqueue(new APDUClass(new APCIClassIFormat(++sr, nr), calBuffer)); SendList.Enqueue(new APDUClass(new APCIClassIFormat(++sr, nr), ymBuffer)); //SendList.Enqueue(new APDUClass(new APCIClassUFormat(APCIClassUFormat.UFormatType.StopSet), null)); ReadEnable = true; ReceiveOnce(); tm.AutoReset = true; tm.Interval = scaneRate; tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed); tm.Start(); new Thread(t => { while (true) { foreach (var item in dicTag) { var sSendMessage = string.Format(sJsonRT, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), item.Key, item.Value); if (sendTCPClient.IsConnection) { sendTCPClient.SendMessage(sSendMessage); UDPPushClient.SendTo(EncodeUDP(sSendMessage), Remote); Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + sSendMessage); ELogger.Info("发送:" + sSendMessage); } } Thread.Sleep(PauseTime); } }).Start(); waitHandel.WaitOne(); } catch (Exception ex) { ELogger.Error("backgroundWorker1_DoWork error:" + ex.Message + ex.StackTrace); } }