private async Task ReadHandlerAsync(ReadEventArgs arg) { if (arg.Result) { StringBuilder sbSendValue = new StringBuilder(); foreach (var data in arg.Data) { if (!Data.ContainsKey(data.Key)) { Data.Add(data.Key, data.Value); } else { Data[data.Key] = data.Value; } sbSendValue.AppendFormat("{0}={1};", data.Key, data.Value); await RedisHelper.SetAsync(data.Key, data.Value); } if (sbSendValue.Length > 0) { await RedisHelper.PublishAsync("Collector_Value", sbSendValue.ToString()); } sbSendValue.Clear(); sbSendValue = null; } else { LogError(string.Format("读取超时:{0}。", arg.ErrorMsg)); } arg.Dispose(); arg = null; }
private static void Reader_ReadHandler(object sender, ReadEventArgs e) { Task.Run(() => { if (e.Result == 0 || e.Result == 5) { var CXNCKD = S7.GetIntAt(e.Data, 42); Console.WriteLine(string.Format("一号线-车辆车厢内侧宽度:{0}", CXNCKD)); var CXNCCD = S7.GetIntAt(e.Data, 40); Console.WriteLine(string.Format("一号线-车辆车厢内侧长度:{0}", CXNCCD)); var CKGD = S7.GetIntAt(e.Data, 6); Console.WriteLine(string.Format("一号线-车框高度:{0}", CKGD)); var SDZCBS = S7.GetIntAt(e.Data, 36); Console.WriteLine(string.Format("一号线-设定装车包数:{0}", SDZCBS)); var JHZQCS = S7.GetIntAt(e.Data, 34); Console.WriteLine(string.Format("一号线-计划抓取次数:{0}", JHZQCS)); var DQZCSKZL = S7.GetIntAt(e.Data, 176); Console.WriteLine(string.Format("一号线-当前装车刷卡重量:{0}", DQZCSKZL)); var ZZCS = S7.GetDIntAt(e.Data, 138); Console.WriteLine(string.Format("一号线-总装车数:{0}", ZZCS)); var DTZCS = S7.GetIntAt(e.Data, 142); Console.WriteLine(string.Format("一号线-当天装车数:{0}", DTZCS)); var ZZCZL = S7.GetRealAt(e.Data, 158); Console.WriteLine(string.Format("一号线-总装车重量:{0}", ZZCZL)); var DTZCZL = S7.GetRealAt(e.Data, 162); Console.WriteLine(string.Format("一号线-当天装车重量:{0}", DTZCZL)); var JCSJHG = S7.GetBitAt(e.Data, 70, 2); Console.WriteLine(string.Format("一号线-检测数据合格:{0}", JCSJHG)); var WMBXZHG = S7.GetBitAt(e.Data, 70, 3); Console.WriteLine(string.Format("一号线-尾门板X值合格:{0}", WMBXZHG)); var DQZCZL = S7.GetDIntAt(e.Data, 0); Console.WriteLine(string.Format("一号线-当前装车重量:{0}", DQZCZL)); var CPH = S7.GetStringAt(e.Data, 180, Encoding.GetEncoding("GB2312")); Console.WriteLine(string.Format("一号线-车牌号:{0}", CPH)); } e.Dispose(); e = null; }); }
/// <summary> /// Closes the underlying socket /// </summary> /// <param name="disposing"> /// If true, the EventArg objects will be disposed instead of being re-added to /// the IO pool. This should NEVER be set to true unless we are shutting down the server! /// </param> private void Dispose(bool disposeEventArgs = false) { // Set that the socket is being closed once, and properly if (SocketClosed) { return; } SocketClosed = true; ReadEventArgs.Dispose(); WriteEventArgs.Dispose(); DisposedEventArgs = true; //// Do a shutdown before you close the socket //try //{ // Connection.Shutdown(SocketShutdown.Both); //} //catch (Exception) { } //finally //{ // // Unregister for vents // ReadEventArgs.Completed -= IOComplete; // WriteEventArgs.Completed -= IOComplete; // // Close the connection // Connection.Close(); // Connection = null; //} //// Call Disconnect Event //if (!DisconnectEventCalled && OnDisconnected != null) //{ // DisconnectEventCalled = true; // OnDisconnected(); //} //// If we need to dispose out EventArgs //if (disposeEventArgs) //{ // ReadEventArgs.Dispose(); // WriteEventArgs.Dispose(); // DisposedEventArgs = true; //} //else //{ // // Finally, release this stream so we can allow a new connection // SocketManager.Release(this); // Released = true; //} }
/// <summary> /// Closes the underlying socket /// </summary> /// <param name="DisposeEventArgs"> /// If true, the EventArg objects will be disposed instead of being re-added to /// the IO pool. This should NEVER be set to true unless we are shutting down the server! /// </param> public void Close(bool DisposeEventArgs = false) { // Set that the socket is being closed once, and properly if (SocketClosed) { return; } SocketClosed = true; // Do a shutdown before you close the socket try { Connection.Shutdown(SocketShutdown.Both); } catch (Exception) { } finally { // Unregister for vents ReadEventArgs.Completed -= IOComplete; WriteEventArgs.Completed -= IOComplete; // Close the connection Connection.Close(); Connection = null; } // If we need to dispose out EventArgs if (DisposeEventArgs) { ReadEventArgs.Dispose(); WriteEventArgs.Dispose(); DisposedEventArgs = true; } else { // Finally, release this stream so we can allow a new connection SocketManager.Release(this); Released = true; } // Call Disconnect Event if (!DisconnectEventCalled && OnDisconnect != null) { DisconnectEventCalled = true; OnDisconnect(this); } }