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; BaudLabel: 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; ChannelLabel0: TLabel; ChannelLabel1: TLabel; ChannelLabel2: TLabel; ChannelLabel3: TLabel; ChannelLabel4: TLabel; ChannelLabel5: TLabel; ChannelLabel6: TLabel; ChannelLabel7: TLabel; VoltsGroupBox: TGroupBox; ExitButton: TButton; AcquireButton: TBitBtn; SaveDialog: TSaveDialog; CountsLabel0: TLabel; CountsLabel1: TLabel; CountsLabel2: TLabel; CountsLabel3: TLabel; CountsLabel4: TLabel; CountsLabel5: TLabel; CountsLabel6: TLabel; CountsLabel7: TLabel; Memo1: TMemo; OffsetGroupBox: TGroupBox; OffsetEdit0: TEdit; OffsetEdit1: TEdit; OffsetEdit2: TEdit; OffsetEdit3: TEdit; OffsetEdit4: TEdit; OffsetEdit5: TEdit; OffsetEdit6: TEdit; OffsetEdit7: TEdit; EndedGroupBox: TGroupBox; DifferentialCheck0: TCheckBox; DifferentialCheck1: TCheckBox; DifferentialCheck2: TCheckBox; DifferentialCheck3: TCheckBox; DifferentialCheck4: TCheckBox; DifferentialCheck5: TCheckBox; DifferentialCheck6: TCheckBox; DifferentialCheck7: TCheckBox; 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 ExitButtonClick(Sender: TObject); procedure CountTimerTimer(Sender: TObject); procedure AcquireButtonClick(Sender: TObject); procedure OffsetEditKeyPress(Sender: TObject; var Key: Char); private procedure ReadCom(Data: String); procedure changebaud; public end; var MainForm: TMainForm; implementation uses ACCES32,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; CountsList: array[0..7] of TLabel; OffsetEditArray: array[0..7] of TEdit; DifferentialCheckArray: array[0..7] of TCheckBox; 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); begin if (InPortB($61) = $AA55) then begin Application.MessageBox('ACCESNT.SYS not detected. Please copy ACCESNT.SYS into [NT]/system32/drivers and re-run this sample.', 'Warning', IDOK); end; StuffedMemo := TStringList.Create; Commo := TInterCom.Create(self); Commo.Parent := Self; Commo.ReadAsLines := True; Commo.OnReadCom := ReadCom; BaudCombo.ItemIndex := 3; CountsList[0] := CountsLabel0; CountsList[1] := CountsLabel1; CountsList[2] := CountsLabel2; CountsList[3] := CountsLabel3; CountsList[4] := CountsLabel4; CountsList[5] := CountsLabel5; CountsList[6] := CountsLabel6; CountsList[7] := CountsLabel7; OffsetEditArray[0] := OffsetEdit0; OffsetEditArray[1] := OffsetEdit1; OffsetEditArray[2] := OffsetEdit2; OffsetEditArray[3] := OffsetEdit3; OffsetEditArray[4] := OffsetEdit4; OffsetEditArray[5] := OffsetEdit5; OffsetEditArray[6] := OffsetEdit6; OffsetEditArray[7] := OffsetEdit7; DifferentialCheckArray[0] := DifferentialCheck0; DifferentialCheckArray[1] := DifferentialCheck1; DifferentialCheckArray[2] := DifferentialCheck2; DifferentialCheckArray[3] := DifferentialCheck3; DifferentialCheckArray[4] := DifferentialCheck4; DifferentialCheckArray[5] := DifferentialCheck5; DifferentialCheckArray[6] := DifferentialCheck6; DifferentialCheckArray[7] := DifferentialCheck7; Channel := 0; Watch := 0; end; procedure TMainForm.ConnectButtonClick(Sender: TObject); var Msg: PChar; begin if ConnectButton.Caption = 'Disconnect' then begin //If disconnecting Begin CountTimer.Enabled := False; AcquireButton.Tag := 0; AcquireButton.Caption := 'Start Acquire'; 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; 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; 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 Memo1.Lines.Add('H'); 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 Memo1.Lines.Add('V'); 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 Memo1.Lines.Add('N'); 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 Memo1.Lines.Add(SendEdit.Text); 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'); Memo1.Lines.Add('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)); Memo1.Lines.Add('!' + 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'); Memo1.Lines.Add('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.ExitButtonClick(Sender: TObject); begin Close; end; procedure TMainForm.CountTimerTimer(Sender: TObject); var x, z: integer; y: Cardinal; Counts: Smallint; WriteString, Test: String; begin CountTimer.Enabled := False; OutputToRich := False; Y := GetTickCount + 500; GlobalComValue := ''; if (DifferentialCheckArray[channel].Checked) then WriteString := 'A0' + IntToHex(channel, 1) + '0' else WriteString := 'A0' + IntToHex(channel, 1) + '8'; WriteString := WriteString + OffsetEditArray[channel].Text; Commo.WriteComLine(WriteString); Memo1.Lines.Add('> ' + WriteString); Repeat Application.ProcessMessages; Until (GlobalComValue <> '') or (GetTickCount > y); if GlobalComValue <> '' then begin z := 0; Test := GlobalComValue; Memo1.Lines.Add('<' + Test); For X := 1 to Length(Test) do if Test[x] in ['0'..'9','A'..'F'] then inc(z); if not(Length(Test) = z) then inc(Watch) else begin Watch := 0; Counts := StrToInt('$' + Test); CountsList[Channel].Caption := IntToStr(Counts); StuffedMemo.Add('Ch. ' + IntToStr(channel) + ',' + DateTimeToStr(now) + ',' + IntToStr(Counts)); //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; procedure TMainForm.OffsetEditKeyPress(Sender: TObject; var Key: Char); begin Key := UpCase(Key); if not(Key in [#8,'0'..'9', 'A'..'F']) then Key := #0; end; end.