【发布时间】:2017-02-07 16:54:58
【问题描述】:
实际上,我在 stackoverflow 中发现了一些类似的东西,但它对我不起作用。他们想在给定的时间(23:00)退出他们的应用程序。我想在 3000 秒后退出它。对我来说,这个答案很有帮助,现在它正在起作用,重点是,重要的是,你将这段代码放在你的程序中的什么位置。
此代码用于管理多波束 RFID 阅读器以定位标签。它将为您提供 x 和 y 坐标(以及一些其他信息 - EPC、时间戳、报告类型等)。实际上程序运行正常,但我只想运行 5 分钟。我正在测量不同设置的阅读器的准确性,如果我的所有测量结果都是 3000 秒(或其他),它会更具可比性。现在,如果我按回车,程序就会退出。
我搜索并尝试了很多,但它不想为我工作。我希望有人可以在这里提供帮助。
代码如下:
using System;
using Impinj.OctaneSdk;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace OctaneSdkExamples
{
class Program
{
// Create an instance of the ImpinjReader class.
static ImpinjReader reader = new ImpinjReader();
static void Main(string[] args)
{
try
{
// Connect to the reader.
// Change the ReaderHostname constant in SolutionConstants.cs
// to the IP address or hostname of your reader.
reader.Connect(SolutionConstants.ReaderHostname);
// Assign the LocationReported event handler.
// This specifies which method to call
// when a location report is available.
reader.LocationReported += OnLocationReported;
// Get the default settings
// We'll use these as a starting point
// and then modify the settings we're
// interested in.
Settings settings = reader.QueryDefaultSettings();
// Put the xArray into location mode
settings.SpatialConfig.Mode = SpatialMode.Location;
// Enable all three report types
settings.SpatialConfig.Location.EntryReportEnabled = true;
settings.SpatialConfig.Location.UpdateReportEnabled = true;
settings.SpatialConfig.Location.ExitReportEnabled = true;
// Set xArray placement parameters
// The mounting height of the xArray, in centimeters
settings.SpatialConfig.Placement.HeightCm = 100
;
// These settings aren't required in a single xArray environment
// They can be set to zero (which is the default)
settings.SpatialConfig.Placement.FacilityXLocationCm = 0;
settings.SpatialConfig.Placement.FacilityYLocationCm = 0;
settings.SpatialConfig.Placement.OrientationDegrees = 0;
// Set xArray location parameters
settings.SpatialConfig.Location.ComputeWindowSeconds = 10;
settings.ReaderMode = ReaderMode.AutoSetDenseReader;
settings.Session = 2;
settings.SpatialConfig.Location.TagAgeIntervalSeconds = 20;
// Specify how often we want to receive location reports
settings.SpatialConfig.Location.UpdateIntervalSeconds = 5;
// Set this to true if the maximum transmit power is desired, false if a custom value is desired
settings.SpatialConfig.Location.MaxTxPower = false;
// If MaxTxPower is set to false, then a custom power can be used. Provide a power in .25 dBm increments
settings.SpatialConfig.Location.TxPowerInDbm = 23.00;
// Disable antennas targeting areas from which we may not want location reports,
// in this case we're disabling antennas 10 and 15
List<ushort> disabledAntennas = new List<ushort> { };
settings.SpatialConfig.Location.DisabledAntennaList = disabledAntennas;
// Uncomment this is you want to filter tags
/*
// Setup a tag filter.
// Only the tags that match this filter will respond.
// We want to apply the filter to the EPC memory bank.
settings.Filters.TagFilter1.MemoryBank = MemoryBank.Epc;
// Start matching at the third word (bit 32), since the
// first two words of the EPC memory bank are the
// CRC and control bits. BitPointers.Epc is a helper
// enumeration you can use, so you don't have to remember this.
settings.Filters.TagFilter1.BitPointer = BitPointers.Epc;
// Only match tags with EPCs that start with "3008"
settings.Filters.TagFilter1.TagMask = "3008";
// This filter is 16 bits long (one word).
settings.Filters.TagFilter1.BitCount = 16;
// Set the filter mode. Use only the first filter
settings.Filters.Mode = TagFilterMode.OnlyFilter1;
*/
// Apply the newly modified settings.
reader.ApplySettings(settings);
// Start the reader
reader.Start();
timer1_Tick(3000);
// Wait for the user to press enter.
Console.WriteLine("Press enter to exit");
Console.ReadLine();
// Apply the default settings before exiting.
reader.ApplyDefaultSettings();
// Disconnect from the reader.
reader.Disconnect();
}
catch (OctaneSdkException e)
{
// Handle Octane SDK errors.
Console.WriteLine("Octane SDK exception: {0}", e.Message);
}
catch (Exception e)
{
// Handle other .NET errors.
Console.WriteLine("Exception : {0}", e.Message);
}
}
// This event handler will be called when a location report is ready.
static void OnLocationReported(ImpinjReader reader, LocationReport report)
{
// Print out the report details
Console.WriteLine("Location report");
Console.WriteLine(" Type = {0}", report.ReportType);
Console.WriteLine(" EPC = {0}", report.Epc);
Console.WriteLine(" X = {0} cm", report.LocationXCm);
Console.WriteLine(" Y = {0} cm", report.LocationYCm);
Console.WriteLine(" Timestamp = {0} ({1})", report.Timestamp, report.Timestamp.LocalDateTime);
Console.WriteLine(" Read count = {0}", report.ConfidenceFactors.ReadCount);
// Saving data
string path = @"b:\Master Thesis\xArray\Tests\Test 3 - Height-100cm, Disabled Antennas - , TxPowerInDbm-23.25, UpdateIntervalSeconds-5, ComputeWindowSeconds-10, ReaderMode_AutoSetDenseReader, TagAgeIntervalSeconds-20, tags 40 cm from origo .txt";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Type, Epc, X Localization, Y localization, Timestamp, Local Date Time, Read Count ");
// This text is added only once to the file.
}
}
// This text is always added, making the file longer over time
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(" {0} , {1} , {2} , {3} , {4} , {5} , {6} ", report.ReportType, report.Epc, report.LocationXCm, report.LocationYCm, report.Timestamp, report.Timestamp.LocalDateTime, report.ConfidenceFactors.ReadCount);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
reader.Disconnect();
}
}
}
【问题讨论】:
-
好吧,
Console.ReadLine();是什么让程序按照预期的输入保持打开状态,如果你想让它关闭,为什么还要它存在? -
推荐发布一个最低限度的完整可验证示例。 stackoverflow.com/help/mcve
标签: c# visual-studio time timer exit