Solved

RS232

Posted on 2002-04-08
7
346 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
Comment Utility
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
Comment Utility
I need an example for set,open, write, read ... Port !!!
0
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
You cannot access ports anymore in Windows NT, 2000, XP!
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 10

Expert Comment

by:Jacco
Comment Utility
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:mnasman
Comment Utility
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
Comment Utility
> 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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

15 Experts available now in Live!

Get 1:1 Help Now