Re: com port communication with a device
[email protected] Wed, 6 Jun 2007 07:58:39 EDT
| Newsgroups | gmane.comp.lang.delphi.jedi |
|---|---|
| Message-ID | <[email protected]> |
Here is some code I used to interface with foot-pedals which used the
protocol lines, but the general design is similar to handling data transfer.
procedure TStatusThread.Execute;
var
hComm : THandle;
DCB : TDCB;
OLStatus : TOverlapped;
Reslt, TmpInt : DWord;
TimedOut : boolean;
begin
FreeOnTerminate := true;
try
try
hComm := 0;
{open the comm port as a file}
hComm := CreateFile(PChar(PortName[CommSelf.FPort]), GENERIC_READ or
GENERIC_WRITE,
0, nil, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED, 0);
if hComm = INVALID_HANDLE_VALUE then
raise EPortError.Create(
Format('Failed to open %s port : Error %d',
[PortName[CommSelf.FPort], GetLastError]));
{modify the comm port protocol to give us a "switch supply voltage"}
GetCommState(hComm, DCB); // get port existing setup
with DCB do begin
{although we don't send & receive data, the DCB settings are not
implemented on the comm port unless the data parameters are set}
BaudRate := CBR_19200; // set some baud rate
ByteSize := 8;
Parity := NOPARITY;
StopBits := ONESTOPBIT;
DCBlength := SizeOf(DCB);
{this are flags to set our "switch supply voltage"}
Flags := (RTS_CONTROL_ENABLE shl 12);
// shl 12 positions the RTS_CONTROL_ENABLE flag in the other flags
end;
if not SetCommState(hComm, DCB) then // change the settings
raise EPortError.Create(Format('SetCommState Failed : Error %d',
[GetLastError]));
{these are the protocol signals to monitor for our "switched inputs"}
SetCommMask(hComm, EV_CTS or EV_DSR or EV_RLSD);
{note : Don't use EV_RING, it is not detected in Win 95 - MS KB Q137862}
OLStatus.hEvent := CreateEvent(nil, true, false, nil); // to watch the port
if (OLStatus.hEvent = 0) then
raise EPortError.Create(
Format('Failed to create overlapped event : Error %d',
[GetLastError]));
TimedOut := false; // set here to cause initial WaitCommEvent()
repeat
if not TimedOut then // set an event monitor
WaitCommEvent(hComm, FCommEvent, @OLStatus);
{now wait for the event or a timeout}
Reslt := WaitForSingleObject(OLStatus.hEvent, 300{mS});
case Reslt of
WAIT_OBJECT_0 : if GetOverlappedResult(hComm, OLStatus, TmpInt, false) then
begin
{a comm event has occurred - check the line status ...}
GetCommModemStatus(hComm, FModemStatus);
{... and indicate the status on the form}
Synchronize(HandleCommEvent);
{... and trigger a fresh event monitoring}
TimedOut := false;
end {if GetOverlappedResult(etc etc)}
else
raise EPortError.Create(
Format('Error in overlapped status : Error %d',
[GetLastError]));
{end; if GetOverlappedResult(etc etc) else}
WAIT_TIMEOUT : {start the watch again - event monitor is still valid}
TimedOut := true;
else begin
raise EPortError.Create(
Format('WaitForSingleObject failed : Error %d',
[GetLastError]));
Terminate;
end;
end; {case Reslt}
until Terminated;
finally
CloseHandle(OLStatus.hEvent);
CloseHandle(hComm);
Terminate;
end; {try / finally}
except
on E: Exception do
ShowMessage(E.Message);
end; {try / except}
end;
Alan Lloyd
[Non-text portions of this message have been removed]
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Delphi-JEDI/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/Delphi-JEDI/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:[email protected]
mailto:[email protected]
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/