unit MainUnit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TMainForm = class(TForm) GreetButton: TButton; FirmButton: TButton; HelpButton: TButton; ConnectButton: TButton; ResendButton: TButton; SentMemo: TMemo; ReceivedMemo: TMemo; SentLabel: TLabel; ReceivedLabel: TLabel; BaudStatic: TStaticText; BaudLabel: TLabel; Baud28800Button: TButton; Baud9600Button: TButton; SendEdit: TEdit; SendButton: TButton; PortEdit: TEdit; PortLabel: TLabel; SyncLabel: TLabel; procedure BaudButtonClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormActivate(Sender: TObject); procedure SendButtonClick(Sender: TObject); procedure ResendButtonClick(Sender: TObject); procedure GreetButtonClick(Sender: TObject); procedure FirmButtonClick(Sender: TObject); procedure HelpButtonClick(Sender: TObject); procedure DisconnectButtonClick(Sender: TObject); procedure ConnectButtonClick(Sender: TObject); private procedure ReadCom(Data: String); procedure SendCommand(Data: String); public { Public declarations } end; var MainForm: TMainForm; implementation {$R *.DFM} uses Com; var Commo: TCom; ExpectingLine: Boolean; procedure TMainForm.FormCreate(Sender: TObject); begin Commo := TCom.Create; Commo.OnReadCom := ReadCom; end; procedure TMainForm.FormActivate(Sender: TObject); begin MainForm.Constraints.MaxWidth := MainForm.Width; MainForm.Constraints.MinWidth := MainForm.Width; end; procedure TMainForm.FormDestroy(Sender: TObject); begin Commo.Free; end; procedure TMainForm.BaudButtonClick(Sender: TObject); var BaudStr: String; Timeout: LongWord; begin BaudStr := (Sender as TButton).Caption; if Commo.Connected then begin if BaudStr = '1200' then SendCommand('BAUD=000') else if BaudStr = '2400' then SendCommand('BAUD=111') else if BaudStr = '4800' then SendCommand('BAUD=222') else if BaudStr = '9600' then SendCommand('BAUD=333') else if BaudStr = '14400' then SendCommand('BAUD=444') else if BaudStr = '19200' then SendCommand('BAUD=555') else if BaudStr = '28800' then SendCommand('BAUD=666') else if BaudStr = '57600' then SendCommand('BAUD=777') else Exit ; //Wait for incoming signal or until .1s have passed Timeout := GetTickCount; repeat Application.ProcessMessages; until (ExpectingLine = False) or (Abs(GetTickCount - Timeout) > 100); Commo.SetBaud(StrToInt(BaudStr)); end; BaudStatic.Caption := BaudStr; end; procedure TMainForm.ReadCom(Data: String); begin if ExpectingLine then begin ReceivedMemo.Lines[ReceivedMemo.Lines.Count-1] := Data; ExpectingLine := False; end else begin ReceivedMemo.Lines.Add(Data); SentMemo.Lines.Add('...'); end; end; procedure TMainForm.ConnectButtonClick(Sender: TObject); var Msg: PChar; begin if Commo.OpenCom(StrToInt(PortEdit.Text)) then begin ConnectButton.Caption := 'Disconnect'; ConnectButton.OnClick := DisconnectButtonClick; Commo.SetBaud(StrToInt(BaudStatic.Caption)); end else begin FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_ALLOCATE_BUFFER, nil, GetLastError, 0, PChar(@Msg), 0, nil); Application.MessageBox(@Trim('Failed to connect: ' + Msg)[1], 'Can''t Connect', MB_APPLMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONEXCLAMATION); LocalFree(LongWord(Msg)); end; end; procedure TMainForm.DisconnectButtonClick(Sender: TObject); begin Commo.CloseCom; ConnectButton.Caption := 'Connect'; ConnectButton.OnClick := ConnectButtonClick; end; procedure TMainForm.SendCommand(Data: String); begin Commo.WriteComLine(Data); SentMemo.Lines.Add(Data); ReceivedMemo.Lines.Add('...'); ExpectingLine := True; end; procedure TMainForm.SendButtonClick(Sender: TObject); begin SendCommand(SendEdit.Text); end; procedure TMainForm.ResendButtonClick(Sender: TObject); begin SendCommand('N'); end; procedure TMainForm.GreetButtonClick(Sender: TObject); begin SendCommand('H'); end; procedure TMainForm.FirmButtonClick(Sender: TObject); begin SendCommand('V'); end; procedure TMainForm.HelpButtonClick(Sender: TObject); begin SendCommand('?'); end; end.