Пример #1
0
        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事件委托
        }
Пример #2
0
        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());
            }
        }