Example #1
0
        /// <summary>
        /// ³������� ���������� �� ���-����
        /// </summary>
        /// <param name="SendArr">����������</param>
        /// <returns>���� true �� �������� ���������� �� ���-���� �������� ������</returns>
        public bool Write(byte[] SendArr)
        {
            if (handle == (IntPtr)Com_WinApi.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 = Com_WinApi.CreateEvent(IntPtr.Zero, true, false, null);

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

            return fOK;
        }
Example #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 = Com_WinApi.CreateEvent(IntPtr.Zero, true, false, null);

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

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