CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2013
    Posts
    1

    Serial communication in windows - ClearCommError finds no data in read buffer

    I want to send data from a laptop (windows 7, processor 2.60GHz) to a desktop (windows xp, processor 3.10GHz) using serial communication (using a USB to RS232 convertor). The WriteFile function is able to send the data from the laptop (NumberOfBytesWritten is correct). But on the desktop side, ClearCommError detects no data in the read buffer.

    This is the relevant code in my desktop:

    while(1) {
    ClearCommError(hPort,&dwErrors,&commStatus);
    if (commStatus.cbInQue != 0) ReadFile(hPort,&data,1,&dwBytesRead,NULL);
    Sleep(10);
    }

    The if condition is never satisfied. The baudrate and other parameters in the DCB struct are the same on both sides.

    The same code works when I write and read in the same system by shorting the RX and TX pins in the RS232 connector.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Serial communication in windows - ClearCommError finds no data in read buffer

    Have you got the RS232 cable wired correctly at both ends? Do you need straight through or do you need to switch rx/tx? Do you just need 3 wire RS232 or does it need hardware handshaking as well? On the desktop side, open HyperTerminal and make sure you are receiving what the laptop is transmitting. If Hyperterminal on the desktop shows what the laptop is sending then the problem is with your receive code. If it doesn't display then try opening Hyperterminal on both machines and see if you can talk to each other. If you still can't then the problem is likely to be hardware related.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Serial communication in windows - ClearCommError finds no data in read buffer

    RS232 to RS232 direct (without modems) would need a crossover cable (also known as null-modem cable (full crossover) or link cable (3-wire cross over))
    for pure transfer, 3 wires will do.
    If you need additional flow control, you'll need a full crossover.

    Like 2kaud said, try in a terminal program.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured