DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Siva Guest

    COM Port Reading


    I have a problem in reading the COM1 port.
    I opening the COM port by using CreateFile() fn.
    Baud rate is set by using SetCommState() fn.
    SetCommMask() is used for mask.
    After this when i call ReadFile() fn, the program hangs in ReadFile() fn.
    Anyone can give a skeleton code or reason for this.

    Thanks,
    Siva


  2. #2
    oli Guest

    Re: COM Port Reading

    hComm = CreateFile((const char*) "COM1",GENERIC_READ |
    GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);

    if(hComm== INVALID_HANDLE_VALUE)
    {
    .... errrorr arrrrggsss,...
    }

    if(!GetCommState(hComm,&dcb))
    {
    .... errrorr arrrrggsss,...
    }

    dcb.BaudRate = CBR_600; //hi,hi,hi
    dcb.ByteSize =(BYTE)bytes;//number of bits/byte, 4-8
    dcb.StopBits =(BYTE)stopbit;// 0,1,2 = 1, 1.5, 2
    dcb.Parity =(BYTE)parity;// 0-4=no,odd,even,mark,space

    /* set state */
    if(!SetCommState(hComm,&dcb))
    {
    .... errrorr arrrrggsss,...
    }
    /*write*/
    if (!WriteFile(hComm, data, strlen(data), &dwWritten, NULL))
    {
    .... errrorr arrrrggsss,...
    }

    /*read*/
    if (!WriteFile(hComm, datatoread, dwNumberToRead, &dwread, NULL))
    {
    .... errrorr arrrrggsss,...
    }

    CloseHandle( hComm);
    /*I have not try but...*/
    regards,
    evaleto@gkb.com

    Siva <siva1010@hotmail.com> wrote in message news:38cdcd69@news.devx.com...
    >
    > I have a problem in reading the COM1 port.
    > I opening the COM port by using CreateFile() fn.
    > Baud rate is set by using SetCommState() fn.
    > SetCommMask() is used for mask.
    > After this when i call ReadFile() fn, the program hangs in ReadFile() fn.
    > Anyone can give a skeleton code or reason for this.
    >
    > Thanks,
    > Siva
    >




  3. #3
    Tarek Eslim Guest

    Re: COM Port Reading


    How you assign the file handle to the COM1 itself, I checked your code written,
    you just
    state "COM1" in the filename parameter in the CreateFile fun() ?

    Could you please explain how to distinguish between other ports also? I want
    to open
    handles for COM1 & COM2 and LPT at the same time.

    Best Regards

    Tarek


    "oli" <evaleto@gkb.com> wrote:
    > hComm = CreateFile((const char*) "COM1",GENERIC_READ |
    >GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
    >
    > if(hComm== INVALID_HANDLE_VALUE)
    >{
    >.... errrorr arrrrggsss,...
    >}
    >
    > if(!GetCommState(hComm,&dcb))
    > {
    >.... errrorr arrrrggsss,...
    > }
    >
    > dcb.BaudRate = CBR_600; //hi,hi,hi
    > dcb.ByteSize =(BYTE)bytes;//number of bits/byte, 4-8
    > dcb.StopBits =(BYTE)stopbit;// 0,1,2 = 1, 1.5, 2
    > dcb.Parity =(BYTE)parity;// 0-4=no,odd,even,mark,space
    >
    > /* set state */
    > if(!SetCommState(hComm,&dcb))
    > {
    >.... errrorr arrrrggsss,...
    > }
    >/*write*/
    > if (!WriteFile(hComm, data, strlen(data), &dwWritten, NULL))
    >{
    >.... errrorr arrrrggsss,...
    >}
    >
    >/*read*/
    > if (!WriteFile(hComm, datatoread, dwNumberToRead, &dwread, NULL))
    >{
    >.... errrorr arrrrggsss,...
    >}
    >
    > CloseHandle( hComm);
    >/*I have not try but...*/
    >regards,
    >evaleto@gkb.com
    >
    >Siva <siva1010@hotmail.com> wrote in message news:38cdcd69@news.devx.com...
    >>
    >> I have a problem in reading the COM1 port.
    >> I opening the COM port by using CreateFile() fn.
    >> Baud rate is set by using SetCommState() fn.
    >> SetCommMask() is used for mask.
    >> After this when i call ReadFile() fn, the program hangs in ReadFile()

    fn.
    >> Anyone can give a skeleton code or reason for this.
    >>
    >> Thanks,
    >> Siva
    >>

    >
    >



  4. #4
    Boris Karadjov Guest

    Re: COM Port Reading

    Use SetCommTimeouts to instruct Windows how to recoginze a pause in the
    serial communication and return the control to your application before
    filling the entire buffer passed to ReadFile function. See
    http://msdn.microsoft.com/library/en.../time_outs.asp
    --
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com


    "Siva" <siva1010@hotmail.com> wrote in message
    news:38cdcd69@news.devx.com...
    >
    > I have a problem in reading the COM1 port.
    > I opening the COM port by using CreateFile() fn.
    > Baud rate is set by using SetCommState() fn.
    > SetCommMask() is used for mask.
    > After this when i call ReadFile() fn, the program hangs in ReadFile() fn.
    > Anyone can give a skeleton code or reason for this.
    >
    > Thanks,
    > Siva
    >




Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links