コード例 #1
0
        /// <summary>
        /// 读取串口的数据
        /// </summary>
        /// <param name="send"></param>
        /// <returns></returns>
        public OperateResult <byte[]> ReadBase(byte[] send)
        {
            OperateResult <byte[]> result = null;

            hybirdLock.Enter( );

            try
            {
                if (send == null)
                {
                    send = new byte[0];
                }

                SP_ReadData.Write(send, 0, send.Length);
                resetEvent.WaitOne( );
                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( );
            }

            return(result);
        }
コード例 #2
0
        /// <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;                        // 是否接收完成的标志位
                isComError        = false;                        // 是否异常的标志
                ComErrorMsg       = string.Empty;
                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 (isComError)
                {
                    result = new OperateResult <byte[]>(ComErrorMsg);
                }
                else 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);
        }