예제 #1
0
        private void ProcessSend(SocketAsyncEventArgs e)
        {
            ClientToken clientToken = (ClientToken)e.UserToken;

            if (e.SocketError != SocketError.Success)
            {
                if (e.SocketError == SocketError.OperationAborted)
                {
                    return;
                }

                //Fire DataSentError event
                if (DataSendError != null)
                {
                    if (CallDataSendErrorAsync)
                    {
                        DataSendError.BeginInvoke(this, r => DataSendError.EndInvoke(r), null);
                    }
                    else
                    {
                        DataSendError(this);
                    }
                }

                clientToken.ResetSend();
                ProcessDisconnect(e);
            }

            clientToken.SendBytesRemaining = clientToken.SendBytesRemaining - e.BytesTransferred;
            if (clientToken.SendBytesRemaining == 0)
            {
                //Fire DataSent event
                if (DataSent != null)
                {
                    if (CallDataSentAsync)
                    {
                        DataSent.BeginInvoke(this, r => DataSent.EndInvoke(r), null);
                    }
                    else
                    {
                        DataSent(this);
                    }
                }

                clientToken.ResetSend();
            }
            else
            {
                clientToken.BytesSentAlready += e.BytesTransferred;
                StartSend();
            }
        }
예제 #2
0
 private void WriteRaw(SerialPort port, byte[] src, int srcOffset, int count)
 {
     if (!port.IsOpen)
     {
         return;
     }
     try
     {
         port.BaseStream.Write(src, srcOffset, count);
         byte[] sent = new byte[count];
         Buffer.BlockCopy(src, srcOffset, sent, 0, count);
         DataSent?.BeginInvoke(port.PortName, sent, DataSentInvokeCallback, null);
     }
     catch (Exception exception)
     {
         PrintError(exception.ToString());
         Close();
         PrintInfo($"{serial.PortName} closed!");
     }
 }