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 = Com_WinApi.CreateFile(portName, (UInt32)(dwDesiredAccess.GENERIC_READ | dwDesiredAccess.GENERIC_WRITE), 0, IntPtr.Zero, (UInt32)dwCreationDisposion.OPEN_EXISTING, (UInt32)dwFileFlags.FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (handle == (IntPtr)Com_WinApi.INVALID_HANDLE_VALUE) return false; DCB _dcb = new DCB(); Com_WinApi.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 (!Com_WinApi.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(); Com_WinApi.GetCommTimeouts(handle, out _tOut); //Setup timeouts _tOut.ReadIntervalTimeout = ReadIntervalTimeout; _tOut.ReadTotalTimeoutConstant = ReadTotalTimeoutConstant; _tOut.ReadTotalTimeoutMultiplier = ReadTotalTimeoutMultiplier; _tOut.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant; _tOut.WriteTotalTimeoutMultiplier = WriteTotalTimeoutMultiplier; if (!Com_WinApi.SetCommTimeouts(handle, ref _tOut)) { System.Windows.Forms.MessageBox.Show("����� ���������� �������� �����", "COM ����", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return false; } Com_WinApi.PurgeComm(handle, Com_WinApi.PURGE_TXCLEAR); Com_WinApi.PurgeComm(handle, Com_WinApi.PURGE_RXCLEAR); isOpen = true; return true; }
public static extern Boolean BuildCommDCBAndTimeouts(String lpDef, ref DCB lpDCB, ref COMMTIMEOUTS lpCommTimeouts);