Differenze tra le versioni di "Ottenere i valori di baud rates del flusso in entrata ed in uscita dei file-device terminali"

Da Gambas-it.org - Wikipedia.
Riga 16: Riga 16:
 
   Dim baud As New Collection
 
   Dim baud As New Collection
 
   Dim dev As String
 
   Dim dev As String
 +
 +
  With baud
 +
    .Add(0, "0")
 +
    .Add(600, "10")
 +
    .Add(1200, "11")
 +
    .Add(1800, "12")
 +
    .Add(2400, "13")
 +
    .Add(4800, "14")
 +
    .Add(9600, "15")
 +
    .Add(19200, "16")
 +
    .Add(38400, "17")
 +
    .Add(57600, "10001")
 +
    .Add(115200, "10002")
 +
    .Add(230400, "10003")
 +
    .Add(460800, "10004")
 +
    .Add(500000, "10005")
 +
  End With
 +
 
 +
<FONT Color=gray>' ''Verifichamo per esempio il file-device "/dev/tty0"''</font>
 +
  dev = "/dev/tty0"
 
    
 
    
  With baud
 
    .Add(0, "0")
 
    .Add(600, "10")
 
    .Add(1200, "11")
 
    .Add(1800, "12")
 
    .Add(2400, "13")
 
    .Add(4800, "14")
 
    .Add(9600, "15")
 
    .Add(19200, "16")
 
    .Add(38400, "17")
 
    .Add(57600, "10001")
 
    .Add(115200, "10002")
 
    .Add(230400, "10003")
 
    .Add(460800, "10004")
 
    .Add(500000, "10005")
 
  End With
 
   
 
<FONT Color=gray>' ''Verifichamo per esempio il file-device "/dev/tty0"''</font>
 
  dev = "/dev/tty0"
 
 
 
 
  <FONT Color=gray>' ''Se il file-device è protetto, allora gli attribuiamo i permessi più ampi.''
 
  <FONT Color=gray>' ''Se il file-device è protetto, allora gli attribuiamo i permessi più ampi.''
 
  ' ''Nella riga di comando seguente si dovrà sostituire la parola "PASSWORD" con la password dell'utente:''</font>
 
  ' ''Nella riga di comando seguente si dovrà sostituire la parola "PASSWORD" con la password dell'utente:''</font>
  If Access(dev, gb.Read) = False Then Shell "echo <FONT Color=gray>PASSWORD</font> | sudo -S chmod 666 " & dev Wait
+
  If Access(dev, gb.Read) = False Then Shell "echo <FONT Color=gray>PASSWORD</font> | sudo -S chmod 666 " & dev Wait
 +
 
 +
  fl = Open dev For Read
 
    
 
    
  fl = Open dev For Read
+
  termios = Alloc(SizeOf(gb.Byte), 60)
   
 
  termios = Alloc(60)
 
 
 
  <FONT Color=#B22222>tcgetattr</font>(fl.Handle, termios)
 
  Print "\n"; dev; ":\n"
 
  Print "Baud rate in Entrata: ", baud[Int@(termios + 52)];; "baud"
 
  Print "Baud rate in Uscita:  ", baud[Int@(termios + 56)];; "baud"
 
 
 
  Free(termios)
 
  fl.Close
 
 
    
 
    
 +
  <FONT Color=#B22222>tcgetattr</font>(fl.Handle, termios)
 +
  Print "\n"; dev; ":\n"
 +
  Print "Baud rate in Entrata: ", baud[Int@(termios + 52)];; "baud"
 +
  Print "Baud rate in Uscita:  ", baud[Int@(termios + 56)];; "baud"
 +
 
 +
  Free(termios)
 +
  fl.Close
 +
 
 
  '''End'''
 
  '''End'''
 
  
  

Versione delle 23:16, 26 dic 2021

Per ottenere i valori di baud rates del flusso in entrata ed in uscita dei file-device terminali, è possibile utilizzare la funzione tcgetattr() della libreria dinamica condivisa libc.6.so, che pertanto dovrà essere specificatamente richiamata in Gambas.


Mostriamo un esempio pratico:

Library "libc:6"

' int tcgetattr (int __fd, struct termios *__termios_p)
' Put the state of FD into *TERMIOS_P.
Private Extern tcgetattr(__fd As Integer, __termios_p As Pointer) As Integer


Public Sub Main()
 
 Dim fl As File
 Dim termios As Pointer
 Dim baud As New Collection
 Dim dev As String

 With baud
   .Add(0, "0")
   .Add(600, "10")
   .Add(1200, "11")
   .Add(1800, "12")
   .Add(2400, "13")
   .Add(4800, "14")
   .Add(9600, "15")
   .Add(19200, "16")
   .Add(38400, "17")
   .Add(57600, "10001")
   .Add(115200, "10002")
   .Add(230400, "10003")
   .Add(460800, "10004")
   .Add(500000, "10005")
 End With
  
' Verifichamo per esempio il file-device "/dev/tty0"
 dev = "/dev/tty0"
 
' Se il file-device è protetto, allora gli attribuiamo i permessi più ampi.
' Nella riga di comando seguente si dovrà sostituire la parola "PASSWORD" con la password dell'utente:
 If Access(dev, gb.Read) = False Then Shell "echo PASSWORD | sudo -S chmod 666 " & dev Wait
 
 fl = Open dev For Read
  
 termios = Alloc(SizeOf(gb.Byte), 60)
  
 tcgetattr(fl.Handle, termios)
 Print "\n"; dev; ":\n"
 Print "Baud rate in Entrata: ", baud[Int@(termios + 52)];; "baud"
 Print "Baud rate in Uscita:  ", baud[Int@(termios + 56)];; "baud"
 
 Free(termios)
 fl.Close
 
End


Riferimenti