//突发上传 public string AutoSend(APDUClass apdu) { ASDUClass asduNew = new ASDUClass(1); asduNew.Pack(ASDUClass.TransRes.AutoSend, ASDUClass.FunType.Single_point, 0x6001, (double)Convert.ToByte(SwitchStatus)); APCIClassIFormat apciNew = new APCIClassIFormat(_sr++, _nr); APDUClass apduNew = new APDUClass(apciNew, asduNew); //apci的长度将在此处被设置 byte[] bytes = apduNew.ToArray(); return(log); }
private void button2_Click(object sender, EventArgs e) { ASDUClass clockBuffer = new ASDUClass(); //clockBuffer.Pack(ASDUClass.TransRes.File, ASDUClass.FunType.FileReady); clockBuffer.Pack(textBox3.Text); APDUClass apdu = new APDUClass(new APCIClassIFormat(++sr, nr), clockBuffer); byte[] byf = new byte[] { 0x68, 0x04, 0x0B, 0x00, 0x00, 0x00 }; byte[] bytetemp = new byte[] { 0x68, 0x18, 0x12, 0x00, 0x00, 0x00, 0x40, 0x01, 0x0d, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0x33, 0x03, 0x0e, 0x10, 0x09, 0x13 }; byte[] bytet = new byte[] { 0x68, 0x16, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x97, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, 0x99 }; Send(apdu.ToArray()); }
//回复激活终止, 暂未实现 public APDUClass StopActive(APDUClass apdu) { ASDUClass asduNew = new ASDUClass(1); //类型标识、根据命令确定可变结构限定词的值、设置数据 asduNew.Pack(ASDUClass.TransRes.ActiveStop, ASDUClass.FunType.CalAll, 0, 0x14); APCIClassIFormat apciNew = new APCIClassIFormat(_sr++, _nr); APDUClass apduNew = new APDUClass(apciNew, asduNew); //apci的长度将在此处被设置 //byte[] bytes = apduNew.ToArray(); //log = string.Format("Client received data: {0} From: {1}.\n", apdu.ToString(), Client.ClientSession); log = string.Format("Stop Active \n Client sended data: {0} to: {1}.\n", apduNew.ToString(), Client.ClientSession); return(apduNew); }
/// <summary> /// 对二进制串解报 /// </summary> /// <param name="bytetemp"></param> /// <param name="con"></param> public int UnpackReceive(byte[] bytetemp, Control con,ref ASDUClass.FunType ft, ref ASDUClass.TransRes tr) { try { if ((bytetemp.Length == 1) && (bytetemp[0] == 0xe5)) { //收到 E5 return 1; } if ((bytetemp.Length == 5) && (bytetemp[0] == 0x10) && (bytetemp[4] == 0x16)) { Frame_101 temp = new Frame_101(bytetemp); if (temp.GetControl().PRM == false) { con.Copy(con, temp.GetControl()); Console.WriteLine("RX: ACD:{0} DFC:{1} FUNCTION:{2}\r\n", temp.GetControl().ACD, temp.GetControl().DFC, temp.GetControl().function_0); try { sw = File.AppendText(logfile); sw.WriteLine(DateTime.Now.ToString() + " RX: ACD:{0} DFC:{1} FUNCTION:{2}\r\n", temp.GetControl().ACD, temp.GetControl().DFC, temp.GetControl().function_0); sw.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } //收到固定长度帧 return 2; } } if (bytetemp.Length > 5) { Frame_101 vtemp = new Frame_101(bytetemp); if ((vtemp != null) && (vtemp.GetControl().PRM == false)) { Console.WriteLine("RX: ACD:{0} DFC:{1} FUNCTION:{2} Time:{3} ASDUType:{4} Res:{5}", vtemp.GetControl().ACD, vtemp.GetControl().DFC, vtemp.GetControl().function_0, DateTime.Now, vtemp.GetAsduType(), vtemp.Res); //Console.WriteLine("RX: {0}", vtemp.ToString()); try { sw = File.AppendText(logfile); sw.WriteLine(DateTime.Now.ToString() + " RX: ACD:{0} DFC:{1} FUNCTION:{2} ASDUType:{3} Res:{4}", vtemp.GetControl().ACD, vtemp.GetControl().DFC, vtemp.GetControl().function_0, vtemp.GetAsduType(), vtemp.Res); sw.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } con.Copy(con, vtemp.GetControl()); ft = vtemp.GetAsduType(); tr = vtemp.Res; var datas = vtemp.GetData(); foreach (var data in datas) { //if (data.Addr == 0) continue; Console.WriteLine("RX: " + "addr:" + data.Addr.ToString() + " " + "data:" + data.Data.ToString() + " " + "time:" + data.Time.ToString()); //try //{ // sw = File.AppendText(logfile); // sw.WriteLine("RX: " + "addr:" + data.Addr.ToString() + " " + // "data:" + data.Data.ToString() + " " + // "time:" + data.Time.ToString()); // sw.Close(); //} //catch (Exception ex) //{ // Console.WriteLine(ex.Message.ToString()); //} numInf numtemp = new numInf(); if (find.TryGetValue(data.Addr, out numtemp)) { numtemp.val = Convert.ToSingle(data.Data); if (data.Time != null) { numtemp.dtm = Convert.ToDateTime(data.Time); } else numtemp.dtm = DateTime.Now; find.Remove(data.Addr); find.Add(data.Addr, numtemp); } else { numtemp.val = Convert.ToSingle(data.Data); if (data.Time != null) { numtemp.dtm = Convert.ToDateTime(data.Time); } else numtemp.dtm = DateTime.Now; find.Add(data.Addr, numtemp); } } Console.WriteLine("\n"); try { sw = File.AppendText(logfile); sw.WriteLine("\r\n"); sw.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } //收到可变长度帧 return 3; } } //收到错误帧 return 4; } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); return 4; } }
/// <summary> /// 总招 /// </summary> /// <param name="varfcb" >是否改变fcb</param> public void RequestAllData(bool varfcb) { Control con; Frame_101 vlm; if (varfcb == false) con = new Control(prm, fcb, fcv, Control.FUNCTION_1.UserDataConfirm, Control.FUNCTION_0.Undef); else { fcb = !fcb; con = new Control(prm, fcb, fcv, Control.FUNCTION_1.UserDataConfirm, Control.FUNCTION_0.Undef); } ASDUClass calBuffer = new ASDUClass(); calBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalAll, m_config_101.m_PublicAddress); vlm = new Frame_101(0x68, con, m_config_101.m_LinkAddress, calBuffer); //port1.Write(vlm.ToArray(), 0, vlm.ToArray().Length); SendList.Enqueue(vlm); }
public void InitLink() { prm = true; fcb = false; fcv = false; Control contemp = new Control(); port1.DataReceived -= DataReceived; //请求链路状态 Control con = new Control(prm, fcb, fcv, Control.FUNCTION_1.RequestLinkState, Control.FUNCTION_0.Undef); Frame_101 flm = new Frame_101( 0x10,con, m_config_101.m_LinkAddress); while (true) { port1.Write(flm.ToArray(), 0, flm.ToArray().Length); try { sw = File.AppendText(logfile); sw.WriteLine(DateTime.Now.ToString() + " TX: " + flm.ToString()); sw.Close(); } catch { } Console.WriteLine("TX: " + flm.ToString()); Thread.Sleep(1000); byte[] btemp = new byte[port1.BytesToRead]; port1.Read(btemp, 0, port1.BytesToRead); if (BitConverter.ToString(btemp, 0) == "") continue; try { sw = File.AppendText(logfile); sw.WriteLine(DateTime.Now.ToString() + " RX: " + BitConverter.ToString(btemp, 0).Replace("-", " ")); sw.Close(); } catch { } Console.WriteLine("RX: " + BitConverter.ToString(btemp, 0).Replace("-", " ")); if (UnpackReceive(btemp, contemp) == 2) { if ((contemp.PRM == false) && (contemp.function_0 == Control.FUNCTION_0.RespondLinkState)) break; } } //复位远方链路 con = new Control(prm, fcb, fcv, Control.FUNCTION_1.ResetRemoteLink, Control.FUNCTION_0.Undef); flm = new Frame_101(0x10, con, m_config_101.m_LinkAddress); while (true) { port1.Write(flm.ToArray(), 0, flm.ToArray().Length); try { sw = File.AppendText(logfile); sw.WriteLine(DateTime.Now.ToString() + " TX: " + flm.ToString()); sw.Close(); } catch { } Console.WriteLine("TX: " + flm.ToString()); Thread.Sleep(1000); byte[] btemp = new byte[port1.BytesToRead]; port1.Read(btemp, 0, port1.BytesToRead); if (BitConverter.ToString(btemp, 0) == "") continue; Console.WriteLine("RX: " + BitConverter.ToString(btemp, 0).Replace("-", " ")); try { sw = File.AppendText(logfile); sw.WriteLine(DateTime.Now.ToString() + " RX: " + BitConverter.ToString(btemp, 0).Replace("-", " ")); sw.Close(); } catch { } if (UnpackReceive(btemp, contemp) == 2) { if ((contemp.PRM == false) && (contemp.function_0 == Control.FUNCTION_0.AckLink)) { fcb = true; fcv = true; break; } } } //是否有一级数据 if (contemp.ACD) {//招唤一级数据 con = new Control(prm, fcb, fcv, Control.FUNCTION_1.RequestPrimaryData, Control.FUNCTION_0.Undef); flm = new Frame_101(0x10, con, m_config_101.m_LinkAddress); port1.Write(flm.ToArray(), 0, flm.ToArray().Length); Console.WriteLine("TX: " + flm.ToString()); try { sw = File.AppendText(logfile); sw.WriteLine(DateTime.Now.ToString() + " TX: " + flm.ToString()); sw.Close(); } catch { } } else { //招唤二级数据 //con = new Control(prm, fcb, fcv, Control.FUNCTION_1.RequestSeconData, Control.FUNCTION_0.Undef); //flm = new Frame_101(0x10, con, m_config_101.m_LinkAddress); //port1.Write(flm.ToArray(), 0, flm.ToArray().Length); //总招 con = new Control(prm, fcb, fcv, Control.FUNCTION_1.UserDataConfirm, Control.FUNCTION_0.Undef); ASDUClass calBuffer = new ASDUClass(); calBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalAll, m_config_101.m_PublicAddress); flm = new Frame_101(0x68, con, m_config_101.m_LinkAddress, calBuffer); port1.Write(flm.ToArray(), 0, flm.ToArray().Length); Console.WriteLine("TX: " + flm.ToString()); try { sw = File.AppendText(logfile); sw.WriteLine(DateTime.Now.ToString() + " TX: " + flm.ToString()); sw.Close(); } catch { } } port1.DataReceived += new SerialDataReceivedEventHandler(DataReceived);//DataReceived事件委托 }
/// <summary> /// 定时发送遥脉请求的定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ym_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Control con; Frame_101 vlm; fcb = !fcb; con = new Control(prm, fcb, fcv, Control.FUNCTION_1.UserDataConfirm, Control.FUNCTION_0.Undef); ASDUClass calymBuffer = new ASDUClass(); calymBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalEnergyPulse, m_config_101.m_PublicAddress); vlm = new Frame_101(0x68, con, m_config_101.m_LinkAddress, calymBuffer); SendList.Clear(); SendList.Enqueue(vlm); fcb = !fcb; con = new Control(prm, fcb, fcv, Control.FUNCTION_1.UserDataConfirm, Control.FUNCTION_0.Undef); ASDUClass readymBuffer = new ASDUClass(); readymBuffer.SetData_QCC(0x05, m_config_101.m_PublicAddress); vlm = new Frame_101(0x68, con, m_config_101.m_LinkAddress, readymBuffer); SendList.Enqueue(vlm); }
/// <summary> /// 定时发送总招请求的定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void callAll_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { FileStream fs = new FileStream(logfile, FileMode.OpenOrCreate, FileAccess.Write); if (fs.Length > 1024000)//文件大小。byte { while (true) { if (fs.CanWrite) { fs.Close(); StreamWriter sw1 = new StreamWriter(logfile); string w = ""; sw1.Write(w); sw1.Close(); break; } Thread.Sleep(200); } } fs.Close(); } catch { } Control con; Frame_101 vlm; fcb = !fcb; con = new Control(prm, fcb, fcv, Control.FUNCTION_1.UserDataConfirm, Control.FUNCTION_0.Undef); ASDUClass calBuffer = new ASDUClass(); calBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalAll, m_config_101.m_PublicAddress); vlm = new Frame_101(0x68, con, m_config_101.m_LinkAddress, calBuffer); //port1.Write(vlm.ToArray(), 0, vlm.ToArray().Length); SendList.Clear(); SendList.Enqueue(vlm); }
//回复第三组召唤:实现 public APDUClass OnRes003(APDUClass apdu, bool bSwitch) { ASDUClass asduNew = new ASDUClass(1); return(apduNew); }
//回复第二组召唤 public APDUClass OnRes002(APDUClass apdu) { ASDUClass asduNew = new ASDUClass(1); return(apduNew); }
/// <summary> /// 定时发送总招请求的定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void callAll_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { ASDUClass calBuffer = new ASDUClass(); calBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalAll, m_config_104.m_PublicAddress); SendList.Enqueue(new APDUClass(new APCIClassIFormat(sr++, nr), calBuffer)); }
/// <summary> /// 后台线程提交数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { try { FileInfo file = new FileInfo(logfile); long size = file.Length;//文件大小。byte if (size > 1024000) { StreamWriter sw1 = new StreamWriter(logfile); string w = ""; sw1.Write(w); sw1.Close(); } } catch { } if (e.ProgressPercentage == -1) { APDUClass temp = new APDUClass(e.UserState as byte[]); APCIClass.UISFormat dataFormat = temp.GetApciType(); if (temp != null) { try { sw = File.AppendText(logfile); sw.WriteLine(DateTime.Now.ToString() + " TX: " + "Type:" + temp.GetAsduType().ToString() + " Res:" + temp.Res.ToString() + " SR:" + temp.GetSR().ToString() + " NR:" + temp.GetNR().ToString() + "\r\n" + temp.ApciToString() + temp.AsduToString() + "\r\n"); sw.Close(); } catch {} Console.WriteLine(DateTime.Now.ToString() + " TX: " + "Type:" + temp.GetAsduType().ToString() + " Res:" + temp.Res.ToString() + " SR:" + temp.GetSR().ToString() + " NR:" + temp.GetNR().ToString() + "\n" + temp.ApciToString() + temp.AsduToString() + "\r\n"); } } else if (e.ProgressPercentage > 0) { byte[] receive = new byte[e.ProgressPercentage]; Array.Copy(e.UserState as byte[], receive, e.ProgressPercentage); if (receive[0] == 0x68) { int i = 0; while (i < receive.Length) { try { int recevlen = receive[i + 1]; byte[] receivetemp = new byte[recevlen +2]; Array.Copy(receive, i, receivetemp, 0, receivetemp.Length); i = i + receivetemp.Length; APDUClass temp = new APDUClass(receivetemp); APCIClass.UISFormat dataFormat = temp.GetApciType(); //if (dataFormat == APCIClass.UISFormat.I && !(nr > temp.GetSR())) if (dataFormat == APCIClass.UISFormat.I) { nr = (short)temp.GetSR(); if (nr > short.MaxValue) { nr = 0; } nrflag = true; if (((nr % m_config_104.m_AckNW) == m_config_104.m_AckNW - 1)||(sr == 0)) { SendList.Enqueue(new APDUClass(new APCIClassSFormat(nr), null)); for (int id = 0; id < SendList.Count - 1; id++) { APDUClass tempsend = SendList.Dequeue(); SendList.Enqueue(tempsend); } nrflag = false; } if ((temp.GetAsduType() == ASDUClass.FunType.CalAll) && (temp.Res == ASDUClass.TransRes.ActiveEnd)) { ASDUClass calymBuffer = new ASDUClass(); calymBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalEnergyPulse, m_config_104.m_PublicAddress); SendList.Enqueue(new APDUClass(new APCIClassIFormat(sr++, nr), calymBuffer)); ASDUClass readymBuffer = new ASDUClass(); readymBuffer.SetData_QCC(0x05, m_config_104.m_PublicAddress); SendList.Enqueue(new APDUClass(new APCIClassIFormat(sr++, nr), readymBuffer)); } } if (dataFormat == APCIClass.UISFormat.U) { if ((APCIClassUFormat.UFormatType)temp.GetApci().GetControlByte() == APCIClassUFormat.UFormatType.StartSet) SendList.Enqueue(new APDUClass(new APCIClassUFormat(APCIClassUFormat.UFormatType.StartConfirm), null)); if ((APCIClassUFormat.UFormatType)temp.GetApci().GetControlByte() == APCIClassUFormat.UFormatType.TestSet) SendList.Enqueue(new APDUClass(new APCIClassUFormat(APCIClassUFormat.UFormatType.TestConfirm), null)); if ((APCIClassUFormat.UFormatType)temp.GetApci().GetControlByte() == APCIClassUFormat.UFormatType.StopSet) SendList.Enqueue(new APDUClass(new APCIClassUFormat(APCIClassUFormat.UFormatType.StopConfirm), null)); if ((APCIClassUFormat.UFormatType)temp.GetApci().GetControlByte() == APCIClassUFormat.UFormatType.StartConfirm) { ASDUClass calBuffer = new ASDUClass(); calBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalAll, m_config_104.m_PublicAddress); SendList.Enqueue(new APDUClass(new APCIClassIFormat(sr++, nr), calBuffer)); } } ////对方发来确认帧,在核实确认序号等于或小于本分的I帧发送序号,再次发送I帧 ////此部分在“实现发送控制命令”时,给以完成。 //else if (dataFormat == APCIClass.UISFormat.S) //{ // if (nr > short.MaxValue) // { // nr = 0; // } // Console.WriteLine(dataFormat.ToString()); // SendList.Enqueue(new APDUClass(new APCIClassSFormat(nr++), null)); //} if (temp != null) { try { sw = File.AppendText(logfile); sw.WriteLine(DateTime.Now.ToString() + " RX: " + "Type:" + temp.GetAsduType().ToString() + " Res:" + temp.Res.ToString() + " SR:" + temp.GetSR().ToString() + " NR:" + temp.GetNR().ToString() + "\r\n" + BitConverter.ToString(receivetemp, 0).Replace("-", " ")); sw.Close(); } catch { } Console.WriteLine(DateTime.Now.ToString() + " RX: " + "Type:" + temp.GetAsduType().ToString() + " Res:" + temp.Res.ToString() + " SR:" + temp.GetSR().ToString() + " NR:" + temp.GetNR().ToString() + "\n" + BitConverter.ToString(receivetemp, 0).Replace("-", " ")); var datas = temp.GetData(); foreach (var data in datas) { if (data.Addr == 0) continue; try { sw = File.AppendText(logfile); sw.WriteLine("RX " + "addr:" + data.Addr.ToString() + " " + "data:" + data.Data.ToString() + " " + "time:" + data.Time.ToString()); sw.Close(); } catch { } //Console.WriteLine("RX " + "addr:" + data.Addr.ToString() + " " + // "data:" + data.Data.ToString() + " " + // "time:" + data.Time.ToString()); numInf numtemp = new numInf(); if (find.TryGetValue(data.Addr, out numtemp)) { numtemp.val = Convert.ToSingle(data.Data); if (data.Time != null) { numtemp.dtm = Convert.ToDateTime(data.Time); } else numtemp.dtm = DateTime.Now; find.Remove(data.Addr); find.Add(data.Addr, numtemp); } else { numtemp.val = Convert.ToSingle(data.Data); if (data.Time != null) { numtemp.dtm = Convert.ToDateTime(data.Time); } else numtemp.dtm = DateTime.Now; find.Add(data.Addr, numtemp); } } try { sw = File.AppendText(logfile); sw.WriteLine("\r\n"); sw.Close(); } catch { } Console.WriteLine("\r\n"); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } } } } }
/// <summary> /// 定时发送遥脉请求的定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ym_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { ASDUClass calymBuffer = new ASDUClass(); calymBuffer.Pack(ASDUClass.TransRes.Active, ASDUClass.FunType.CalEnergyPulse, m_config_104.m_PublicAddress); SendList.Enqueue(new APDUClass(new APCIClassIFormat(sr++, nr), calymBuffer)); ASDUClass readymBuffer = new ASDUClass(); readymBuffer.SetData_QCC(0x05, m_config_104.m_PublicAddress); SendList.Enqueue(new APDUClass(new APCIClassIFormat(sr++, nr), readymBuffer)); }