Re: strange problem with serial communication

From: Scott McPhillips [MVP] (org-dot-mvps-at-scottmcp)
Date: 12/24/04

  • Next message: Bruce Eitman \(eMVP\): "Re: String Manipulation!"
    Date: Fri, 24 Dec 2004 10:25:16 -0500
    
    

    Maverick wrote:

    > Hi, all
    >
    > I met a big trouble. I am using MFC and multi-thread to write a serial port
    > communication program. Each time I send a string, I alwayse get part of it.
    > For example , I send a string of 120 characters, what I receive is a string
    > which contains only top 8 or 16 or 24 or 32 characters. I cannot receive the
    > same string at all. I've tried different baud rate, including 19200,9600,
    > but all failed. Below is part of my code.

    Your code is written to stop reading whenever ReadFile gets a timeout,
    but you have set the timeout to zero. The code is also written to input
    only one byte for each ReadFile call. These are unreliable things to do.

    Pass a buffer to ReadFile that is large enough to receive the maximum
    expected number of characters. And set the timeout to be a generous
    amount of time that you are sure indicates all incoming characters have
    stopped.

    There are also unreliable features in your thread design. In MFC, the
    thread should be started with AfxBeginThread and it should not access
    the dialog controls. This can cause program lockup. It is also not
    clear how you expect the main thread to know when the reading is
    complete. No interthread signaling or synchronization is present in
    your code, so it may be that your main thread is accessing the buffer
    before the reading is complete.

    -- 
    Scott McPhillips [VC++ MVP]
    

  • Next message: Bruce Eitman \(eMVP\): "Re: String Manipulation!"