//**************************************************************************************************
// COM-PORT OPERATIONS (GENERAL I/O):
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#pragma once

namespace XDeviceAccess {

//**************************************************************************************************
/// General communication operations with COM-port (package sending/receiving etc.)
/// <remarks>
/// This sample class uses Win32-API instead of “System.IO.Ports.SerialPort” (from “System.dll”).
/// </remarks>
public ref class ComPortHelpers {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// PUBLIC MEMBERS:
public:

/// <exception cref="InternalInteropException" />
static IntPtr Open(int portNo,int baudRate,int sysInputQueueSize,int sysOutputQueueSize);

/// <exception cref="InternalInteropException" />
static void Close([HOptionalThroughMinusOne][HInterlockedAccess] IntPtr% portHandle);
static bool TryClose( [HOptionalThroughMinusOne][HInterlockedAccess] IntPtr% portHandle,
                      [HOut] Win32Exception^ err );

/// <exception cref="ArgumentNullException" />
/// <exception cref="InternalInteropException" />
static void Send( IntPtr portHandle, IntPtr overlappedStructurePtr,
                  array<byte>^ outputBuffer, int bytesToSend,
                  int timeout );
/// <exception cref="ArgumentNullException" />
/// <exception cref="InternalInteropException" />
static bool Receive( IntPtr portHandle, IntPtr overlappedStructurePtr,
                     array<byte>^ inputBuffer, int bytesToReceive,
                     int timeout );

/// <exception cref="InternalInteropException" />
static IntPtr CreateOverlappedStructure();
/// <exception cref="InternalInteropException" />
static void ReleaseOverlappedStructure( [HOptional][HInterlockedAccess]
                                        IntPtr% overlappedStructurePtr );
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// PRIVATE MEMBERS:
private:

/// <exception cref="Win32Exception" />
static ComPortHelpers();

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}; // ComPortHelpers
//**************************************************************************************************

} // XDeviceAccess
