Solved

How do I talk to a COM port under Windows9x/NT/2000 using Delphi?

Posted on 2000-03-06
12
341 Views
Last Modified: 2010-04-04
Can anyone give me an example on how to reterive and send data (byte by byte) to/from a COM port using 2400bps as speed 8bit?
0
Comment
Question by:Probie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +3
12 Comments
 
LVL 1

Expert Comment

by:yk030299
ID: 2589790
haha. 50pts costs this:
:-)
assignfile(f,'COM1:');

read(f,byte1);
write(f,byte1);
0
 
LVL 17

Expert Comment

by:inthe
ID: 2590421
Hi
i would use a component from here:
http://www.torry.ru/comms.htm
the Async32 v1.49 one is good and for free.
Regards Barry
0
 
LVL 1

Author Comment

by:Probie
ID: 2590596
Well, I would also like to know how I can setup the port, using all the possible setup values (bps, 7/8bit, stopbit)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:ITugay
ID: 2590716
The best library to work with COM ports is "Asyncpro" by TurboPower. (It may be only my idea).  There you can download trial version of it. If you will have "some troubles" with trial version,just  let me know;)


http://www.turbopower.com/products/apro/

Best regards,
igor@novell.kz
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2590767
Hi Probie,

I prefer this one: free and works like a charme.

http://www2.arnes.si/~sopecrni/

Zif.
0
 
LVL 4

Expert Comment

by:jeurk
ID: 2615476
Hello,

Zif ? Would you recommend that component
even for some commercial stuff. It's for
big and heavy productive use. Is it really working like a charm ?
Have you tested version 2.51 ? or 2.50 ?
Thanks

-Jeurk.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2616120
probie,
if you use any of the components we have suggested they have examples etc and you can set baud rates etc in object inspector..or code:

with comport1 do begin
device := comm2;
baudrate := br9600;
databits := da8;
stopbits := sb10;
end;
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2617065
Hi jeurk,

Yes, I've tested it, but not in heavy productive use. However, I'm going to use it in a commercial product.

Regards, Tom.
0
 
LVL 1

Author Comment

by:Probie
ID: 2617605
How does all these components work?
Is there any Windows API that can be used or can I write direct to the port.
like:
 mov DX,0x2F8;
 out DX,FF;
(which I know how to do)

I guess that windows uses the COMM.DRV file located in C:\WINDOWS\SYSTEM
But how does windows use this file and can I use it myself in delphi?


0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 2617715
you should download some of them and study the sources.the one i posted and the one zif posted comes with sources so you can see how they do it.
basicaly they are using the
createfile,readfile etc api's..
example:
procedure TForm1.Button1Click(Sender: TObject);
 var
   CommPort : string;
   hCommFile : THandle;
   ModemStat : DWord;
 begin
   CommPort := 'COM2';

  {Open the comm port}
   hCommFile := CreateFile(PChar(CommPort),
                           GENERIC_READ,
                           0,
                           nil,
                           OPEN_EXISTING,
                           FILE_ATTRIBUTE_NORMAL,
                           0);
   if hCommFile = INVALID_HANDLE_VALUE then
   begin
     ShowMessage('Unable to open '+ CommPort);
     exit;
   end;

  {Get the Modem Status}
   if GetCommModemStatus(hCommFile, ModemStat) <> false then begin
     if ModemStat and MS_CTS_ON <> 0 then
       ShowMessage('The CTS (clear-to-send) is on.');
     if ModemStat and MS_DSR_ON <> 0 then
       ShowMessage('The DSR (data-set-ready) is on.');
     if ModemStat and MS_RING_ON <> 0then
       ShowMessage('The ring indicator is on.');
     if ModemStat and MS_RLSD_ON <> 0 then
       ShowMessage('The RLSD (receive-line-signal-detect) is
 on.');
 end;

  {Close the comm port}
   CloseHandle(hCommFile);
 end;

but it is so much simpler to use a component than do it all by code.
have you looked at any of them yet ?

0
 
LVL 1

Author Comment

by:Probie
ID: 2618126
Yes, but I wan't to learn how it works.
Not just use a component that someone else has written.
I wrote many COM applications during the old Turbo Pascal time by writing direct to the COM port and the status registers that is used like:
2F8h,2F9h,2FAh.
I basiclly wanted to know the name of the Windows API, which I now do with help of the example Inthe gave.





0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2625649
If you look at the source code's of the components we published, you can learn everything from it.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

617 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