ansprechen der com1 geht nicht mit writefile????!!!!



  • Hallo,

    ich habe folgendes Problem....
    ich habe einen µC (Atmega8), dem ich ein Byte senden will, wenn er dies hat sendet er mir 4 Byte, das funktioniert.
    Ich habe jetzt folgendes Visual c++6.0 Programm geschrieben und die Intitialisierung meckert auch nicht...
    wenn ich jetzt aber schreiben will "writefile(...)" bringt er mir fehler
    "6 The handle is invalid. ERROR_INVALID_HANDLE "
    ich weiss nicht warum

    bitte helft mir...

    Danke Lutz

    #include <iostream.h>
    #include <windows.h>
    
    void main (){
    char weiter;
    HANDLE hCOM=CreateFile("COM1",GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,NULL); 
    if (!SetupComm(hCOM,512,512)) cout << "Error Open" << endl; 
    DCB dcb; //Struktur zum einstellen der Schnittstelleneigenschaften
    ZeroMemory(&dcb,sizeof(DCB)); 
    char buffer[100]; 
    strcpy(buffer,"baud=1200 parity=N data=8 stop=1"); 
    if (BuildCommDCB((char*)&buffer,&dcb)) 
    { 
    if (dcb.BaudRate == CBR_1200) cout << "Yes " << endl; 
    } 
    else cout << " error config DCB"; 
    
    if(GetCommState(hCOM,&dcb)) 
    { 
    dcb.BaudRate = CBR_19200; 
    dcb.ByteSize = 8; 
    dcb.Parity = 0; 
    dcb.StopBits = 0; 
    SetCommState(hCOM,&dcb);
    }
    /*
    COMMTIMEOUTS timeouts; 
    timeouts.ReadIntervalTimeout = 20000; 
    timeouts.ReadTotalTimeoutMultiplier = 1000;
    timeouts.ReadTotalTimeoutConstant = 1000;
    timeouts.WriteTotalTimeoutMultiplier = 1000;
    timeouts.WriteTotalTimeoutConstant = 100;
    if (!SetCommTimeouts(hCOM, &timeouts)) 
    { cout << "Error setting time-outs";
      cin >> weiter;}
    */
    
    if (hCOM!=INVALID_HANDLE_VALUE) 
    { 
    int i;
    for ( i = 1; i < 10; i++ ) 
    {
    DWORD numbytes, numbytes_ok, temp; 
    COMSTAT ComState; 
    OVERLAPPED Overlap;
    char buf_in[20];
    char buf_out[2]="s";
    numbytes = 20; 
    
    ClearCommError(hCOM, &temp, &ComState); // if temp is not null, the port is in the error state 
    if (!temp) WriteFile(hCOM,&buf_out,1,&numbytes_ok,&Overlap);
    //cout << numbytes_ok; 
    /*
    ClearCommError(hCOM, &temp, &ComState); 
    if(!temp) ReadFile(hCOM, buf_in, numbytes, &numbytes_ok, &Overlap); //the numbytes_ok variable contains the actual number of I/O bytes 
                        // handle to file, pipe, or comm device
    */
    DWORD error;
    error=GetLastError();
    cout << error;
    //cout << numbytes_ok; 
    //cout << buf_in;
    
    cout << "funzt";
    }
    
    CloseHandle(hCOM); 
    cin >> weiter;
    
    } 
    
    else {
    cout << "Error Open";
    cin >> weiter;}
    }
    


  • Man darf also davon ausgehen, daß hCom != INVALID_HANDLE_VALUE ist, ja? Es fallen mir aber trotzdem drei Sachen auf:

    • Du initialisierst den DCB nur teilweise. Das geht so nicht. Wenn Du so Sachen wie Handshaking nicht benötigst, mußt Du sie aussschalten.
    • Die OVERLAPPED-Struktur wird nicht initialisiert. Geh da mal mit ZeroMemory drüber (vor dem WriteFile-Aufruf).
    • Der Wert von GetLastError ist nur dann von Bedeutung, wenn WriteFile mit FALSE zurückkommt. Das hast Du gar nicht geprüft. Was liefert also WriteFile?

Anmelden zum Antworten