Exemple #1
0
        /// <summary>
        /// ³������� ���������� �� ���-����
        /// </summary>
        /// <param name="SendArr">����������</param>
        /// <returns>���� true �� �������� ���������� �� ���-���� �������� ������</returns>
        public bool Write(byte[] SendArr)
        {
            if (handle == (IntPtr)API.INVALID_HANDLE_VALUE) return false;
            if (SendArr == null) return false;
            if (SendArr.Length == 0) return true;

            uint BytesWritten = 0;
            OVERLAPPED OverlappedWrite = new OVERLAPPED();
            bool fOK = false;
            OverlappedWrite.hEvent = API.CreateEvent(IntPtr.Zero, true, false, null);

            if (OverlappedWrite.hEvent != null)
            {
                fOK = API.WriteFile(handle, SendArr, (uint)SendArr.Length, out BytesWritten, ref OverlappedWrite);
                if (!fOK && Marshal.GetLastWin32Error() == API.ERROR_IO_PENDING)
                {
                    fOK = (API.WaitForSingleObject(OverlappedWrite.hEvent, 1000) == API.WAIT_OBJECT_0) &&
                        API.GetOverlappedResult(handle, ref OverlappedWrite, ref BytesWritten, false);
                }
                if (fOK)
                    fOK = (BytesWritten == SendArr.Length);
                if (!fOK)
                    PortClear();
                API.CloseHandle(OverlappedWrite.hEvent);
            }
            else
                Marshal.GetLastWin32Error();

            return fOK;
        }
Exemple #2
0
        /// <summary>
        /// ������� ����� � ���-�����
        /// </summary>
        /// <param name="buffer">����� � ���� ���� ���������� ������� ����������</param>
        /// <param name="count">ʳ������ ���������� �����</param>
        /// <returns>���� true �� ������� � ���-����� �������� ������</returns>
        public bool Read(ref byte[] buffer, out uint count)
        {
            count = 0;
            OVERLAPPED OLRead = new OVERLAPPED();
            bool fOK = false;
            OLRead.hEvent = API.CreateEvent(IntPtr.Zero, true, false, null);

            if (OLRead.hEvent != null)
            {
                fOK = API.ReadFile(handle, buffer, (uint)buffer.Length, out count, ref OLRead);
                if (!fOK)
                {
                    if (Marshal.GetLastWin32Error() == API.ERROR_IO_PENDING)
                    {
                        fOK = (API.WaitForSingleObject(OLRead.hEvent, 5000) == API.WAIT_OBJECT_0) &&
                            API.GetOverlappedResult(handle, ref OLRead, ref count, false);
                    }
                }
                API.CloseHandle(OLRead.hEvent);
                if (!fOK)
                    PortClear();
            }
            else
                Marshal.GetLastWin32Error();

            return fOK;
        }
 public static extern Boolean ReadFile(IntPtr hFile, Byte[] lpBuffer, 
     UInt32 nNumberOfBytesToRead, out UInt32 nNumberOfBytesRead, ref OVERLAPPED lpOverlapped);
 public static extern Boolean GetOverlappedResult(IntPtr hFile, ref OVERLAPPED lpOverlapped, 
     ref UInt32 nNumberOfBytesTransferred, Boolean bWait);
 public static extern Boolean WriteFile(IntPtr fFile, Byte[] lpBuffer, UInt32 nNumberOfBytesToWrite, 
     out UInt32 lpNumberOfBytesWritten, ref OVERLAPPED lpOverlapped);
 public static extern Boolean WaitCommEvent(IntPtr hFile, ref Int32 lpEvtMask, 
     ref OVERLAPPED lpOverlapped);