[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

Com Ports

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
kennys
Asked:
kennys
  • 3
1 Solution
 
mirek071497Commented:
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
 
mirek071497Commented:
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
 
kennysAuthor Commented:
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
 
mirek071497Commented:
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

Independent Software Vendors: 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!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now