Solved

Com Ports

Posted on 1997-09-20
4
331 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Norton antivirus 11 66
Burning ISO Win/Mac 21 43
delphi parse string to params 3 82
Backup Data/Portable NAS storage 4 49
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Let’s list some of the technologies that enable smooth teleworking. 
Viewers will learn how to use the Hootsuite Dashboard.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

706 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

20 Experts available now in Live!

Get 1:1 Help Now