PORTS Forum

Ports and Interfaces => Serial Ports => Topic started by: Joe0x7F on February 08, 2012, 07:48:05 pm

Title: DeviceIoControl(IOCTL_SERIAL_PURGE) Invalid Handle
Post by: Joe0x7F on February 08, 2012, 07:48:05 pm
Any ideas why DeviceIoControl() would be failing and GetLastError() returning 0x06 = Invalid Handel?

I was writing to the Serial (COMx:) Port fine just before the DeviceIoControl() call.

....
#include <windows.h>
#include <winioctl.h>
.....
#define IOCTL_SERIAL_PURGE   CTL_CODE(FILE_DEVICE_SERIAL_PORT,19,METHOD_BUFFERED,FILE_ANY_ACCESS)
...
FILE *pFile;
DWORD flag1=PURGE_TXCLEAR;
DWORD nOutBufferSize=256;
DWORD BytesReturned=0;
struct _OVERLAPPED O1;
char OutBuffer[256]="";
....
pFile = CreateFile("COM4:",
                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      NULL,
                      OPEN_EXISTING,
                      0,
                      NULL);
....
flag1=PURGE_TXCLEAR;
dwError=DeviceIoControl(pFile, IOCTL_SERIAL_PURGE, &flag1, sizeof(flag1), &OutBuffer, nOutBufferSize, &BytesReturned, &O1);




Thanks!

-----------
Title: Re: DeviceIoControl(IOCTL_SERIAL_PURGE) Invalid Handle
Post by: Jan Axelson on February 08, 2012, 11:39:29 pm
Did you try PurgeComm:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363428%28v=vs.85%29.aspx'

Jan
Title: Re: DeviceIoControl(IOCTL_SERIAL_PURGE) Invalid Handle
Post by: Joe0x7F on February 09, 2012, 11:04:55 am
Thanks Jan!!!

Works like a champ!

-----