Differenze tra le versioni di "Conoscere numero seriale, modello ed altre informazioni su hard-disk SATA con la funzione esterna ioctl()"

Da Gambas-it.org - Wikipedia.
Riga 19: Riga 19:
 
   Dim fl As File
 
   Dim fl As File
 
   Dim err As Integer
 
   Dim err As Integer
    
+
 
+
   fl = Open "/dev/sda" For Read
    fl = Open "/dev/sda" For Read
 
 
      
 
      
    hd = Alloc(512)
+
  hd = Alloc(512)
 
    
 
    
    err = ioctl(fl.Handle, HDIO_GET_IDENTITY, hd)
+
  err = ioctl(fl.Handle, HDIO_GET_IDENTITY, hd)
    If err < 0 Then Error.Raise("Errore alla funzione 'ioctl()' !")
+
  If err < 0 Then Error.Raise("Errore alla funzione 'ioctl()' !")
 
      
 
      
    Print "Cilindri:      "; Short@(hd + 2)
+
  Print "Cilindri:      "; Short@(hd + 2)
    Print "Heads:          "; Short@(hd + 6)
+
  Print "Heads:          "; Short@(hd + 6)
    Print "Settori:        "; Short@(hd + 12)
+
  Print "Settori:        "; Short@(hd + 12)
    Print "Numero seriale: "; String@(hd + 20)
+
  Print "Num. seriale:   "; Trim(Left(String@(hd + 20), 20))
    Print "Firmware rev.:  "; String@(hd + 46)
+
  Print "Firmware rev.:  "; Left(String@(hd + 46), 8)
    Print "Modello:        "; String@(hd + 54)
+
  Print "Modello:        "; Left(String@(hd + 54), 40)
 
      
 
      
    Free(hd)
+
  Free(hd)
    fl.Close
+
  fl.Close
 
    
 
    
 
  '''End'''
 
  '''End'''

Versione delle 16:52, 10 ago 2020

Utilizzando la funzione esterna ioctl(), e facendo riferimento alla Struttura esterna hd_driveid del file header /usr/include/linux/hdreg.h, è possibile ottenere alcune informazioni, come numero seriale, modello etc., rlative a un hard-disk SATA.

Sarà necessario richiamare nel progetto Gambas la libreria dinamica condivisa: "libc.so.6"


Mostriamo di seguito un esempio pratico, avendo cura di eliminare la protezione al file-device "/dev/sda":

Library "libc:6"

Private Const HDIO_GET_IDENTITY As Integer = &030D

' int ioctl (int __fd, unsigned long int __request, ...)
' Perform the I/O control operation specified by REQUEST on FD.
Private Extern ioctl(__fd As Integer, __request As Long, drived As Pointer) As Integer 'Hd_driveid) As Integer


Public Sub Main()
 
 Dim hd As Pointer
 Dim fl As File
 Dim err As Integer
  
 fl = Open "/dev/sda" For Read
   
 hd = Alloc(512)
  
 err = ioctl(fl.Handle, HDIO_GET_IDENTITY, hd)
 If err < 0 Then Error.Raise("Errore alla funzione 'ioctl()' !")
   
 Print "Cilindri:       "; Short@(hd + 2)
 Print "Heads:          "; Short@(hd + 6)
 Print "Settori:        "; Short@(hd + 12)
 Print "Num. seriale:   "; Trim(Left(String@(hd + 20), 20))
 Print "Firmware rev.:  "; Left(String@(hd + 46), 8)
 Print "Modello:        "; Left(String@(hd + 54), 40)
   
 Free(hd)
 fl.Close
  
End