Solved

Com Ports

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

695 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