[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

RS232

Posted on 2002-04-08
7
Medium Priority
?
402 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
Independent Software Vendors: 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 400 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and 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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

872 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