Пример #1
0
        void OnReceived(Object sender, ReceivedEventArgs e)
        {
            var data = e.Data;

            if (data == null || data.Length < 1)
            {
                return;
            }

            BytesOfReceived += data.Length;

            // 处理数据委托
            if (Received != null)
            {
                var e2 = new BufferEventArgs {
                    Value = data
                };
                Received(this, e2);
                if (!e2.Cancel)
                {
                    return;
                }
                // 外部可能修改了数据
                data = e2.Value;
                //if (!BufferEventArgs.Invoke(Received, data)) return null;
            }

            // 处理字符串委托
            if (ReceivedString == null)
            {
                return;
            }

            var cfg = SerialPortConfig.Current;

            var line = "";

            if (cfg.HexShow)
            {
                if (data.Length > 32)
                {
                    line = "[{0}]=\r\n{1}".F(data.Length, data.ToHex("-", 32));
                }
                else
                {
                    line = "[{0}]={1}".F(data.Length, data.ToHex("-", 32));
                }
                if (cfg.HexNewLine)
                {
                    line += Environment.NewLine;
                }
            }
            else
            {
                line = cfg.Encoding.GetString(data);
                if (_stream == null)
                {
                    _stream = new MemoryStream();
                }
                else if (_stream.Length > 10 * 1024 && _stream.Position == _stream.Length) // 达到最大大小时,从头开始使用
                {
                    _stream = new MemoryStream();
                }
                _stream.Write(data);
                _stream.Seek(-1 * data.Length, SeekOrigin.Current);

                if (_reader == null ||
                    _reader.BaseStream != _stream ||
                    _reader.CurrentEncoding != cfg.Encoding)
                {
                    _reader = new StreamReader(_stream, cfg.Encoding);
                }
                line = _reader.ReadToEnd();
                // 替换掉无效数据
                line = line.Replace("\0", null);
            }

            ReceivedString?.Invoke(this, new StringEventArgs {
                Value = line
            });
        }
Пример #2
0
        void OnReceived(Object sender, ReceivedEventArgs e)
        {
            var data = e.Data;
            if (data == null || data.Length < 1) return;

            BytesOfReceived += data.Length;

            // 处理数据委托
            if (Received != null)
            {
                var e2 = new BufferEventArgs { Value = data };
                Received(this, e2);
                if (!e2.Cancel) return;
                // 外部可能修改了数据
                data = e2.Value;
                //if (!BufferEventArgs.Invoke(Received, data)) return null;
            }

            // 处理字符串委托
            if (ReceivedString == null) return;

            var cfg = SerialPortConfig.Current;

            var line = "";
            if (cfg.HexShow)
            {
                if (data.Length > 32)
                    line = "[{0}]=\r\n{1}".F(data.Length, data.ToHex("-", 32));
                else
                    line = "[{0}]={1}".F(data.Length, data.ToHex("-", 32));
                if (cfg.HexNewLine) line += Environment.NewLine;
            }
            else
            {
                line = cfg.Encoding.GetString(data);
                if (_stream == null)
                    _stream = new MemoryStream();
                else if (_stream.Length > 10 * 1024 && _stream.Position == _stream.Length) // 达到最大大小时,从头开始使用
                    _stream = new MemoryStream();
                _stream.Write(data);
                _stream.Seek(-1 * data.Length, SeekOrigin.Current);

                if (_reader == null ||
                    _reader.BaseStream != _stream ||
                    _reader.CurrentEncoding != cfg.Encoding)
                    _reader = new StreamReader(_stream, cfg.Encoding);
                line = _reader.ReadToEnd();
            }

            if (ReceivedString != null) ReceivedString(this, new StringEventArgs { Value = line });
        }