User`s manual

ThinkCore DA-660 WinCE User’s Manual Programming Examples
5-7
ref byte lpInBuffer,
int nInBufferSize,
ref byte lpOutBuffer,
int nOutBufferSize,
ref int lpBytesReturned,
IntPtr lpOverlapped
);
const uint MOXA_SET_OP_MODE = (0x400 + 66);
const uint MOXA_GET_OP_MODE = (0x400 + 67);
const uint RS232_MODE = 0;
const uint RS485_2WIRE_MODE = 1;
const uint RS422_MODE = 2;
const uint RS485_4WIRE_MODE = 3;
private readonly static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
private const UInt32 OPEN_EXISTING = 3;
private const UInt32 GENERIC_READ = 0x80000000;
private const UInt32 GENERIC_WRITE = 0x40000000;
String sPort;
public MxComPortConfig(String sPort)
{
this.sPort = sPort;
}
private bool SetComPortInterface(uint mode)
{
IntPtr comPort = CECreateFileW(sPort, GENERIC_READ, 0, IntPtr.Zero,
OPEN_EXISTING, 0, IntPtr.Zero);
if (comPort == INVALID_HANDLE_VALUE)
return false;
int nBytesReturned = 0;
byte bIn = (byte)mode;
byte bOut = 0;
DeviceIoControl(comPort, MOXA_SET_OP_MODE, ref bIn, 1, ref bOut, 0, ref
nBytesReturned, IntPtr.Zero);
CECloseHandle(comPort);
return true;
}
public bool SetRS232()
{
return SetComPortInterface(RS232_MODE);
}
public bool SetRS422()