Ejemplo n.º 1
0
        /// <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());
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
 /// <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));
 }