static void Upload() { using (var stack = new NibusStack( new SerialTransport("COM7", 115200, true), new NibusDataCodec(), new NmsCodec())) { var nmsProtocol = stack.GetCodec <NmsCodec>().Protocol; try { var sw = new Stopwatch(); var total = 0; var options = new NibusOptions { Progress = new Progress <object>( cb => { if (total == 0) { total = (int)cb; Console.WriteLine("Total: {0}", total); } else { Console.Write("{0}\t{1}\r", cb, (int)cb * 100 / total); } }) }; sw.Start(); var result = nmsProtocol.UploadDomainAsync(Address.CreateMac(0x6c, 0xea), "NVRAM", 0, 0, options).Result; sw.Stop(); Console.WriteLine("Duration: {0}", sw.Elapsed); using (var file = File.Create(@"c:\temp\nvram.hex")) { file.Write(result, 0, result.Length); } } catch (AggregateException e) { Console.WriteLine(e.Flatten().InnerException); } } }
/// <summary> /// Создаем стек NiBUS и подписываемся на события. /// </summary> /// TODO: Имя COM-порта зашито в код, измените на свой. private void InitNibus() { try { _stack = NibusStack.CreateSerialNmsStack("COM14"); var nmsCodec = _stack.GetCodec <NmsCodec>(); var nmsProtocol = nmsCodec.Protocol; _iceHockeyProtocol = new IceHockeyProtocol(nmsProtocol); _iceHockeyProtocol.HomeScoreChanged += OnHomeScoreChanged; _iceHockeyProtocol.VisitorScoreChanged += OnVisitorScoreChanged; _iceHockeyProtocol.PenaltyStatChanged += OnPenaltyStatChanged; _iceHockeyProtocol.TimerChanged += OnTimerChanged; } catch (Exception e) { MessageBox.Show(e.Message, "Ошибка создания стека NiBUS.", MessageBoxButtons.OK, MessageBoxIcon.Error); buttonLoad.Enabled = false; buttonSynchronize.Enabled = false; } }
static void Ping() { using (var stack = new NibusStack( new SerialTransport("COM7", 115200, true), new NibusDataCodec(), new NmsCodec())) { var nmsProtocol = stack.GetCodec <NmsCodec>().Protocol; var et = new List <long>(102); var destanation = Address.CreateMac(0x20, 0x44); var sw = new Stopwatch(); for (int i = 0; i < 102; i++) { var ping = nmsProtocol.Ping(destanation); sw.Restart(); nmsProtocol.ReadValueAsync(destanation, 3).Wait(); sw.Stop(); Console.WriteLine("Ping: {0}, Read: {1}", ping, sw.ElapsedMilliseconds); et.Add(ping); } var first = et[0]; et.RemoveAt(0); et.Sort(); Console.WriteLine("Min: {0}, Max: {1}, average: {2}, mediana: {3}, first: {4}", et.First(), et.Last(), et.Average(), et[50], first); //et = new List<long>(102); //for (int i = 0; i < 102; i++) //{ // var ping = nmsProtocol.PingAsync(destanation).Result; // //Console.WriteLine("Ping: {0}", ping); // et.Add(ping); //} //first = et[0]; //et.RemoveAt(0); //et.Sort(); //Console.WriteLine("Async version Min: {0}, Max: {1}, average: {2}, mediana: {3}, first: {4}", et.First(), et.Last(), et.Average(), et[50], first); } }
private void ReadValueAsync_ThrowTimeout() { var nmsProtocol = _stack.GetCodec <NmsCodec>().Protocol; var options = new NibusOptions { Attempts = 5, Timeout = TimeSpan.FromSeconds(1) }; try { nmsProtocol.ReadValueAsync(Address.CreateMac(1, 2, 3, 4, 5, 6), 2, options).Wait(); } catch (AggregateException e) { throw e.Flatten().InnerException; } }