Solved

Com Ports

Posted on 1997-09-20
4
332 Views
Last Modified: 2013-11-15
I upgraded to Delphi3 frm D1 and have the following question

I would like to know how to send a telephone number to com port 2.

I was given the following answer but unfortunatly, as a beginner,I still
need more help.

This is the answer I was given.

>if u used the port[xxx]:=? to access the com port, u cant anymore
>since this function is obsolete from delphi 2.0, here is a replica of
>the port function- just use getport and setport;

>instead of: port[888]:=10; use: setport(888,10);
>instead of: i:=port[888]; use: i:=getport(888);

>procedure setport(index:word;const value:byte);assembler;
>asm
>mov al,value
>mov dx,index
>out dx,al
>end;


>function getport(index:word):byte;assembler;
>asm
>mov dx,index
>in al,dx
>mov result,al
>end;



My modem is on com port 2 so I assume I use
setport(2,xx) to set the port to 2.

What is the second parameter(xx) for in setport.

Can anyone show me how to send a phonenumber to com port 2

Do I need to close the port and if so, how.


In my Delphi 1 program I used the following:-

 begin
   CloseComm(ComID);
   StrPCopy(Prt2, Prt);
   ComID := OpenComm(Prt2, 1024, 2048);
   dialed:=s;
   
   Vol = Volume D = TP =pulse dialing
   DialString := 'ATM2' + Vol + 'D' + TP + S + ',,' + ^M^J;
   StrPCopy(Send, DialString);
   if WriteComm(ComID, Send, StrLen(Send)) >= 0 then
   begin

    ShowMessage('          Dialing ' + S + #13 + #13');

    DialString := 'ATH' + ^M^J;
    startt:=timetostr(time);
    StrPCopy(Send, DialString);
 
    WriteComm(ComID, Send, StrLen(Send));

   end;
0
Comment
Question by:kennys
  • 3
4 Comments
 
LVL 3

Expert Comment

by:mirek071497
ID: 1345674
This is not good way you need rather WriteFile and ReadFile to use COM ports on Win32 environment. I have not now example so i must first try but this need like this :

var
st : ShortString; { strin to send }
pHand : THandle;
Count : Longint;

{ first you must get handle to COM2 }
pHand := CreateFile( 'COM2',
                       GENERIC_READ+GENERIC_WRITE,
                       0,
                       nil,
                       OPEN_EXISTING,
                       FILE_ATTRIBUTE_NORMAL,
                       0);

if pHand=INVALID_HANDLE_VALUE then
begin
    ShowMessage( 'INVALID HANDLE' );
    exit;
end;

{ now you can use WriteFile or ReadFile }
if not WriteFile(pHand,st[1],length(st),Count,nil) then
    ShowMessage('not written' )
else
    ShowMessage( 'Written '+IntToSTr(Count)+' bytes');

{ after work you must call CloseHandle }
CloseHandle(pHand);

I try this later and mayby on monday i give you simple working example.

Regards
Mirek
0
 
LVL 3

Accepted Solution

by:
mirek071497 earned 200 total points
ID: 1345675
Ok This is working Example

procedure SendCOM( nr : byte; st : ShortString );
var
  Port  : String;
  pHand : Integer;
  Count : Longint;
  FCommConfig : TCommConfig;
begin
  Port := 'COM'+IntToSTr(nr);
  pHand := CreateFile( PCHar(Port), GENERIC_READ+GENERIC_WRITE, 0, nil,
                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if pHand=INVALID_HANDLE_VALUE then
  begin
    ShowMessage( 'INVALID HANDLE' );
    exit;
  end;
  Count:= Sizeof(TCommConfig);
  GetCommConfig( pHand, FCommConfig, Count);
  SetCommState( pHand, FCommConfig.DCB);
  { here you can play with Comm Settings
    functions :
      SetupComm, GetCommTimeOuts and SetCommTimeOuts,
      GetCommState and SetCommState
  descriftion is in Win32 Help }

  if not WriteFile(pHand,st[1],length(st),Count,nil) then
    ShowMessage('not written' )
  else
    ShowMessage( 'Written '+IntToSTr(Count)+' bytes');

  CloseHandle(pHand);
end;


use this as :

SendCOM( 2, 'ATDT 2231232' );
0
 

Author Comment

by:kennys
ID: 1345676
I tried the example you gave me but it does not dial
and returns "16 bytes written" when I try to phone a number.
I have substituted the number example you had after ATDT.
 
I realise this is way beyond me as I have only started
with Delphi,but I would like to be able to do this and
build a program arround it.
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1345677
You must use this function with #13#10 so modem must know if liine was ended

exmple
  SendCOM( 2,'ATDT 12312333'#13#10 );
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.

863 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

23 Experts available now in Live!

Get 1:1 Help Now