public static extern Boolean GetCommTimeouts(IntPtr hFile, out COMMTIMEOUTS lpCommTimeouts);
/// <summary> /// ³������� ���-����� /// </summary> /// <param name="portIndex">����� �����</param> /// <returns>���� true �� ���-���� �������� ������</returns> public bool Open(int portIndex) { string portName = "COM" + portIndex.ToString() + ":"; handle = API.CreateFile(portName, (UInt32)(winapi.dwDesiredAccess.GENERIC_READ | winapi.dwDesiredAccess.GENERIC_WRITE), 0, IntPtr.Zero, (UInt32)winapi.dwCreationDisposion.OPEN_EXISTING, (UInt32)winapi.dwFileFlags.FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (handle == (IntPtr)API.INVALID_HANDLE_VALUE) return false; DCB _dcb = new DCB(); API.GetCommState(handle, ref _dcb); //Setup dcb _dcb.BaudRate = (int)this.baudRate; //Clear RtsControl _dcb.Flags &= 0x7FFFCFFF; //Clear DsrControl _dcb.Flags &= 0x7FFFFFCF; //Set fBinary to 1 _dcb.Flags |= 0x00000001; //Set fParity to 1 _dcb.Flags |= 0x00000002; _dcb.ByteSize = (byte)this.dataBits; _dcb.Parity = (byte)this.parity; _dcb.StopBits = (byte)this.stopBits; //Handflow _dcb.XonLim = 2048; _dcb.XoffLim = 512; _dcb.XonChar = (char)0x11; _dcb.XoffChar = (char)0x13; if (!API.SetCommState(handle, ref _dcb)) { System.Windows.Forms.MessageBox.Show("����� ���������� ��������� �����", "COM ����", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return false; } COMMTIMEOUTS _tOut = new COMMTIMEOUTS(); API.GetCommTimeouts(handle, out _tOut); //Setup timeouts _tOut.ReadIntervalTimeout = ReadIntervalTimeout; _tOut.ReadTotalTimeoutConstant = ReadTotalTimeoutConstant; _tOut.ReadTotalTimeoutMultiplier = ReadTotalTimeoutMultiplier; _tOut.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant; _tOut.WriteTotalTimeoutMultiplier = WriteTotalTimeoutMultiplier; if (!API.SetCommTimeouts(handle, ref _tOut)) { System.Windows.Forms.MessageBox.Show("����� ���������� �������� �����", "COM ����", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return false; } API.PurgeComm(handle, API.PURGE_TXCLEAR); API.PurgeComm(handle, API.PURGE_RXCLEAR); isOpen = true; return true; }
public static extern Boolean BuildCommDCBAndTimeouts(String lpDef, ref DCB lpDCB, ref COMMTIMEOUTS lpCommTimeouts);