Skip to content

Designing the software data logger for MASTECH MS8229 part2

Once setting up the synaser we are ready to add a TTimer in our project that :
1) Gets the code (GetCode)
2) Validates the parsed code (IsValidCode)
3) Parses the status from the code (ParseStatus)
4) Prints the code into the screen (PrintStatusInLabel)

//The timer !
procedure TFormMain.TimerTimer(Sender: TObject);
var y:double;
begin
  Application.ProcessMessages;
  GetCode();
  StatusBar.Panels.Items[1].Text := 'Code : ' + PrintCode();
  if IsValidCode then begin
    StatusBar.Panels.Items[1].Text := StatusBar.Panels.Items[1].Text +  ' [valid]';
    ParseStatus();
    LabelScreen.Caption := PrintStatusInLabel();

  end else begin
    StatusBar.Panels.Items[1].Text := StatusBar.Panels.Items[1].Text +  ' [invalid]';
  end;

  FormMain.Refresh;

  //Close the timer and disconnect if IsConnected is false
  if not IsConnected then begin
    Timer.Enabled:=false;
    Disconnect();
  end;
end;

The on connect / disconnect event is a OnClick TButton event.
We simply enable or disable the TTimer.
Notice that disabling the timer on this event can cause errors since the timer can be doing a reading operation.
So we set the global variable IsConnected to false and then in the TTimer event (worker) on a "secure zone" makes the disconnection.
This is a classic threading error for young players. Threads will be discussed in another series of lessons.
Right now look it this way : TTimer does a readingĀ  job.
Only TTimer knows when is done with the reading so i (external event) will simply inform the TTimer to disconnect when he finishes his reading and he can securely disconnect.

//On connect disconnect
procedure TFormMain.ButtonConnectDisconnectClick(Sender: TObject);
begin

  //Connect
  if ButtonConnectDisconnect.Caption = 'Connect' then begin

    Connect(EditPort.Text); //Connect at the port ...

    if IsConnected then begin
      ButtonConnectDisconnect.Caption:='Disconnect';
      Timer.Interval:=SpinEditEveryXMs.Value;
      Timer.Enabled:=true;
    end;

  //Disconnect
  end else if ButtonConnectDisconnect.Caption = 'Disconnect' then begin

    IsConnected := false; //this must stop the timer securely
    ButtonConnectDisconnect.Caption:='Connect';   

  end;
end;

 

The print status in label can be something like that :

function TFormMain.PrintStatusInLabel():string;
var stemp:string;
begin

  if My_Multimeter.Status.IsRS232 = true then
    stemp += ' [RS233] ';

  if My_Multimeter.Status.IsAuto = true then
    stemp += ' [AUTO] ';

  if My_Multimeter.Status.AC_DC_None = AC then
    stemp += ' [AC] '
  else if My_Multimeter.Status.AC_DC_None = DC then
    stemp += ' [DC] ';

  stemp += My_Multimeter.Status.ValuePrefix + ' ' + My_Multimeter.Status.UnitPrefix + My_Multimeter.Status.UnitValue;

  stemp += #13#10; //Break line

  if My_Multimeter.Status.IsDiode = true then
    stemp += ' DIODE ';

  if My_Multimeter.Status.IsSound = true then
    stemp += ' SOUND ';

  if My_Multimeter.Status.IsHold = true then
    stemp += ' HOLD ';

  if My_Multimeter.Status.IsRelative = true then
    stemp += ' REL ';

  if My_Multimeter.Status.IsBattery = true then
    stemp += ' BAT ';

  result := stemp;

end;

At this point you should be able to get the readings on your screen (LabelScreen.Caption)
Of course once reading the values you can store them into a ListView / csv file.
You can use your dedicated functions in order to do this job.
This step is really easy to do and it is not explained in this article.
In the next page we will see the plot graph.