Recent

Author Topic: Tchart with log scale and needed real time on X-axis  (Read 10634 times)

wp

  • Hero Member
  • *****
  • Posts: 13353
Re: Tchart with log scale and needed real time on X-axis
« Reply #30 on: January 08, 2024, 07:20:49 pm »
Don't you use some kind of ini file to store parameters such as form size and positions, user entries etc.? That "serial" number could go in there as well.

Code: Pascal  [Select][+][-]
  1. uses
  2.   IniFiles;
  3.  
  4. function CreateIniFile: TCustomIniFile;
  5. var
  6.   fn: String;
  7. begin
  8.   fn := ChangeFileExt(GetAppConfigFile(false), '.ini');;
  9.   Result := TIniFile.Create(fn);
  10. end;
  11.  
  12. procedure TForm1.ReadFromIni;  // call this in the form's OnCreate or OnActivate. Have the caller check the serial.
  13. var
  14.   ini: TCustomIniFile;
  15. begin
  16.   ini := CreateIniFile;
  17.   try
  18.     SerialNumber := ini.ReadInteger('Settings', 'Serial', 0);
  19.     // other settings here, too...
  20.   finally
  21.     ini.Free;
  22.   end;
  23. end;
  24.  
  25. procedure TForm1.WriteToIni;   // Call this in the OnDestroy event of the form
  26. var
  27.   ini: TCustomIniFile;
  28. begin
  29.   ini := CreateIniFile
  30.   try
  31.     ini.WriteInteger('Settings', 'Serial', SerialNumber);
  32.     // write other settings here, too
  33.   finally
  34.     ini.Free;
  35.   end;
  36. end;

Similarly you could also write to XML or JSON files, have a look at TXMLPropStorage (https://lazarus-ccr.sourceforge.io/docs/lcl/xmlpropstorage/txmlpropstorage.html, https://wiki.lazarus.freepascal.org/TXMLPropStorage), or TJSONPropStorage (https://lazarus-ccr.sourceforge.io/docs/lcl/jsonpropstorage/tjsonpropstorage.html, https://wiki.lazarus.freepascal.org/TJsonPropStorage).

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #31 on: January 08, 2024, 10:37:45 pm »
Hi WP,

Thank you!

There a lot of variables used in my Teensy such as gauge constants or linear inputs.

I can adjust all parameters from the pumpdown app however I store
all settings directly in the Teensy EEprom that’s more safe even if Pumpdown program
Is not running the vacuum meter is still alive.

The meter has a Oled character 256x64 display with setpoints also stored in EEprom, also for
gas constants for helium / argon / nitrogen.

I will post a nice picture when the project is done including hardware.


Thanks again without people like you my meter was not that sophisticated!

But this will be a one of a kind!


Best regards,

Johan


wp

  • Hero Member
  • *****
  • Posts: 13353
Re: Tchart with log scale and needed real time on X-axis
« Reply #32 on: January 08, 2024, 11:56:21 pm »
The meter has a Oled character 256x64 display with setpoints also stored in EEprom, also for
gas constants for helium / argon / nitrogen.
BTW, if you plan to simulate this display in your GUI: there is a nice TLCDDisplay component in the industrial package: https://wiki.freepascal.org/Industrial#TLCDDisplay. Use Online-Package-Manager to install it.

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #33 on: January 09, 2024, 09:44:52 am »
Hi WP,

I am not get this working I put the two procedures in my Unit 1....

unit1.pas(228,18) Error: method identifier expected
unit1.pas(234,5) Error: Identifier not found "SerialNumber"
unit1.pas(241,18) Error: method identifier expected
unit1.pas(246,3) Error: Syntax error, ";" expected but "TRY" found

What did I wrong .... perhaps put the code in your Pumpdown as example?

Thanks,
Johan

Code: Pascal  [Select][+][-]
  1. function CreateIniFile: TCustomIniFile;
  2. var
  3.   fn: String;
  4. begin
  5.   fn := ChangeFileExt(GetAppConfigFile(false), '.ini');;
  6.   Result := TIniFile.Create(fn);
  7. end;
  8. procedure TForm1.ReadFromIni;  // call this in the form's OnCreate or OnActivate. Have the caller check the serial.
  9. var
  10.   ini: TCustomIniFile;
  11. begin
  12.   ini := CreateIniFile;
  13.   try
  14.     SerialNumber := ini.ReadInteger('Settings', 'Serial', 0);
  15.     // other settings here, too...
  16.   finally
  17.     ini.Free;
  18.   end;
  19. end;
  20.  
  21. procedure TForm1.WriteToIni;   // Call this in the OnDestroy event of the form
  22. var
  23.   ini: TCustomIniFile;
  24. begin
  25.   ini := CreateIniFile
  26.   try
  27.     ini.WriteInteger('Settings', 'Serial', SerialNumber);
  28.     // write other settings here, too
  29.   finally
  30.     ini.Free;
  31.   end;
  32. end;                                      



dseligo

  • Hero Member
  • *****
  • Posts: 1653
Re: Tchart with log scale and needed real time on X-axis
« Reply #34 on: January 09, 2024, 09:51:56 am »
Hi WP,

I am not get this working I put the two procedures in my Unit 1....

These procedures are methods, part of TForm1. Did you declare them in TForm1 class?
Try this: place cursor on one of this procedures and press Shift + Ctrl + C

Quote
unit1.pas(228,18) Error: method identifier expected
unit1.pas(234,5) Error: Identifier not found "SerialNumber"
unit1.pas(241,18) Error: method identifier expected
unit1.pas(246,3) Error: Syntax error, ";" expected but "TRY" found

What did I wrong .... perhaps put the code in your Pumpdown as example?

Thanks,
Johan

Code: Pascal  [Select][+][-]
  1. function CreateIniFile: TCustomIniFile;
  2. var
  3.   fn: String;
  4. begin
  5.   fn := ChangeFileExt(GetAppConfigFile(false), '.ini');;
  6.   Result := TIniFile.Create(fn);
  7. end;
  8. procedure TForm1.ReadFromIni;  // call this in the form's OnCreate or OnActivate. Have the caller check the serial.
  9. var
  10.   ini: TCustomIniFile;
  11. begin
  12.   ini := CreateIniFile;
  13.   try
  14.     SerialNumber := ini.ReadInteger('Settings', 'Serial', 0);
  15.     // other settings here, too...
  16.   finally
  17.     ini.Free;
  18.   end;
  19. end;
  20.  
  21. procedure TForm1.WriteToIni;   // Call this in the OnDestroy event of the form
  22. var
  23.   ini: TCustomIniFile;
  24. begin
  25.   ini := CreateIniFile
  26.   try
  27.     ini.WriteInteger('Settings', 'Serial', SerialNumber);
  28.     // write other settings here, too
  29.   finally
  30.     ini.Free;
  31.   end;
  32. end;                                      

You are missing semicolon in line 25.

And error about SerialNumber: where did you declare this variable?

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #35 on: January 09, 2024, 10:04:36 am »
Hi WP,

No errors after compiling good ...... call this in the form's OnCreate or OnActivate. Have the caller check the serial this is what I not understand...
Best,
Johan

wp

  • Hero Member
  • *****
  • Posts: 13353
Re: Tchart with log scale and needed real time on X-axis
« Reply #36 on: January 09, 2024, 10:42:37 am »
My problem is that I don't fully understand what's about this "serial number" (I don't even know whether it's a serial number at all...).

In the attached project is a modification of the previous "pumpdown" demo; I am assuming that there is a "password" which must be entered to run the application. When the application runs for the very first time the password is defined. But note that this is only a very low security level because the password is stored in the ini file, and an "intruder" can reset the password by deleting the ini file (and define his own and lock YOU out of the application)...

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #37 on: January 09, 2024, 12:11:46 pm »
Hi WP,

I think and you have right ...do not look at the serial number of the Teensy but use a password for the application.

I tested yours and this works I only wondering where the ini file is stored?
And is this a safe way ?.... so as you told is there than a better solution? so that others not can run the application

Best and thanks,
Johan


Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #38 on: January 09, 2024, 12:53:14 pm »
Found the ini file, can we protect our programs a bit better instead of a readable ini file ?

Best regards,
Johan

wp

  • Hero Member
  • *****
  • Posts: 13353
Re: Tchart with log scale and needed real time on X-axis
« Reply #39 on: January 09, 2024, 05:54:11 pm »
Hmm, I am not a security expert. Don't blame me if I'm giving you bad advice now...

First of all, do you really need a password? Don't you trust your collegues? Having to enter a password for no good reason is annoying. And if you have curious collegues just the presence of a password may trigger them to find a way how to by-pass it...

Anyway, let's assume: you need a password.

The ini file is stored in the directory returned by GetAppConfigFile. In a "normal PC", this directory is in your home directory, and another user does not have access to this directory - this is rather safe.

But: probably the PC running this pump system is accessible to all employees/students/etc working in that laboratory, and there is a "general" account, nobody logs in under his own ID. So - this argument is not very strong...

You could store a hash of the password, rather than the password itself. When the user enters the password, the hash is calculated and compared with the stored hash (rather than comparing the passwords directly). Example:
Code: Pascal  [Select][+][-]
  1. uses
  2.   md5;
  3.  
  4. function CalcPasswordHash(APassword: String): String;
  5. var
  6.   md5Digest: TMDDigest;
  7. begin
  8.   md5Digest := MD5String(APassword);
  9.   Result := md5Print(md5Digest);
  10. end;
  11.  
  12. procedure TForm1.FormCreate(Sender: TObject);
  13. var
  14.   i: Integer;
  15.   pwd: String;
  16. begin
  17.   // Application called for the first time --> define the password
  18.   if not FileExists(CalcIniFileName) then
  19.   begin
  20.     pwd := PasswordBox('Define password', 'Define the password that a user will need to run this application');
  21.     FPassword := CalcPasswordHash(pwd);
  22.     WriteToIni(true);
  23.     FPassword := '';
  24.   end;
  25.  
  26.   ReadFromIni;
  27.   pwd := PasswordBox('Enter password', 'Password to run this application');
  28.   if FPassword <> CalcPasswordHash(pwd) then
  29.   begin
  30.     MessageDlg('Incorrect password. Terminating...', mtError, [mbOK], 0);
  31.     Application.Terminate;
  32.   end;
  33. ...

About the risk of deleting the ini file: Rewrite my code above such that the application does not start when
/1/ the entered password is not correct (like before), or
/2/ the ini file is not found.

For this to work, you must have a second program which only has the purpose to create an ini file before the first start of the application, and, of course, it must define the password and write its hash to the ini file. This tool should only be accessible to you (and trusted collegues), do not store it on the same machine on which the pumpdown program runs.

OK... Still, a clever collegue may notice that the ini file contains an md5 hash of the password (from its length). He could write a tool like yours and create an md5 hash for a new password, copy it into the ini file and now could run the pump although he is not allowed to... To make this hack more difficult, you could split the ini file in two parts: one with the general settings (window sizes, positions, etc), stored at the current place, and one with the password hash only stored at a different place: ideally on a server so that only you have write access, or in the Windows registry key HK_LocalMachine to which only admins have write access (*), or (well... "security by obscurity"...) in some other directory and renamed so that it is not obvious that it belongs to the pumpdown program.

(*) Hopefully, the lab PC running the pump is not configured to provide admin rights to all users...
« Last Edit: January 09, 2024, 06:05:48 pm by wp »

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #40 on: January 09, 2024, 07:05:21 pm »
Dear WP, group,

I managed  to protect my hardware in combination with the Pumpdown app.

Information: I look at the serial number of my Teensy 4.0 MCU and send this to my application when the serial match the given serial number the program will run otherwise it will terminated.

The INI file information with serial number is in this case public that is not a problem at all because most important that other people do not know the hardware serial of the Teensy  and so it is protected in a good way.

Tested and works awesome, thank you WP for helping!

This was exactly what I needed.

Best,
Johan

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #41 on: January 09, 2024, 07:16:26 pm »
I have several meters I don't like that students exchange a certain meter to an other place / laboratorium  .... that's the reason I made a match between software and hardware see it as a package.

Best,
Johan


wp

  • Hero Member
  • *****
  • Posts: 13353
Re: Tchart with log scale and needed real time on X-axis
« Reply #42 on: January 09, 2024, 07:27:40 pm »
I understand. But what will you do when that meter becomes defective and needs to be replaced? The serial of the replacement meter will be different, so you'll have to recompile your application with the new serial. Hopefully, that future Lazarus version will be compatible - if not, you'll have to debug your application and get it up and running again - usually these things are happening immediately before a dead-line in your work group. Or you'll have left the group and are working somewhere else, and the collegue who took over that activity from you does not know how to use Lazarus?

I think there is no way but to provide a config file with the serial number somewhere

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #43 on: January 09, 2024, 07:38:43 pm »
Exactly true for this reason I made a log in screen to bypass the protected read serial number and readout the serial number direct.

When I change the Teensy 4.0 MCU  I only must log into a other form and shows me the actual Teensy 4 serial number.

Than I can add this number with a othet password protected login to set the serial number to the INI file.

This take me only 5 seconds to do than and there is no need to use other software to get the Teensy serial number.

best,
Johan

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #44 on: January 10, 2024, 12:50:05 pm »
Dear WP,

Protection is done:

Now I am sure that my meters are paired to the software.

Via Admin and SerialNr in the menu I read the harware serial number from my device (Teensy4) now I see the serial number and press Set,  this number is then stored in the INI file.

I think a perfect solution... thanks for all the help!!!!!!

See the small video as well.

Best regards,
Johan

 

TinyPortal © 2005-2018