protected virtual void OnWeightCompleted(WeightEventArgs e) { if (WeightCompleted != null) { WeightCompleted(this, e); } }
//数据解析函数 public void OnDataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { string data = _sp.ReadLine(); int bytesLen = data.Length; if (data.Length != _fixedBytesLen) { return; // 数据不符合条件 } String displayWeight = data.Substring(4, 6); // 显示重量(毛重或净重) String grossWeight = data.Substring(10, 6); // 毛重 try { _weight = double.Parse(displayWeight); } catch (Exception ex) { _weight = _preWeight; return; } // 重量变化 if (_weight != _preWeight) { _preWeight = _weight; _count = 0; RaiseWeightChanged(_weight.ToString()); } else { _count++; if (_count > 8 && _weight > 0) // 稳定值 { WeightEventArgs arg = new WeightEventArgs(_weight.ToString()); WeightCompleted(this, arg);// 称重完成事件 _count = 0; } } }
protected virtual void OnWeightCompleted(WeightEventArgs e) { if (WeightCompleted != null) WeightCompleted(this, e); }
//引发事件 public void RaiseWeightChanged(string weight) { WeightEventArgs e = new WeightEventArgs(weight); OnWeightChanged(e); }
//数据解析函数 public void OnDataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { string data = _sp.ReadLine(); int bytesLen = data.Length; if (data.Length != _fixedBytesLen) return; // 数据不符合条件 String displayWeight = data.Substring(4, 6); // 显示重量(毛重或净重) String grossWeight = data.Substring(10, 6); // 毛重 try { _weight = double.Parse(displayWeight); } catch (Exception ex) { _weight = _preWeight; return; } // 重量变化 if (_weight != _preWeight) { _preWeight = _weight; _count = 0; RaiseWeightChanged(_weight.ToString()); } else { _count++; if (_count > 8 && _weight > 0) // 稳定值 { WeightEventArgs arg = new WeightEventArgs(_weight.ToString()); WeightCompleted(this, arg);// 称重完成事件 _count = 0; } } }