/// <summary> /// 招唤二级数据 /// </summary> /// <param name="varfcb" >是否改变fcb</param> public void RequestSeconData(bool varfcb) { Control con; Frame_101 flm; if (varfcb == false) con = new Control(prm, fcb, fcv, Control.FUNCTION_1.RequestSeconData, Control.FUNCTION_0.Undef); else { fcb = !fcb; 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); SendList.Enqueue(flm); }
/// <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事件委托 }
public static void UnPackString(string str) { string stemp = str.ToLower().Replace(" ", ""); byte[] bytetemp = new byte[stemp.Length / 2]; for (int i = 0; i < stemp.Length / 2; i++) { string ss = stemp.Substring(2 * i, 2); bytetemp[i] = Convert.ToByte(ss, 16); } try { if ((bytetemp.Length == 5) && (bytetemp[0] == 0x10) && (bytetemp[4] == 0x16)) { Frame_101 temp = new Frame_101(bytetemp); if (temp.GetControl().PRM) { Console.WriteLine("TX: FCB:{0} FCV:{1} FUNCTION:{2}", temp.GetControl().FCB, temp.GetControl().FCV, temp.GetControl().function_1); } else { Console.WriteLine("RX: ACD:{0} DFC:{1} FUNCTION:{2}", temp.GetControl().ACD, temp.GetControl().DFC, temp.GetControl().function_0); } } 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()); 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()); } Console.WriteLine("\r\n"); } if ((vtemp != null) && (vtemp.GetControl().PRM == true)) { Console.WriteLine("TX: FCB:{0} FCV:{1} FUNCTION:{2} Time:{3} ASDUType:{4} Res:{5}", vtemp.GetControl().FCB, vtemp.GetControl().FCV, vtemp.GetControl().function_1, DateTime.Now, vtemp.GetAsduType(), vtemp.Res); Console.WriteLine("TX: {0}\n\r", vtemp.ToString()); } } } 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) { 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); }