/// <summary> /// Sets the timeouts for the serial port communication. /// </summary> /// <param name="timeouts">Timeouts to be set.</param> public void SetTimeouts(SerialPortTimeouts timeouts) { if (!Win32Api.SetCommTimeouts(m_fileHandle, ref timeouts)) { throw new Win32Exception(Win32Api.GetLastError(), "'SetCommTimeouts()' failed"); } }
/// <summary> /// Sets the state (config parameters) on the serial port. /// </summary> /// <param name="state">The configuation to be applied to the serial port.</param> public void SetState(SerialPortState state) { if (!IsOpen) { throw new InvalidOperationException("Port is not open."); } if (!Win32Api.SetCommState(m_fileHandle, ref state)) { throw new Win32Exception(Win32Api.GetLastError(), "'SetCommState()' failed"); } }
/// <summary> /// Gets the timeouts currently set for the serial port communication. /// </summary> /// <returns>The timeouts currently set.</returns> public SerialPortTimeouts GetTimeouts() { if (!IsOpen) { throw new InvalidOperationException("Port is not open."); } SerialPortTimeouts commTimeouts = new SerialPortTimeouts(); if (!Win32Api.GetCommTimeouts(m_fileHandle, ref commTimeouts)) { throw new Win32Exception(Win32Api.GetLastError(), "'GetCommTimeouts()' failed"); } return(commTimeouts); }
/// <summary> /// Writes data to the serial port. /// </summary> /// <param name="data">The data to be written.</param> /// <param name="offset">(Not used)</param> /// <param name="count">Number of bytes from the data array to be written into the port.</param> /// <returns>Number of bytes actually written.</returns> public int Write(byte[] data, int offset, int count) { if (!IsOpen) { throw new InvalidOperationException("Port is not open."); } uint bytesWritten = 0; if (!Win32Api.WriteFile(m_fileHandle, data, (uint)count, out bytesWritten, IntPtr.Zero)) { throw new Win32Exception(Win32Api.GetLastError(), "'WriteFile()' failed"); } return((int)bytesWritten); }
/// <summary> /// Gets the current state (config parameters) set on the serial port. /// </summary> /// <returns>The current serial port state.</returns> public SerialPortState GetState() { if (!IsOpen) { throw new InvalidOperationException("Port is not open."); } SerialPortState dcb = new SerialPortState(); if (!Win32Api.GetCommState(m_fileHandle, ref dcb)) { throw new Win32Exception(Win32Api.GetLastError(), "'GetCommState()' failed"); } return(dcb); }