Discussion:
pInvoke EscapeCommFunction problem
(too old to reply)
David Hall
2003-10-29 19:08:52 UTC
Permalink
Does anyone have any idea why I might get a System.NotSupportedException
when I pInvoke EscapeCommFunction, regardless of the flags I use in the
call?

I can successfully pInvoke ClearCommBreak and SetCommBreak to get the same
results as I would expect to get by calling EscapeCommFunction with SETBREAK
or CLRBREAK, but any call to EscapeCommFunction throws an exception. I need
to be able to set and clear DTR and RTS on a COM Port, and
EscapeCommFunction is the only way I see to do it.

By the way, before calling the code below, I have previously set fDtrControl
and fRtsControl to Disabled by pInvoking SetCommState. This shouldn't affect
the ability to SETBREAK or CLRBREAK, but it could affect the use of the
other EscapeFunctions.

Here's some of the C# code for my CE .NET 4.1 device...

internal enum EscapeFunctions : ulong
{
SETXOFF = 1, // Simulate XOFF received
SETXON = 2, // Simulate XON received
SETRTS = 3, // Set RTS high
CLRRTS = 4, // Set RTS low
SETDTR = 5, // Set DTR high
CLRDTR = 6, // Set DTR low
// NT supports RESETDEV, not supported on CE
// RESETDEV = 7, // Reset device if possible
SETBREAK = 8, // Set the device break line.
CLRBREAK = 9, // Clear the device break line.
SETIR = 10, // Set the port to IR mode.
CLRIR = 11 // Set the port to non-IR mode.
}

[DllImport("coredll.dll", EntryPoint="EscapeCommFunction", SetLastError =
true)]
internal static extern bool EscapeCommFunction(IntPtr hFile,
EscapeFunctions escapeFunctions);

[DllImport("coredll.dll", EntryPoint="ClearCommBreak", SetLastError =
true)]
internal static extern bool ClearCommBreak(IntPtr hFile);

[DllImport("coredll.dll", EntryPoint="SetCommBreak", SetLastError = true)]
internal static extern bool SetCommBreak(IntPtr hFile);

public void TestBreak(IntPtr hFile)
{
// This works fine
SetCommBreak(hFile);
Thread.Sleep(500);
ClearCommBreak(hFile);
Thread.Sleep(500);

// This throws a System.NotSupportedException
// regardless of the value of the second parameter
EscapeCommFunction(hFile, EscapeFunctions.SETBREAK);
Thread.Sleep(500);
EscapeCommFunction(hFile, EscapeFunctions.CLRBREAK);
Thread.Sleep(500);
}

Thanks,
Dave
David Hall
2003-10-29 21:56:20 UTC
Permalink
I discovered the problem. Just need to make one minor change.

enum EscapeFunctions : ulong
... needs to be...
enum EscapeFunctions : uint


Unlike C++ which defines the size of uint as system dependent, but ulong as
32 bits, C# defines uint as 32 bits and ulong as 64 bits.

All works fine now...
Dave

Loading...