Skip to content

Commit

Permalink
Provide an implementation for DiscardInBuffer and DiscardOutBuffer
Browse files Browse the repository at this point in the history
The implementations on Linux remained not implemented, throwing an exception. The issue does not affect Windows.

Issue: DOTNET-97, #26
  • Loading branch information
jcurl committed May 7, 2017
1 parent 9a89ea6 commit 891452d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions code/Native/Unix/INativeSerialDll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@ interface INativeSerialDll
int serial_abortwaitforevent(IntPtr handle);
int serial_read(IntPtr handle, IntPtr data, int length);
int serial_write(IntPtr handle, IntPtr data, int length);
int serial_discardinbuffer(IntPtr handle);
int serial_discardoutbuffer(IntPtr handle);
}
}
20 changes: 20 additions & 0 deletions code/Native/Unix/SerialUnix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ private static class UnsafeNativeMethods

[DllImport("libnserial.so.1", SetLastError = true)]
internal static extern int serial_abortwaitformodemevent(IntPtr handle);

[DllImport("libnserial.so.1", SetLastError = true)]
internal static extern int serial_discardinbuffer(IntPtr handle);

[DllImport("libnserial.so.1", SetLastError = true)]
internal static extern int serial_discardoutbuffer(IntPtr handle);
}

[ThreadStatic]
Expand Down Expand Up @@ -558,6 +564,20 @@ public int serial_abortwaitformodemevent(IntPtr handle)
return result;
}

public int serial_discardinbuffer(IntPtr handle)
{
int result = UnsafeNativeMethods.serial_discardinbuffer(handle);
errno = Marshal.GetLastWin32Error();
return result;
}

public int serial_discardoutbuffer(IntPtr handle)
{
int result = UnsafeNativeMethods.serial_discardoutbuffer(handle);
errno = Marshal.GetLastWin32Error();
return result;
}

public SysErrNo netfx_errno(int errno)
{
#if !NETSTANDARD15
Expand Down
4 changes: 2 additions & 2 deletions code/Native/UnixNativeSerial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -607,15 +607,15 @@ public bool IsOpen
/// </summary>
public void DiscardInBuffer()
{
throw new NotImplementedException();
if (m_Dll.serial_discardinbuffer(m_Handle) == -1) ThrowException();
}

/// <summary>
/// Discards the output queue buffer of the driver.
/// </summary>
public void DiscardOutBuffer()
{
throw new NotImplementedException();
if (m_Dll.serial_discardoutbuffer(m_Handle) == -1) ThrowException();
}

/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions test/SerialPortStreamTest/SerialPortStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2117,5 +2117,23 @@ public void ReadUntilDisconnectAndReadAgainThenDispose()
Console.WriteLine("Second Read: {0}", read);
}
}

[Test]
public void DiscardInBuffer()
{
using (SerialPortStream serialSource = new SerialPortStream(c_SourcePort, 115200, 8, Parity.None, StopBits.One)) {
serialSource.Open();
serialSource.DiscardInBuffer();
}
}

[Test]
public void DiscardOutBuffer()
{
using (SerialPortStream serialSource = new SerialPortStream(c_SourcePort, 115200, 8, Parity.None, StopBits.One)) {
serialSource.Open();
serialSource.DiscardOutBuffer();
}
}
}
}

0 comments on commit 891452d

Please sign in to comment.