locked
serial communication read problem RRS feed

  • Question

  • hi all

    i am trying to work around with serial communication in vc 6.0 i tried a program with the help of internet and compiled it successfully, i think the data can be written to the serial communication successfully, but i am unable to read it back.
    the objective of the program is to just write a string like "hello" and just display it back on console. i would like to remind that it is not a homework, just a piece of learning activity. i am posting my code here any advice how to read is greatly appriciated.
    thanking you

    Code Block

    #include "windows.h"
    #include "iostream.h"
    #include "stdio.h"


    int main(int argc, char* argv[])
    {

        HANDLE hcomm;
        //char lpWbuff[]="Echo\r";
        char lpReadbuff[1024];
        OVERLAPPED ovRead,ovWrite;
        DCB dcb;
    //    HANDLE HreadEvent;
        DWORD dwReadBytes,dwWrittenBytes;
        BOOL fSuccess;
        COMMTIMEOUTS cto;

        ovRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
        ovWrite.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
       

        hcomm=CreateFile("\\\\.\\COM2",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);

        if (hcomm==INVALID_HANDLE_VALUE)
        {
            printf("failed to open handle\n");
            return -1;
        }
        printf("Handle created\n");

        memset(&ovRead,0,sizeof(ovRead));
        memset(&ovWrite,0,sizeof(ovWrite));


        fSuccess=(SetupComm(hcomm,1024,1024));

        if (fSuccess=false)
        {
            printf("failed to set comm state");
            return -1;
        }


        fSuccess=BuildCommDCB("baud=9600,parity=N,data=8,stop=1",&dcb);

        if (fSuccess=false)
        {
            printf("failed to build comm state");
            return -1;
        }


        cto.ReadIntervalTimeout =1000;
        cto.ReadTotalTimeoutConstant =1000;
        cto.ReadTotalTimeoutMultiplier=1000;
        cto.WriteTotalTimeoutConstant=1000;
        cto.WriteTotalTimeoutMultiplier=1000;
       
        fSuccess=SetCommTimeouts(hcomm,&cto);

        if (fSuccess=false)
        {
            printf("failed to set comm timeouts");
            return -1;
        }

       
        fSuccess=SetCommState(hcomm,&dcb);

        if (fSuccess=false)
        {
            printf("failed to set comm state");
            return -1;
        }
        printf("set comm state success\n");

        fSuccess =ReadFile(hcomm,lpReadbuff,sizeof(lpReadbuff),&dwReadBytes ,&ovRead);
        fSuccess=WriteFile (hcomm,"hello\r",strlen("hello\r"),&dwWrittenBytes,&ovWrite);

        if (fSuccess=false)
        {
            printf("failed to Write to comm port");
        }

        printf("written to comm port\n");

        /*if (fSuccess=false)
        {
            printf("failed to Write to comm port");
        }
       
            lpReadbuff[dwReadBytes]='\0';
            printf("%s\n",lpReadbuff);*/

        fSuccess=GetOverlappedResult(hcomm,&ovRead,&dwReadBytes,TRUE);
           
            if (fSuccess=true)
            {
                lpReadbuff[dwReadBytes]=0;
                printf("%s\n",lpReadbuff);
            }
            else
            {
                printf("/n failed");
            }
           
       

        CloseHandle(hcomm);


        return 0;
    }





    Tuesday, January 8, 2008 8:04 AM

Answers

  • If you search google you should easily come up with the layout of a serial DB9 connector, but I think it is pin 2 and 3 that you have to connect.

    For DB25 I don't know where they are. could be the same or could be different, but google is your friend.

     

    Anyway, if you connect those pins you should get communication.

     

    Wednesday, January 9, 2008 7:25 AM

All replies

  • Here:

    fSuccess =ReadFile(hcomm,lpReadbuff,sizeof(lpReadbuff),&dwReadBytes ,&ovRead);
        fSuccess=WriteFile (hcomm,"hello\r",strlen("hello\r"),&dwWrittenBytes,&ovWrite);

     

    you ignore the result of the read operation.

    Also, do you get any errors, or does your program simply hang?

    If it hangs: did you connect the TX and the RX pin of your serial port? because if you didn't, then the data you write will never be read.

     

    Tuesday, January 8, 2008 11:31 AM
  • hai

    the result of the read option is success. and i didnt get any errors during compilation. the output is

    Code Block

    Handle Created
    Set comm state success
    read from comm port
    written to comm port

    press any key to continue.


    i dont know about connecting TX and RX pins of serial port, if i am persume correct i have to sort the 9 and  11th pins of the serial port or should i create a null modem to communicate with my own pc.i think i am completely wrong about serial port .
    sorry for any inconvieniece cause.



    Wednesday, January 9, 2008 5:29 AM
  • If you search google you should easily come up with the layout of a serial DB9 connector, but I think it is pin 2 and 3 that you have to connect.

    For DB25 I don't know where they are. could be the same or could be different, but google is your friend.

     

    Anyway, if you connect those pins you should get communication.

     

    Wednesday, January 9, 2008 7:25 AM
  • hai

    will try and get back to the forum

    best regards

    Wednesday, January 9, 2008 7:56 AM
  •  Bruno van Dooren wrote:

    If you search google you should easily come up with the layout of a serial DB9 connector, but I think it is pin 2 and 3 that you have to connect.

    For DB25 I don't know where they are. could be the same or could be different, but google is your friend.

     

    It's pin 2 and 3 on the 25-pin connector as well. 

    Wednesday, January 9, 2008 10:44 PM