unit MainUnit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; type TMainForm = class(TForm) Label1: TLabel; Label2: TLabel; LineEdit: TEdit; MainRich: TRichEdit; PortEdit: TEdit; BaudCombo: TComboBox; ConnectButton: TButton; procedure FormCreate(Sender: TObject); procedure LineEditKeyPress(Sender: TObject; var Key: Char); procedure ConnectClick(Sender: TObject); procedure DisconnectClick(Sender: TObject); private procedure ReadCom(Data: String); procedure ShowText(Text: String; Col: TColor); { Private declarations } public { Public declarations } end; var MainForm: TMainForm; implementation {$R *.DFM} uses Com; var Commo: TCom; procedure TMainForm.ShowText(Text: String; Col: TColor); begin MainRich.SelStart := Length(MainRich.Lines.Text); MainRich.SelAttributes.Color := Col; MainRich.Lines.Add(Text); MainRich.SetFocus; LineEdit.SetFocus; end; procedure TMainForm.ReadCom(Data: String); begin ShowText(Data, clBlack); end; procedure TMainForm.FormCreate(Sender: TObject); begin Commo := TCom.Create; Commo.OnReadCom := ReadCom; BaudCombo.ItemIndex := 0; end; procedure TMainForm.LineEditKeyPress(Sender: TObject; var Key: Char); begin if Key = #13 then begin Key := #0; ShowText(LineEdit.Text, clBlue); Commo.WriteCom(LineEdit.Text); end; end; procedure TMainForm.ConnectClick(Sender: TObject); begin if Commo.OpenCom(StrToInt(PortEdit.Text)) then begin if BaudCombo.ItemIndex >= 0 then Commo.SetBaud(StrToInt(BaudCombo.Text)) ; ConnectButton.Caption := 'Disconnect'; ConnectButton.OnClick := DisconnectClick; end else Application.MessageBox('Could not connect. Please check your Com port settings.', 'Com Error', MB_OK); end; procedure TMainForm.DisconnectClick(Sender: TObject); begin Commo.CloseCom; ConnectButton.Caption := 'Connect'; ConnectButton.OnClick := ConnectClick; end; end.