unit template; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, Registry,Menus, StdCtrls, jpeg, ComCtrls, Buttons; type TMainForm = class(TForm) CountTimer: TTimer; ComPanel: TPanel; Status: TStatusBar; IORich: TRichEdit; ComGroup: TGroupBox; Label7: TLabel; PortLabel: TLabel; ConnectButton: TButton; BaudStatic: TStaticText; PortEdit: TEdit; BaudCombo: TComboBox; DetectGroup: TGroupBox; DetectLabel: TLabel; DetectPB: TButton; CommandGroup: TGroupBox; HelloButton: TButton; VersionButton: TButton; ResendButton: TButton; SendEdit: TEdit; SendButton: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label8: TLabel; Label9: TLabel; GroupBox1: TGroupBox; GroupBox2: TGroupBox; GroupBox3: TGroupBox; GroupBox4: TGroupBox; Label10: TLabel; Button1: TButton; AcquireButton: TBitBtn; SaveDialog: TSaveDialog; procedure Exit2Click(Sender: TObject); procedure BaudComboChange(Sender: TObject); procedure ConnectButtonClick(Sender: TObject); procedure EndTestClick(Sender: TObject); procedure DetectPBClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure HelloButtonClick(Sender: TObject); procedure VersionButtonClick(Sender: TObject); procedure ResendButtonClick(Sender: TObject); procedure SendButtonClick(Sender: TObject); procedure GainChange(Sender: TObject); procedure PolarityChange(Sender: TObject); procedure Button1Click(Sender: TObject); procedure CountTimerTimer(Sender: TObject); procedure AcquireButtonClick(Sender: TObject); private procedure ReadCom(Data: String); procedure changebaud; public end; var MainForm: TMainForm; implementation uses InterComs; const MBK = MB_APPLMODAL or MB_SETFOREGROUND or MB_TOPMOST; USER_DEFAULT_LANGID = (SUBLANG_DEFAULT shl 10) + LANG_NEUTRAL; CurrentBaud: array[0..7] of String = ('1200','2400','4800','9600','14400','19200','28800','57600'); var //global variables for internal use {$R *.DFM} StuffedMemo: TStringList; ChannelList: array[0..7] of TLabel; LabelList: array[0..7] of TLabel; GainList: Array[0..7] of TButton; PolarityList: Array[0..7] of TButton; Channel, Watch : integer; POD, BaudIndex : integer; Commo: TInterCom; OutputToRich: Boolean; LoopCont: Boolean; GlobalComValue: string; procedure TMainForm.FormDestroy(Sender: TObject); begin StuffedMemo.Free; Commo.Free; end; procedure TMainForm.Exit2Click(Sender: TObject); begin Close; end; procedure TMainForm.FormCreate(Sender: TObject); var i : integer; begin StuffedMemo := TStringList.Create; Commo := TInterCom.Create(self); Commo.Parent := Self; Commo.ReadAsLines := True; Commo.OnReadCom := ReadCom; BaudCombo.ItemIndex := 3; for i:= 0 to 7 do begin GainList[i] := TButton.Create(self); GainList[i].Width := 60; GainList[i].Height := 21; GainList[i].Parent := GroupBox2; GainList[i].Top := 16 + (24 * i); GainList[i].Left := 8; GainList[i].Caption := '5'; GainList[i].OnClick := GainChange; GainList[i].Tag := i; GainList[i].Enabled := False; PolarityList[i] := TButton.Create(self); PolarityList[i].Width := 70; PolarityList[i].Height := 21; PolarityList[i].Parent := GroupBox3; PolarityList[i].Top := 16 + (24 * i); PolarityList[i].Left := 8; PolarityList[i].Caption := 'Bipolar'; PolarityList[i].OnClick := PolarityChange; PolarityList[i].tag := i; PolarityList[i].Enabled := False; LabelList[i] := TLabel.Create(self); LabelList[i].Parent := GroupBox4; LabelList[i].Top := 16 + (24 * i); LabelList[i].Left := 32; LabelList[i].Caption := '±5'; LabelList[i].Enabled := False; ChannelList[i] := TLabel.Create(self); ChannelList[i].Parent := GroupBox1; ChannelList[i].Top := 16 + (24 * i); ChannelList[i].Left := 20; ChannelList[i].Caption := '0000'; end; Channel := 0; Watch := 0; end; procedure TMainForm.ConnectButtonClick(Sender: TObject); var y : integer; Msg: PChar; begin if ConnectButton.Caption = 'Disconnect' then begin //If disconnecting Begin CountTimer.Enabled := False; AcquireButton.Tag := 0; AcquireButton.Caption := 'Start Aquire'; AcquireButton.Font.Color := clBlack; AcquireButton.Enabled := False; Commo.Close; //Close TInterComs instance Status.Panels[0].Text := 'Disconnected'; ConnectButton.Caption := 'Connect'; EndTestClick(self); CommandGroup.Visible := False; IORich.Clear; for y := 0 to 7 do begin GainList[y].Enabled := False; PolarityList[y].Enabled := False; LabelList[y].Enabled := False; end; end else if Commo.Open(StrToInt(PortEdit.Text), StrToInt(BaudStatic.Caption)) then begin //Else connect to com Status.Panels[0].Text := 'Connected on COM' + PortEdit.Text; ConnectButton.Caption := 'Disconnect'; CommandGroup.Visible := True; for y := 0 to 7 do begin GainList[y].Enabled := True; PolarityList[y].Enabled := True; LabelList[y].Enabled := True; end; AcquireButton.Enabled := True; 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.BaudComboChange(Sender: TObject); begin //Begin BaudComboChange BaudStatic.Caption := BaudCombo.Items[BaudCombo.ItemIndex]; end; //End BaudComboChange procedure TMainForm.ReadCom(Data : string); begin //Begin ReadCom {Receives Data from Com and stores it to a global variable} GlobalComValue := Data; if OutputToRich then begin IORich.Lines.Add(Data); end; end; //End ReadCom procedure TMainForm.HelloButtonClick(Sender: TObject); var y : cardinal; begin GlobalComValue := ''; y := GetTickCount + 50; Repeat Application.ProcessMessages; Until (GlobalComValue <> '') or (GetTickCount > Y); CountTimer.Enabled := False; OutputToRich := True; y := GetTickCount + 500; GlobalComValue := ''; Commo.WriteComLine('H'); //Send hello command to Pod Repeat Application.ProcessMessages; Until (GlobalComValue <> '') or (GetTickCount > Y); if GlobalComValue <> '' then begin Watch := 0; if (ConnectButton.Caption = 'Disconnect') and (AcquireButton.Tag = 1) then CountTimer.Enabled := True; end else if (Watch > 3) and (ConnectButton.Caption = 'Disconnect') then Application.MessageBox('Connection lost with pod, please check connections and power lines','Read failed!',MB_OK); end; procedure TMainForm.VersionButtonClick(Sender: TObject); var y : Cardinal; begin GlobalComValue := ''; y := GetTickCount + 50; Repeat Application.ProcessMessages; Until (GlobalComValue <> '') or (GetTickCount > Y); CountTimer.Enabled := False; OutputToRich := True; y := GetTickCount + 500; GlobalComValue := ''; Commo.WriteComLine('V'); //Send version command to Pod Repeat Application.ProcessMessages; Until (GlobalComValue <> '') or (GetTickCount > Y); if GlobalComValue <> '' then begin Watch := 0; if (ConnectButton.Caption = 'Disconnect') and (AcquireButton.Tag = 1) then CountTimer.Enabled := True; end else if (Watch > 3) and (ConnectButton.Caption = 'Disconnect') then Application.MessageBox('Connection lost with pod, please check connections and power lines','Read failed!',MB_OK); end; procedure TMainForm.ResendButtonClick(Sender: TObject); var y : Cardinal; begin GlobalComValue := ''; y := GetTickCount + 50; Repeat Application.ProcessMessages; Until (GlobalComValue <> '') or (GetTickCount > Y); CountTimer.Enabled := False; OutputToRich := True; y := GetTickCount + 500; GlobalComValue := ''; Commo.WriteComLine('N'); //Send resend command to Pod Repeat Application.ProcessMessages; Until (GlobalComValue <> '') or (GetTickCount > Y); if GlobalComValue <> '' then begin Watch := 0; if (ConnectButton.Caption = 'Disconnect') and (AcquireButton.Tag = 1) then CountTimer.Enabled := True; end else if (Watch > 3) and (ConnectButton.Caption = 'Disconnect') then Application.MessageBox('Connection lost with pod, please check connections and power lines','Read failed!',MB_OK); end; procedure TMainForm.SendButtonClick(Sender: TObject); var y : Cardinal; begin GlobalComValue := ''; y := GetTickCount + 50; Repeat Application.ProcessMessages; Until (GlobalComValue <> '') or (GetTickCount > Y); CountTimer.Enabled := False; OutputToRich := True; y := GetTickCount + 500; GlobalComValue := ''; Commo.WriteComLine(SendEdit.Text); //Send user inputed command to Pod Repeat Application.ProcessMessages; Until (GlobalComValue <> '') or (GetTickCount > Y); if GlobalComValue <> '' then begin Watch := 0; if (ConnectButton.Caption = 'Disconnect') and (AcquireButton.Tag = 1) then CountTimer.Enabled := True; end else if (Watch > 3) and (ConnectButton.Caption = 'Disconnect') then Application.MessageBox('Connection lost with pod, please check connections and power lines','Read failed!',MB_OK); end; procedure TMainForm.DetectPBClick(Sender: TObject); var FirstLoop,PodFound : Boolean; y : Cardinal; Msg: PChar; begin // Begin Detect Pod/Baud CountTimer.Enabled := False; DetectPB.Caption := 'Stop Detect'; DetectPB.OnClick := EndTestClick; LoopCont := True; PodFound := False; FirstLoop := True; BaudIndex := 2; POD := 0; if Commo.Open(StrToInt(PortEdit.Text), StrToInt(BaudStatic.Caption)) then begin ChangeBaud; OutputToRich := False; repeat if FirstLoop then begin // If first time talking to pod DetectLabel.Caption := 'Testing: '+ IntToHex(Pod,2) + ' on Baud ' + CurrentBaud[BaudIndex]; GlobalComValue := ''; Commo.WriteComLine('H'); Y := GetTickCount + 250; repeat Application.ProcessMessages; until (GlobalComValue <> '') or (GetTickCount > y); if GlobalComValue <> '' then PodFound := True else ChangeBaud; if BaudIndex = 3 then FirstLoop := False; end else begin // Else if POD = 256 then ChangeBaud; DetectLabel.Caption := 'Looking for Pod Address at '+ IntToHex(Pod,2) + ' on Baud ' + CurrentBaud[BaudIndex]; GlobalComValue := ''; Commo.WriteComLine('!' + IntToHex(POD,2)); Y := GetTickCount + 260; repeat Application.ProcessMessages; until (GlobalComValue <> '') or (GetTickCount > y); if GlobalComValue <> '' then PodFound := True else begin GlobalComValue := ''; Commo.WriteComLine('H'); Y := GetTickCount + 500; repeat Application.ProcessMessages; until (GlobalComValue <> '') or (GetTickCount > y); if GlobalComValue <> '' then PodFound := True else inc(POD); end; end; until (PodFound) or (LoopCont = False); 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; DetectLabel.Caption := ''; DetectPB.Caption := 'Detect'; DetectPB.OnClick := DetectPBClick; BaudCombo.ItemIndex := BaudIndex; if PodFound then Status.SimpleText := 'Found Pod Address at ' + IntToHex(Pod,2) + ' on Baud ' + CurrentBaud[BaudIndex]; if (ConnectButton.Caption = 'Disconnect') and (AcquireButton.Tag = 1) then CountTimer.Enabled := True; end; // End Detect Pod/Baud Procedure TMainForm.ChangeBaud; var Msg: PChar; begin if BaudIndex = 6 then BaudIndex := 0 else inc(BaudIndex); //Cycle through all valid baud rates Commo.Close; //Close instance of InterComs for reopening with new Baud rate if Commo.Open(StrToInt(PortEdit.Text), StrToInt(CurrentBaud[BaudIndex])) then //Reopen instance of InterComs Pod := 0 //Reset Pod address check to 0 else begin //Else warn user 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.EndTestClick(Sender: TObject); begin LoopCont := False; //If test is ended before Pod Address found then end the loop DetectPB.Caption := 'Detect'; DetectPB.OnClick := DetectPBClick; end; procedure TMainForm.GainChange(Sender: TObject); begin if GainList[(Sender as TButton).Tag].Caption = '10' then begin if PolarityList[(Sender as TButton).Tag].Caption = 'Unipolar' then LabelList[(Sender as TButton).Tag].Caption := '0 to 5' else LabelList[(Sender as TButton).Tag].Caption := '±5'; GainList[(Sender as TButton).Tag].Caption := '5'; end else begin if PolarityList[(Sender as TButton).Tag].Caption = 'Unipolar' then LabelList[(Sender as TButton).Tag].Caption := '0 to 10' else LabelList[(Sender as TButton).Tag].Caption := '±10'; GainList[(Sender as TButton).Tag].Caption := '10'; end; end; procedure TMainForm.PolarityChange(Sender: TObject); begin if PolarityList[(Sender as TButton).Tag].Caption = 'Bipolar' then begin if GainList[(Sender as TButton).Tag].Caption = '5' then LabelList[(Sender as TButton).Tag].Caption := '0 to 5' else LabelList[(Sender as TButton).Tag].Caption := '0 to 10'; PolarityList[(Sender as TButton).Tag].Caption := 'Unipolar'; end else begin if GainList[(Sender as TButton).Tag].Caption = '5' then LabelList[(Sender as TButton).Tag].Caption := '±5' else LabelList[(Sender as TButton).Tag].Caption := '±10'; PolarityList[(Sender as TButton).Tag].Caption := 'Bipolar'; end; end; procedure TMainForm.Button1Click(Sender: TObject); begin Close; end; procedure TMainForm.CountTimerTimer(Sender: TObject); var x, y : integer; Volts : Double; Counts: Smallint; MaxVolts: Integer; Test : String; begin CountTimer.Enabled := False; OutputToRich := False; Y := GetTickCount + 500; GlobalComValue := ''; if GainList[Channel].Caption = '10' then Test := '8' else Test := '0'; Commo.WriteComLine('A' + IntToStr(ord(PolarityList[Channel].Caption = 'Bipolar')) + Test + IntToHex((channel shl 4),2)); Repeat Application.ProcessMessages; Until (GlobalComValue <> '') or (GetTickCount > y); if GlobalComValue <> '' then begin y := 0; Test := GlobalComValue; For X := 1 to Length(Test) do if Test[x] in ['0'..'9','A'..'F'] then inc(y); if not(Length(Test) = Y) then inc(Watch) else begin Watch := 0; Counts := StrToInt('$' + Test); MaxVolts := StrToInt(GainList[Channel].Caption); if PolarityList[Channel].Caption = 'Bipolar' then Volts := (MaxVolts * 2 * Counts - MaxVolts) / 4096 else Volts := (MaxVolts * Counts) / 4096; ChannelList[Channel].Caption := FloatToStrF(Volts,ffFixed, 7, 3); StuffedMemo.Add('Ch. ' + IntToStr(channel) + ',' + DateTimeToStr(now) + ',' + FloatToStrF(Volts,ffFixed, 7, 3)); //Store human readable string if Channel = 7 then Channel := 0 else inc(Channel); end; end else inc(Watch); if (Watch > 3) and (ConnectButton.Caption = 'Disconnect') then begin Application.MessageBox('Connection lost with pod, please check connections and power lines','Read failed!',MB_OK); AcquireButton.Tag := 0; CountTimer.Enabled := False; if SaveDialog.FileName <> '.\.' then StuffedMemo.SaveToFile(SaveDialog.Filename); AcquireButton.Caption := 'Start Acquire'; AcquireButton.Font.Color := clBlack; Exit; end; if (ConnectButton.Caption = 'Disconnect') and (AcquireButton.Tag = 1) then CountTimer.Enabled := True; end; procedure TMainForm.AcquireButtonClick(Sender: TObject); var Present: TDateTime; Year, Month, Day: Word; TempStr, TempMonth, TempDay : String; begin if AcquireButton.Tag = 0 then begin Present:= Now; //Get current time DecodeDate(Present, Year, Month, Day); //Decode current time if Month < 10 then TempMonth := '0' + IntToStr(Month) else TempMonth := IntToStr(Month); if Day < 10 then TempDay := '0' + IntToStr(Day) else TempDay := IntToStr(Day); TempStr := copy(IntToStr(Year),3,2) + TempMonth + TempDay; //Store current time SaveDialog.FileName := ExtractFilePath(SaveDialog.FileName) + 'Log' + TempStr + '.CSV'; repeat //Begin Save dialog loop if not(SaveDialog.Execute) then Break else if FileExists(SaveDialog.FileName) then begin if Application.MessageBox('A file with that name already exists - Overwrite?', 'File already Exists', MB_OKCANCEL) = IDOK then begin AcquireButton.Tag := 1; AcquireButton.Font.Color := clRed; AcquireButton.Caption := 'Stop Acquire'; DetectGroup.Visible := False; CommandGroup.Visible := False; CountTimer.Enabled := True; Break; end else Break; end else begin DetectGroup.Visible := False; CommandGroup.Visible := False; AcquireButton.Tag := 1; AcquireButton.Font.Color := clRed; AcquireButton.Caption := 'Stop Acquire'; CountTimer.Enabled := True; Break; end; until False; //End Save dialog loop end else begin AcquireButton.Tag := 0; CountTimer.Enabled := False; if SaveDialog.FileName <> '.\.' then StuffedMemo.SaveToFile(SaveDialog.Filename); AcquireButton.Caption := 'Start Acquire'; AcquireButton.Font.Color := clBlack; end; end; end.