Example #1
0
        /// <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;
            }
        }
Example #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());
            }
        }