C#错误(监控接收/发送的字节数)[编辑]

时间:2012-09-20 06:22:19

标签: c#

我想监控RFID阅读器发送和接收的字节数。但是这件事没有编译。 :( 它说它缺少'}'但是如果我放一个,则无法识别transportLogFile(在函数SerialListener上)。 这有什么问题?

版权所有(c)2009 ThingMagic,Inc。

   using System; // system namespace reserved for items associated with .NET Framework class library
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Threading;
using ThingMagic;


namespace Read_v5
{
    class Program
    {
        private TextWriter transportLogFile;
        static void Main(string[] args)
        {
            TextWriter transportLogFile;
            StreamWriter writer = new StreamWriter("log.txt");
            writer.AutoFlush = true;
            transportLogFile = writer;
            using (Reader rdr = Reader.Create("tmr:///com29"))
            {
                if (rdr is SerialReader)
                    rdr.Transport += TimestampListener;

                SimpleReadPlan readplan = new SimpleReadPlan();
                rdr.Connect(); //establish connection
                rdr.ParamSet("/reader/region/id", Reader.Region.OPEN); //region set in OPEN
                rdr.ParamSet("/reader/read/plan", readplan);
                int[] antennas = (int[])rdr.ParamGet("/reader/antenna/connectedPortList");
                rdr.ParamSet("/reader/tagop/antenna", 1);
                rdr.ParamSet("/reader/tagop/protocol", TagProtocol.GEN2);
                //   Console.WriteLine(rdr.ParamGet("/reader/tagop/antenna"));
                //   Console.WriteLine(rdr.ParamGet("/reader/tagop/protocol"));
                //  Console.WriteLine(" " + rdr.ParamGet("/reader/antenna/portList"));
                //   rdr.ParamSet("/reader/antenna/txRxMap", new int[][]{new int[]{1,1,1}});
                readplan.Antennas = new int[] { 1 };
                //    Console.WriteLine(" " + rdr.ParamGet("/reader/antenna/portList").ToString());
                readplan.Protocol = TagProtocol.GEN2;
                //      Console.WriteLine(rdr.ParamGet("/reader/antenna/portList"));
                rdr.ParamSet("/reader/read/asyncOffTime", 100);
                rdr.ParamSet("/reader/read/asyncOnTime", 500);
                rdr.StartReading();
                //     Console.WriteLine("FIN");
                //     int y = 0;
                while (rdr.Equals(rdr))
                {

                    //  Console.WriteLine("\r\n<Do this\r\n");
                    rdr.TagRead += delegate(Object sender, TagReadDataEventArgs e)
                    {
                        Console.WriteLine("EPC: " + e.TagReadData.EpcString);
                        Console.WriteLine("RSSI: " + e.TagReadData.Rssi.ToString());
                        Console.WriteLine("Phase: " + e.TagReadData.Phase.ToString());
                    };
                    Thread.Sleep(200);

                }
                rdr.StopReading();
                rdr.Destroy();
                transportLogFile.Close();
            }
        }
        private void TimestampListener(Object sender, TransportListenerEventArgs e)
        {
         //   TextWriter transportLogFile = null;
            transportLogFile.Write(String.Format("{0} {1}",
                DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"),
                e.Tx ? "Sending" : "Received"));
            for (int i = 0; i < e.Data.Length; i++)
            {
                if ((i & 15) == 0)
                {
                    transportLogFile.WriteLine();
                    transportLogFile.Write("  ");
                }
                transportLogFile.Write("  " + e.Data[i].ToString("X2"));
            }
            transportLogFile.WriteLine();
        }

    }

}

1 个答案:

答案 0 :(得分:1)

在文件末尾添加一个}。从我可以看到命名空间永远不会关闭。

编辑: 变量问题。您可以通过执行以下方法来解决此问题。

class Program 
{ 
    //Declare the variable here:
    private static TextWriter transportLogFile;

    public static void Main(string[] args) 
    {
       //TextWriter transportLogFile = null; 
       transportLogFile = writer;          
    }
    private static void MyMethod()
    {
       //Access transportLogFile here.
    }
}