Solved

Blocking WriteFile to COM port

Posted on 2002-07-15
6
2,254 Views
Last Modified: 2012-06-22
Hi there.  I'm using a TCommPortDriver component, but when I write to the COMPort (using SendString) without my RS485 device connected, the whole program freezes up.  It freezes only when I have hardware handshaking (RTS/CTS) enabled and it freezes on the "WriteFile" statement.  Is there any way to make it non-blocking?
0
Comment
Question by:PaulKorzycki
  • 3
  • 3
6 Comments
 
LVL 2

Expert Comment

by:mullet_attack
ID: 7154708
you could check the handshaking status of your RS485 device , and if it's not on-line, then don't write to it
0
 

Author Comment

by:PaulKorzycki
ID: 7154715
how do you check the status?  What should I look for?  Why does WriteFile get stuck anyways, does it wait for CTS?
0
 
LVL 2

Accepted Solution

by:
mullet_attack earned 50 total points
ID: 7155756
i don't know the comm component your using, but i'll assume since it supports handshaking, it should have a CTS property or some sort of GetCTSStatus method.
if that's the case, determine the status of CTS when your RS485 device is not connected, and when it is connected.
if the status is the same, then we have a problem. if different then simply test for the appropriate status before you try writing to the port.

WriteFile can be used both blocking and non-blocking (overlapped i/o). I guess in your comm component it's used in blocking mode. try using a different comm component.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:PaulKorzycki
ID: 7159942
Thanks for all the help.  The component I'm using is v2.0 of TCommPortDriver by Marco Cocco.  It doesn't seem to have any way to check the CTS status, but I'm going to see if I can do it myself.  Any comments on how to do this via delphi?
0
 

Author Comment

by:PaulKorzycki
ID: 7160005
I figured it out.  The component sets the comm status using SetCommStatus.  One of the parameters it sets up in the dcb (device control block) is a flag that specifies options.  One of these options is "OutxCtsFlow" which makes the port wait for CTS to be high before sending any data!  I simply changed this flag to not include this option, and it works now.

The following site has great information regarding serial port functionality in Delphi:

http://www.geocities.com/macrotech_tr/hardware/tserialport.html

Thanks again!
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 7160048
looks like you just disabled the h/w handshaking.

here's the code from your component  :

case FComPortHwHandshaking of // Type of hw handshaking to use
    hhNONE:; // No hardware handshaking
    hhRTSCTS: // RTS/CTS (request-to-send/clear-to-send) hardware handshaking
      dcb.Flags := dcb.Flags or dcb_OutxCtsFlow or dcb_RtsControlHandshake;

You would have achieved the same result by setting hwflowcontrol property to "none" in the IDE

you could add an RTS_State property to the component, and get the value of RTS by using GetCommModemStatus. It will tell you what RTS is doing.

if that's of interest, I can help further, but not now. It's 2:45 AM, and I am a bit tired....
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now