ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Понедельник
22 июля
1156628 Топик полностью
йцyкeн (20.12.2021 11:50, просмотров: 190) ответил misyachniy на Решено "Не удалось получить дескриптор порта" по Win 64 bit.
Сначала неплохо бы знать, что такое дескриптор COM порта. Я открываю COM порт как файл, получаю на руки file handle. 
	HANDLE ComPort = CreateFile("COM3",
		GENERIC_READ | GENERIC_WRITE,
		0,    /* comm devices must be opened w/exclusive-access */
		NULL, /* no security attrs */
		OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */
		0,    /* not overlapped I/O */
		NULL  /* hTemplate must be NULL for comm devices */
	);

	if (ComPort == INVALID_HANDLE_VALUE) 
	{
		CloseHandle( ComPort );
		throw "Error opening COM port";
	}

	DCB dcb;
	BOOL fSuccess;

	// Get the current configuration.
	fSuccess = GetCommState(ComPort, &dcb);
	if (!fSuccess) 
	{
		CloseHandle( ComPort );
		throw "Error reading Device Control Block";
	}

	dcb.BaudRate = CBR_115200;
	dcb.ByteSize = 8;
	dcb.Parity = NOPARITY;
	dcb.StopBits = ONESTOPBIT;
	dcb.fDtrControl = DTR_CONTROL_DISABLE;
	dcb.fRtsControl = RTS_CONTROL_DISABLE;

	fSuccess = SetCommState(ComPort, &dcb);

	if (!fSuccess) 
	{
		CloseHandle( ComPort );
		throw "Error writing Device Control Block";
	}

	if( !SetupComm( ComPort, 0x2000 /*size of input buffer*/, 0x2000 /*size of output buffer*/ ) )
	{
		CloseHandle( ComPort );
		throw "Error with SetupCom";
	}
	//Set Timeouts
	COMMTIMEOUTS cto;
	GetCommTimeouts( ComPort, &cto ); 
	cto.ReadTotalTimeoutConstant = 1;
	SetCommTimeouts( ComPort, &cto );

	if( !PurgeComm( ComPort, PURGE_TXCLEAR | PURGE_RXCLEAR ) )
	{
		CloseHandle( ComPort );
		throw "Error with PurgeCom";
	}