Recent

Author Topic: kernel 6.8 and checking serial ports - failure to match driver name  (Read 5871 times)

robert rozee

  • Full Member
  • ***
  • Posts: 204
Re: kernel 6.8 and checking serial ports - failure to match driver name
« Reply #45 on: December 19, 2024, 12:35:30 pm »
revised code, making use of fpAccess() to check we have RW access to each device:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses SysUtils, BaseUnix, TermIO;
  4.  
  5. var DeviceName:string;
  6.             SR:TSearchRec;
  7.             FD:longint;
  8. //        tios:TermIOS;
  9.       n, c, PT:integer;
  10.  
  11. begin
  12.   n:=0;
  13.   c:=0;
  14. //if FindFirst('/dev/tty*', faAnyFile , SR) = 0 then                           // original (only finds tty* devices)
  15.   if FindFirst('/sys/class/tty/*', faAnyFile , SR) = 0 then                    // alternative: should ALSO find any rfcomm*
  16.   repeat
  17.     DeviceName:=SR.Name;
  18.  
  19.     if (DeviceName<>'.') and (DeviceName<>'..') then                           // exclude '.' and '..'
  20.     if fpAccess('/dev/'+DeviceName, R_OK+W_OK)=0 then                          // exclude devices we have NO ACCESS to
  21.     if FileExists('/sys/class/tty/'+DeviceName+'/device/driver')  or           // this suffices with FPC prior to 3.20
  22.        DirectoryExists('/sys/class/tty/'+DeviceName+'/device/driver')  then    // from FPC 3.20 onwards we need this instead
  23.     begin
  24.       if FileExists('/sys/class/tty/'+DeviceName+'/type') then PT:=1           // port type 1 = fixed serial port
  25.                                                           else PT:=2;          // port type 2 = removable/USB
  26.       if PT=1 then
  27.       begin
  28.         FD:=fpOpen('/dev/'+DeviceName, O_RDWR or O_NONBLOCK or O_NOCTTY);
  29.         if FD<0 then PT:=0 else                                                // -1, couldn't open /dev entry -> failure!
  30.         begin
  31.           if IsATTY(FD)=0 then PT:=0                                           // IsATTY (and TTYname) use ioctl/TCGETS
  32.                           else inc(n);                                         // count number of fixed serial ports
  33. //        if fpIOCtl(FD,TCGETS,@tios)<>0 then PT:=0                            // TCGETS: on success 0 is returned,
  34. //                                       else inc(n);                          //         on error -1 is returned
  35.           fpClose(FD)
  36.         end
  37.       end;
  38.  
  39.       if PT<>0 then inc(c);                                                    // count total number of ports
  40.       case PT of 1:writeln('* ', DeviceName);                                  // fixed serial port, verified as real
  41.                  2:writeln('  ', DeviceName)                                   // removable (USB) serial port
  42.       end
  43.     end
  44.   until FindNext(SR) <> 0;
  45.  
  46.   if n<>0 then writeln('(', n, ' fixed devices)');
  47.   if c=0 then writeln('no serial ports found');
  48.   FindClose(SR)
  49. end.

addendum 25-dec-2025: changed search from "/dev/tty*" to "/sys/class/tty/*". in theory this should now also catch /dev/rfcomm* (serial over bluetooth) devices.


cheers,
rob   :-)
« Last Edit: December 24, 2024, 02:22:22 pm by robert rozee »

 

TinyPortal © 2005-2018