/// <summary>
        /// 读取串口的数据
        /// </summary>
        /// <param name="send">发送的原始字节数据</param>
        /// <returns>带接收字节的结果对象</returns>
        public OperateResult <byte[]> ReadBase(byte[] send)
        {
            OperateResult <byte[]> result = null;

            hybirdLock.Enter( );

            try
            {
                isReceiveTimeout  = false;                        // 是否接收超时的标志位
                isReceiveComplete = false;                        // 是否接收完成的标志位
                if (send == null)
                {
                    send = new byte[0];
                }

                SP_ReadData.Write(send, 0, send.Length);
                ThreadPool.QueueUserWorkItem(new WaitCallback(CheckReceiveTimeout), null);
                resetEvent.WaitOne( );
                isReceiveComplete = true;

                if (isReceiveTimeout)
                {
                    result = new OperateResult <byte[]>(StringResources.Language.ReceiveDataTimeout + ReceiveTimeout);
                }
                else
                {
                    byte[] tmp = new byte[receiveCount];
                    Array.Copy(buffer, 0, tmp, 0, tmp.Length);

                    result = OperateResult.CreateSuccessResult(tmp);
                }
            }
            catch (Exception ex)
            {
                logNet?.WriteException(ToString( ), ex);
                result = new OperateResult <byte[]>( )
                {
                    Message = ex.Message
                };
            }
            finally
            {
                hybirdLock.Leave( );
            }

            receiveCount = 0;
            return(result);
        }
Example #2
0
        private bool disposedValue = false; // 要检测冗余调用

        /// <summary>
        /// 释放当前的对象
        /// </summary>
        /// <param name="disposing">是否在</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)。
                    hybirdLock?.Dispose( );
                    SP_ReadData?.Dispose( );
                }

                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
                // TODO: 将大型字段设置为 null。

                disposedValue = true;
            }
        }
Example #3
0
        /// <summary>
        /// 重写的数据接收方法,需要连续接收,直到CRC校验成功或是超时,接收时间会比较久
        /// </summary>
        /// <param name="sender">串口对象</param>
        /// <param name="e">串口的数据对象</param>
        protected override void SP_ReadData_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            while (true)
            {
                Thread.Sleep(20);
                if (SP_ReadData.BytesToRead < 1)
                {
                    continue;
                }

                // 继续接收数据
                receiveCount += SP_ReadData.Read(buffer, receiveCount, SP_ReadData.BytesToRead);

                // CRC校验成功及退出
                byte[] data = new byte[receiveCount];
                Array.Copy(buffer, 0, data, 0, receiveCount);
                if (SoftCRC16.CheckCRC16(data))
                {
                    break;
                }
            }
            resetEvent.Set( );
        }
Example #4
0
        /// <summary>
        /// 串口数据接收的回调方法
        /// </summary>
        /// <param name="sender">数据发送</param>
        /// <param name="e">消息</param>
        protected virtual void SP_ReadData_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            while (true)
            {
                Thread.Sleep(20);

                try
                {
                    if (SP_ReadData.BytesToRead < 1)
                    {
                        break;
                    }
                    // 继续接收数据
                    receiveCount += SP_ReadData.Read(buffer, receiveCount, SP_ReadData.BytesToRead);
                }
                catch (Exception ex)
                {
                    isComError  = true;
                    ComErrorMsg = ex.Message;
                    break;
                }
            }
            resetEvent.Set( );
        }
Example #5
0
 private void SP_ReadData_DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
     Thread.Sleep(20);
     receiveCount = SP_ReadData.Read(buffer, 0, SP_ReadData.BytesToRead);
     resetEvent.Set( );
 }