Solved

RS232

Posted on 2002-04-08
7
353 Views
Last Modified: 2010-04-04
Before I Talked with PortRS232 but Using API Function, Now I have Delphi V and I can't to talk with Port, I need help (Sorry my english is poor)
0
Comment
Question by:chiro
7 Comments
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6926623
You read/write a file.
Use CreateFile() to gain access to a comm port
You will need to use GetCommState() and BuildCommDCB() to set the port and ReadFile() to get the data.
Look up CreateFile.

0
 

Author Comment

by:chiro
ID: 6927183
I need an example for set,open, write, read ... Port !!!
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6927201
You cannot access ports anymore in Windows NT, 2000, XP!
0
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.

 
LVL 10

Expert Comment

by:Jacco
ID: 6927331
A good set of componnets to do it for you are the AsyncPro set of TurboPower.

Good luck,

Jacco
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6927487
Hello

  you can find free components in the torry for doing that, look at http://www.torry.net/modems.htm, and you can download the components with sample files
0
 
LVL 3

Accepted Solution

by:
VSF earned 100 total points
ID: 6928016
> I need some help for read data sent to the serial port.
> Can someone give me a example?

procedure TForm1.Button1Click(Sender: TObject);
var
   lpszNumeCom    : PChar;
   dwAccessType   : DWORD;
   dwCreationType : DWORD;
   creationDCB    : TDCB;
   hPort          : THandle;
   buffer         : array[0..55] of char;
   actRead        : integer;
begin
 {Some initial settings: I am working with COM2 in read/write}
 lpszNumeCom    := '//./COM2';
 dwAccessType   := GENERIC_READ or GENERIC_WRITE;
 dwCreationType := OPEN_EXISTING;
 hPort := CreateFile(lpszNumeCom,dwAccessType,0,Nil,dwCreationType,0,0);

 {here I have the handle for the COM port}
 try
  if hPort <> INVALID_HANDLE_VALUE then
   begin
    {Settings for COM : 600bauds, 8,E,1}
    GetCommState(hport,creationDCB);
    creationDCB.BaudRate := 600;
    creationDCB.Parity := EVENPARITY;
    creationDCB.ByteSize := 8;
    creationDCB.StopBits := ONESTOPBIT;
    {Settings for the control type, here I am reading from a Proximity
card
    reader and use only the RX line}
    creationDCB.Flags := creationDCB.Flags + DTR_CONTROL_DISABLE
       + RTS_CONTROL_DISABLE;
    if SetCommState(hPort,creationDCB) then
     begin
      PurgeComm(hPort,PURGE_RXCLEAR);
      {Here I actually can read data from COM}
      {The next function call will not return until it actually reads
data
      from the serial port}
      ReadFile(hPort,buffer,56,actRead,nil);
      PurgeComm(hPort,PURGE_RXCLEAR);
      {in buffer you have data readed from the port}
      begin
       {Do something usefull with data}
      end
     end
   end;
 finally
  CloseHandle(hPort);
 end;
end;

----
> I need to send something (anything - it doesn't matter what) to the serial
> port  (COM 2).
> Help tells that we can use standard CreateFile('COM2',...) function to
> communicate with port but I dont believe it - I tried and got nothing

You can make it work with a little more effort.  Here are some more clues:


    fUnit: DWORD;             { Unit descriptor handle}
    fUnitDescriptor: string;    { Unit descriptor Pascal string }

fUnitDescriptor := 'COM2';

procedure TRs232.OpenPort(descriptor: PChar);
begin
  fUnit := CreateFile(descriptor, GENERIC_READ or GENERIC_WRITE,
                                 0,nil,OPEN_EXISTING,0,0);
end;

function TRs232.GetOpenPortError: BOOLEAN;
  var
  isOK: BOOLEAN;
  Error: DWORD;

begin
  isOK:=(fUnit <> INVALID_HANDLE_VALUE);  //DWORD(-1)
   if not isOK then
   begin
     Error := GetLastError;
     {case fUnit of}
     case Error of
       IE_BADID:  ShowMessage('"' + fUnitDescriptor + '" invalid or unsupported');
       IE_BAUDRATE:  ShowMessage('"' + fUnitDescriptor + '" baudrate unsupported');
       IE_BYTESIZE:  ShowMessage('Specified bytesize is invalid');
       IE_DEFAULT: ShowMessage('Default parameters are in error');
       IE_HARDWARE: ShowMessage('"' + fUnitDescriptor + '" not available');
       IE_MEMORY: ShowMessage('"' + fUnitDescriptor + '" - unable to allocate queues');
       IE_NOPEN:  ShowMessage('"' + fUnitDescriptor + '" is not open');
       IE_OPEN: ShowMessage('"' + fUnitDescriptor + '" is already open');
       else
         {ShowMessage('Note: "' + fUnitDescriptor + '" returned error ' +
          IntToStr(Error)); }
     end;
   end;
   Result:=isOK;
end;



function TRs232.Output(lpBuffer: PChar): BOOLEAN;
var
  lpBytesSent,nBufferSize: LongWord;

begin
  nBufferSize := StrLen(lpBuffer);
  Result := WriteFile(fUnit,        //handle to file to write to
                      lpBuffer,     // pointer to data to write to file
                      nBufferSize,  // number of bytes to write
                      lpBytesSent,  // pointer to number of bytes written
                      nil);  // pointer to structure needed for overlapped IO

end;

function TRs232.Enter(nBytesToRead: LongWord; lpString: PChar; var
lpBytesRead: LongWord): BOOLEAN;

begin

  Result := ReadFile(fUnit,
                     lpString,
                     nBytesToRead,
                     lpBytesRead,
                     nil);
end;

destructor TRs232.Destroy;
begin
  if not CloseHandle(fUnit) then
    ShowMessage('Error closing port "' + fUnitDescriptor + '"');
  inherited Destroy;
end;



Hope this helps!
VSF
www.victory.hpg.com.br
www.boatoda.hpg.com.br
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9003002
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Answer from VSF

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 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

13 Experts available now in Live!

Get 1:1 Help Now