Menu

Simultaneously ReadFile and WriteFile unavailable

Help
Dmitry Gri
2022-05-17
2022-05-18
  • Dmitry Gri

    Dmitry Gri - 2022-05-17

    Hi all, a found a possible bug in v3.0.0.0 version of com0com.
    I was created a pair COM59 COM60/ Open COM60 in Putty terminal, and COM60 in my test program:

    #include "stdafx.h"
    #include <Windows.h>
    
    
    DWORD WINAPI ThFu(void *p)
    {
    HANDLE h = (HANDLE)p;
    DWORD dw;
    char buf[2];
        buf[0]='z';
        while(1)
        {
            Sleep(2000);
            WriteFile(h,buf,1,&dw,NULL);
            printf("w\n");
        }
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    HANDLE hf[3];
    COMMTIMEOUTS ts;
    DWORD dw;
    COMMCONFIG cc;
        char buf[128];
        buf[0]='r';
        hf[2]= CreateFile("\\\\.\\COM59",GENERIC_READ|GENERIC_WRITE 
            ,0,NULL,OPEN_EXISTING,0,NULL);
        if(hf[2]==INVALID_HANDLE_VALUE){
            printf("cant open port!\n");
            return -1;
        }
    
        dw = sizeof(cc);
        memset(&cc,0,sizeof(cc));
        cc.dwSize = sizeof(cc);
        cc.dcb.DCBlength = sizeof(DCB);
        GetCommConfig(hf[2],&cc,&dw);
        dw = sizeof(cc);
        cc.dcb.fDtrControl = 0;
        cc.dcb.fDsrSensitivity = 0;
        cc.dcb.fOutxCtsFlow = 0;
        cc.dcb.fOutxDsrFlow = 0;
        cc.dcb.fRtsControl = 0;
        SetCommConfig(hf[2],&cc,dw);
    COMMTIMEOUTS tm;
        GetCommTimeouts(hf[2],&tm);
        tm.ReadIntervalTimeout = MAXDWORD;
        tm.ReadTotalTimeoutMultiplier = MAXDWORD;
        tm.ReadTotalTimeoutConstant = MAXDWORD - 1;
        tm.ReadIntervalTimeout = MAXDWORD;
        tm.WriteTotalTimeoutMultiplier = 0;
        tm.WriteTotalTimeoutConstant = 0;
        SetCommTimeouts(hf[2],&tm);
        printf("run\n");
        CreateThread(NULL,0,&ThFu,hf[2],0,&dw);
    
        while(1){
            ReadFile(hf[2],buf,1,&dw,NULL);
            printf("readed=%d\n", dw);
        }
        return 0;
    }
    

    I expected to see 'z' every 2 seconds in Putty, but nothing happens. WriteFile succeded only after input in COM60 terminal (something rad on COM59). So, WriteFile blocked forever while ReadFile blocked on read. In asynchronous OVERLAPPED logic (FILE_FLAG_OVERLAPPED in CreateFile + overlapped in read write) all working just fine.
    Whats wrong? How to fix?

     
  • Dmitry Gri

    Dmitry Gri - 2022-05-18

    I\m try to enable "emulate baudrate" an enable "buffer overrun", bot no luck

     

Log in to post a comment.