Howdy, Stranger!

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

Categories

ReadEvent on Serial Port

ratep2001ratep2001 Member Posts: 1
Hi,
I have found many source codes all over the network about how to access the serial port in Delphi. Everybody uses handles with CreateFile (COMx,......) etc, then uses ReadFile, WriteFile. When it comes to receiving characters, almost everybody uses SetCommMask to set for EV_RXCHAR event, then uses WaitCommEvent to respond to this particular event.
But does it mean that I must use some kind of a loop and wait for the event, thus risking to stay in a loop forever and don't make anything else useful? How can I solve this problem more elegantly, like having an event handler for my COMx handle to respond to? I know that one can write an EventHandle for an object, but an event handler for a virtual file handler referring to COMx ??
I would like to stress that I don't want to use some third party components, this solution with having a handle to COMx and making different system calls like GetCommState, WaitCommEvent etc. seems really nice, but how to respond to the incoming characters immediately and empty the input buffer ??? And still not being in a loop and waiting for event, which might not happen at all or occur once in several minutes or so.
Thank you in advance.

Comments

  • zibadianzibadian Member Posts: 6,349
    : Hi,
    : I have found many source codes all over the network about how to access the serial port in Delphi. Everybody uses handles with CreateFile (COMx,......) etc, then uses ReadFile, WriteFile. When it comes to receiving characters, almost everybody uses SetCommMask to set for EV_RXCHAR event, then uses WaitCommEvent to respond to this particular event.
    : But does it mean that I must use some kind of a loop and wait for the event, thus risking to stay in a loop forever and don't make anything else useful? How can I solve this problem more elegantly, like having an event handler for my COMx handle to respond to? I know that one can write an EventHandle for an object, but an event handler for a virtual file handler referring to COMx ??
    : I would like to stress that I don't want to use some third party components, this solution with having a handle to COMx and making different system calls like GetCommState, WaitCommEvent etc. seems really nice, but how to respond to the incoming characters immediately and empty the input buffer ??? And still not being in a loop and waiting for event, which might not happen at all or occur once in several minutes or so.
    : Thank you in advance.
    :
    There are two useful solutions.
    The first is the easiest to code: a time-out function. You need to global boolean variable, which is set to false prior to the loop. Then you run the loop to wait for a comm event. Inside that loop you need to call the ProcessMessages, otherwise your computer will lock up. The second thing you need is a TTimer. In the OnTime you set the global boolean to true. Obviously you loop until the comm event or the boolean is true.
    The second solution is to create a multi-threaded program. This allows the user to keep on working with the program, which it still checks the com port for the expected characters. If there are any you let the thread receive them, and then synchronize the action to send them to the primary thread.

  • MJarusMJarus Member Posts: 19
    : Hi,
    : I have found many source codes all over the network about how to access the serial port in Delphi. Everybody uses handles with CreateFile (COMx,......) etc, then uses ReadFile, WriteFile. When it comes to receiving characters, almost everybody uses SetCommMask to set for EV_RXCHAR event, then uses WaitCommEvent to respond to this particular event.
    : But does it mean that I must use some kind of a loop and wait for the event, thus risking to stay in a loop forever and don't make anything else useful? How can I solve this problem more elegantly, like having an event handler for my COMx handle to respond to? I know that one can write an EventHandle for an object, but an event handler for a virtual file handler referring to COMx ??
    : I would like to stress that I don't want to use some third party components, this solution with having a handle to COMx and making different system calls like GetCommState, WaitCommEvent etc. seems really nice, but how to respond to the incoming characters immediately and empty the input buffer ??? And still not being in a loop and waiting for event, which might not happen at all or occur once in several minutes or so.
    : Thank you in advance.
    :

    Look at the ASyncPro components by TurboPower. They are now freeware (open source) and are available on SourceForge (www.sourceforge.com).
    ===============
    www.cafe441.com
    ===============
    Music is the art of thinking with sounds.
    -- Jules Combarieu


Sign In or Register to comment.