C serial port programming in windows using Visual C++6.0

I am hoping someone can help me out.

I am writing in C and using visual c++ 6.0 and
I need to write a program to open a serial port "COM1", configure it , read from it and write to it.

I also do not know the header files to include in my main program to get the code to work. Does any one have the solution?

Thanks
Kay

Comments

  • : I am hoping someone can help me out.
    :
    : I am writing in C and using visual c++ 6.0 and
    : I need to write a program to open a serial port "COM1", configure it , read from it and write to it.
    :
    : I also do not know the header files to include in my main program to get the code to work. Does any one have the solution?
    :
    : Thanks
    : Kay
    :
    [blue]This one is a good start:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/communications_resources.asp[/blue]
  • /*
    asmguru, the link you sen me is bad. anyhow here is my code which i wrote. i am getting error with read operation. I connected a null modem to the serial port to read the string i wrote to the serial port but nothing is read on serial port. The program copiles, builds and executes but i get error reading.
    can you help !!!
    */

    #include
    #include
    #include
    #include
    #include
    //#include

    int nread,nwrite;


    void main()
    {

    HANDLE hSerial;
    COMMTIMEOUTS timeouts;
    COMMCONFIG dcbSerialParams;
    char *words, *buffRead, *buffWrite;
    DWORD dwBytesWritten, dwBytesRead;



    hSerial = CreateFile("COM1",
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,//FILE_FLAG_OVERLAPPED, // FILE_ATTRIBUTE_NORMAL
    NULL);

    if ( hSerial == INVALID_HANDLE_VALUE)
    {
    if (GetLastError() == ERROR_FILE_NOT_FOUND)
    {
    printf(" serial port does not exist
    ");
    }
    printf(" some other error occured. Inform user.
    ");
    }


    //DCB dcbSerialParams ;
    //GetCommState( hSerial, &dcbSerialParams.dcb);
    if (!GetCommState(hSerial, &dcbSerialParams.dcb))
    {
    printf("error getting state
    ");
    }

    dcbSerialParams.dcb.DCBlength = sizeof(dcbSerialParams.dcb);


    dcbSerialParams.dcb.BaudRate = CBR_38400;
    dcbSerialParams.dcb.ByteSize = 8;
    dcbSerialParams.dcb.StopBits = TWOSTOPBITS;
    dcbSerialParams.dcb.Parity = NOPARITY;

    dcbSerialParams.dcb.fBinary = TRUE;
    dcbSerialParams.dcb.fDtrControl = DTR_CONTROL_DISABLE;
    dcbSerialParams.dcb.fRtsControl = RTS_CONTROL_DISABLE;
    dcbSerialParams.dcb.fOutxCtsFlow = FALSE;
    dcbSerialParams.dcb.fOutxDsrFlow = FALSE;
    dcbSerialParams.dcb.fDsrSensitivity= FALSE;
    dcbSerialParams.dcb.fAbortOnError = TRUE;

    if (!SetCommState(hSerial, &dcbSerialParams.dcb))
    {
    printf(" error setting serial port state
    ");
    }


    GetCommTimeouts(hSerial,&timeouts);
    //COMMTIMEOUTS timeouts = {0};

    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier= 10;

    if(!SetCommTimeouts(hSerial, &timeouts))
    {
    printf("error setting port state
    ");
    }





    //****************Write Operation*********************//
    words = "This is a string to be written to serial port COM1";
    nwrite = strlen(words);

    buffWrite = words;
    dwBytesWritten = 0;

    if (!WriteFile(hSerial, buffWrite, nwrite, &dwBytesWritten, NULL))
    {
    printf("error writing to output buffer
    ");
    }
    //printf("Data written to write buffer is
    %s
    ",buffWrite);



    //***************Read Operation******************//
    buffRead = 0;
    dwBytesRead = 0;
    nread = strlen(words);

    if (!ReadFile(hSerial, buffRead, nread, &dwBytesRead, NULL))
    {
    printf("error reading from input buffer
    ");
    }
    printf("Data read from read buffer is
    %s
    ",buffRead);


    CloseHandle(hSerial);

    }


  • I commented out "buffRead = 0;"
    //buffRead = 0;
    and it worked.
  • mliangmliang San Jose, CA

    Key_Kay, I would like to try our your code. What are the include header file? Did you get it work? Thanks!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories