application.terminate;Before I go any further... my thanks to the good people at Borland (Inprise) for the good information there, from which the heart of all this came.
hCommFile : THandle;
... by putting the above just after the word "private" in the TDD55F1 class declaration.
Then, just after...
hCommFile : THandle;
... insert the following forward declaration....
procedure WriteString(sToSend:string);
... and just before the unit's terminal "end.", insert....
procedure TDD55F1.WriteString(sToSend:string);
var NumberWritten : dWord;
{N.B.: I saw the following in a newsgroup post:
"Warning: There is an error in the FAQ - NumberWritten must be
dword not LongInt"}
begin
if WriteFile(hCommFile,
PChar(sToSend)^,
Length(sToSend),
NumberWritten,
nil)=false then
showmessage('Unable to send');
end;
(We'll see what this is good for in a moment!)
procedure TDD55F1.buOpenConnectionClick(Sender: TObject);
var
CommPort : string;
NumberWritten : dWord;
{N.B.: I saw the following in a newsgroup post:
"Warning: There is an error in the FAQ - NumberWritten must be dword not LongInt"}
begin
CommPort := 'COM1';
hCommFile := CreateFile(PChar(CommPort),
GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if hCommFile=INVALID_HANDLE_VALUE then begin
ShowMessage('Unable to open '+ CommPort);
end // no ; here
else begin
buSendX.enabled:=true;
buSendY.enabled:=true;
buCloseConnection.enabled:=true;
buOpenConnection.enabled:=false;
end; //else
end;
Note: If when you connected via Hyperterminal, you were using a Com port other than 1, then you will need to modify the COM1 above accordingly.
procedure TDD55F1.buSendXClick(Sender: TObject);
begin
WriteString('X');
end;
Run the program. Click on the "Open connection" button. Click the "Send X" button. You should see an "X" appear on the other computer! You should click on the "Close Connection" button before exiting the program, though Windows may clean up after you if you fail to do so. A "proper" application would need to monitor whether the connection was open or closed, close it when the program is closed, if not closed.
procedure TDD55F1.buSendYClick(Sender: TObject);
begin
WriteString('Y');
end;
Now you can send "X" or "Y" to the other computer (Useful, when you've sent a bunch of Xs, and it is a little hard to see if another is arriving!
procedure TDD55F1.buHelloWorldClick(Sender: TObject);
begin
WriteString('Hello World ');
end;
... and then you can send "Hello World" to the destination computer. Ta! Da!
WriteString(chr(65));SendX will now send 65 to the other computer. If it is running Hyperterminal, it will display an "A". There is nothing to stop you from creating a system which will dispense 65 marbles if it is sent 65... but that's a story for another time! (Or visit my site about sensing and control.)
|
|
Click here to visit editor's freeware, shareware page.