Solved

Com Ports

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Let’s list some of the technologies that enable smooth teleworking. 
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
The viewer will learn common shortcuts with easy ways to remember them. The viewer will then learn where to find all of the keyboard shortcuts, how to create/change them, and how to speed up their workflow.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.

770 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