Esempio n. 1
0
        /// <summary>
        /// 串口的数据抵达事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (timer.Enabled == true)
            {
                timer.Enabled = false;
            }
            try
            {
                // 接收缓冲区中数据的字节数
                int int_Len = serialPort.BytesToRead;

                // 接收数据
                byte[] bytes = new byte[int_Len];
                serialPort.Read(bytes, 0, int_Len);

                if (RTxtMsgo != null)
                {
                    RTxtMsgo.WriteTxt(bytes, " RX ", true, 0);
                }

                // 缓冲区溢出
                if (iCurLoc + int_Len > iBufferSize)
                {
                    iCurLoc = 0;
                    return;
                }

                // 将数据存入缓冲区
                for (int i = 0; i < int_Len; i++)
                {
                    byteBuffer[iCurLoc + i] = bytes[i];
                }

                bytes = null;

                // 修改当前指针的位置
                iCurLoc += int_Len;
            }
            catch
            {
                if (timer.Enabled == false)
                {
                    timer.Enabled = true;
                }
            }
            finally
            {
                //if (timer.Enabled == false)
                //{
                //    timer.Enabled = true;
                //}
            }
        }
Esempio n. 2
0
        /// <summary>
        /// TCP/IP客户端接收数据
        /// </summary>
        /// <param name="bytes">接收到字符串</param>
        /// <param name="strAddress">发送数据的远程主机的IP地址</param>
        public virtual void baseSocketClient_DataReceivedByAddress(byte[] bytes, string strAddress)
        {
            if (timer.Enabled == true)
            {
                timer.Enabled = false;
            }
            try
            {
                // 接收缓冲区中数据的字节数
                int int_Len = bytes.Length;
                if (RTxtMsgo != null)
                {
                    RTxtMsgo.WriteTxt(bytes, "RX", true, 0);
                }

                // 缓冲区溢出
                if (iCurLoc + int_Len > iBufferSize)
                {
                    iCurLoc = 0;
                    return;
                }

                // 将数据存入缓冲区
                for (int i = 0; i < int_Len; i++)
                {
                    byteBuffer[iCurLoc + i] = bytes[i];
                }

                bytes = null;

                // 修改当前指针的位置
                iCurLoc += int_Len;
            }
            catch
            {
                if (timer.Enabled == false)
                {
                    timer.Enabled = true;
                }
            }
            finally
            {
            }
        }