Solved

RS232

Posted on 2002-04-08
7
359 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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