Solved

RS232

Posted on 2002-04-08
7
370 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
Industry Leaders: 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!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 82
Convert MS Word document to a PDF file 9 115
DBCtrlGrid, Delphi, Scroll 8 60
Firemonkey Listview item popup menu ? 1 39
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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