Funktion gibt Fehler aus, aber Fehlermeldung sagt, es ist alles erfolgreich.



  • Hallo zusammen,

    ich habe hier folgende Funktion:

    #define DEV_ONLINE 1
    #define DEV_NO_ERROR 0
    #define DEV_ERROR_INVALID_HANDLE -1
    #define DEV_ERROR_NO_COMM_STATE -2
    #define DEV_ERROR_TIMEOUTS -3
    #define DEV_ERROR_WRITE_FILE -4
    #define DEV_ERROR_READ_FILE -5
    #define DEV_ERROR_FILE_NOT_FOUND -6
    
    // ...
    
    int APP_IsDeviceOnNonGUI(char *comPort)
    {
    	//handle of com port
    	HANDLE hSerial;
    	DCB dcbSerial = {0};
    	DCB dcbSerialParams = {0};
    	COMMTIMEOUTS timeouts = {0};
    	// number of bites which are going to be send
    	//to the com port
    	const int nWrite = 6;
    	//number of bites which are going to be read
    	//to the com port
    	const int nRead = 30;
    	DWORD dwBytesWrite = 0;
    	//saving the received data
    	char szBuffRead[100];
    	DWORD dwBytesRead = 0;
    //	char lastError[1024];
    	int i;
    
    	//create handle for serial port
    	hSerial = CreateFile(comPort, GENERIC_READ | GENERIC_WRITE,
    		0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    
    	if (hSerial == INVALID_HANDLE_VALUE)
    	{
    		if (GetLastError() == ERROR_FILE_NOT_FOUND)
    		{
    			//serial port does not exist. Inform user
    			return DEV_ERROR_FILE_NOT_FOUND;
    		}
    
    		//some other error occured. Inform user
    		return DEV_ERROR_INVALID_HANDLE;
    	}
    
    	dcbSerial.DCBlength = sizeof(dcbSerialParams);
    
    	if (!GetCommState(hSerial, &dcbSerialParams))
    	{
    		//error getting state
    		return DEV_ERROR_NO_COMM_STATE;
    	}
    
    	dcbSerialParams.BaudRate = BAUDRATE;
    	dcbSerialParams.ByteSize = 8;
    	dcbSerialParams.StopBits = 0;
    	dcbSerialParams.Parity = 1;
    
    	if(!SetCommState(hSerial, &dcbSerialParams))
    	{
    		//error setting serial port state
    		return DEV_ERROR_NO_COMM_STATE;
    	}
    
    	timeouts.ReadIntervalTimeout = 1;
    	timeouts.ReadTotalTimeoutConstant = 1;
    	timeouts.ReadTotalTimeoutMultiplier = 1;
    	timeouts.WriteTotalTimeoutConstant = 1;
    	timeouts.WriteTotalTimeoutMultiplier = 1;
    
    	if (!SetCommTimeouts(hSerial, &timeouts))
    	{
    		return DEV_ERROR_TIMEOUTS;
    	}
    
    	for (i = 0; i < 2; i++)
    	{
    		OVERLAPPED overlapped_structure;
    		memset(&overlapped_structure, 0, sizeof(overlapped_structure));
    		overlapped_structure.Offset = 4096;
    		overlapped_structure.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    
    		if (!WriteFile(hSerial, "TEST\r\n", nWrite + 1, &dwBytesWrite, NULL))
    		{
    			//error occurred. Report to user
    			return DEV_ERROR_WRITE_FILE;
    		}
    
    		//receive some bytes
    		if (!ReadFile(hSerial, szBuffRead, nRead, &dwBytesRead, NULL))
    		{
    			//error occurred. Report to user
    			return DEV_ERROR_READ_FILE;
    		}
    
    //tests, if received data has device name
    		if (_hasDeviceString(szBuffRead) == TRUE)
    		{
    			return DEV_ONLINE;
    		}
    		else
    		{
    			return DEV_NO_ERROR;
    		}
    	}
    
    	CloseHandle(hSerial);
    	return DEV_NO_ERROR;
    }
    

    Diese Funktion rufe ich in einem Thread immer wieder auf (solange das Programm an ist). Über eine globale Variable lasse ich mir dann den Rückgabewert anzeigen.

    Dort steht immer -1. Jedoch wenn ich mit GetLastError() den Fehler finden möchte, sagt er mir jedoch, dass der Vorgang erfolgreich abgeschlossen ist.

    Hat jemand ne Ahnung wieso das so ist ? 😕



  • Wo wird der Wert der globalen Variable gesetzt?


Log in to reply