[Gambas-user] Gambas2 USB detect !!??

Rob sourceforge-raindog2 at ...94...
Fri Jan 2 20:07:44 CET 2009


On Friday 02 January 2009 12:54, Biggy UAC wrote:
> in my application i want to detect usb flash drive when pluged/unpluged
> and display in a textbox usb drive letter.
> how to detect usb drive stick plug/unplug in gambas2 ??

Linux doesn't have drive letters, but you should be able to get the device 
name (e.g. /dev/sde) in a couple of different ways.

The correct way to do it is to set your program up as a hotplug agent for 
USB drives using udev (probably putting a file in /etc/udev/rules.d).  But 
doing that may screw up your users' normal hotplug functionality (i.e. KDE 
or GNOME asking the user what they want to do with the drive.)  Google for 
udev hotplug for more information on this, because I've never done it 
myself.  Also, on older systems the devfs system was used rather than udev 
and you'd need to deal with /etc/hotplug/usb.usermap (I think.)

Another way, less intrusive but by no means foolproof, is to have a process 
running in your Gambas program, "tail -f /var/log/messages" 
or "tail -f /var/log/syslog".  Here is what appeared in /var/log/messages 
last night on one of my Mandriva boxes when I attached my Archos hard disk 
jukebox:

Jan  1 20:46:59 raindog2 kernel: usb 5-5: new high speed USB device using 
ehci_hcd and address 7
Jan  1 20:46:59 raindog2 kernel: usb 5-5: configuration #1 chosen from 1 
choice
Jan  1 20:46:59 raindog2 kernel: scsi7 : SCSI emulation for USB Mass 
Storage devices
Jan  1 20:47:04 raindog2 kernel:   Vendor: Archos    Model: PC Hard Drive     
Rev: 0316
Jan  1 20:47:04 raindog2 kernel:   Type:   Direct-Access                      
ANSI SCSI revision: 02
Jan  1 20:47:04 raindog2 kernel: SCSI device sdh: 312175080 512-byte hdwr 
sectors (159834 MB)
Jan  1 20:47:04 raindog2 kernel: sdh: Write Protect is off
Jan  1 20:47:04 raindog2 kernel: sdh: assuming drive cache: write through
Jan  1 20:47:04 raindog2 kernel: SCSI device sdh: 312175080 512-byte hdwr 
sectors (159834 MB)
Jan  1 20:47:04 raindog2 kernel: sdh: Write Protect is off
Jan  1 20:47:04 raindog2 kernel: sdh: assuming drive cache: write through
Jan  1 20:47:04 raindog2 kernel:  sdh: sdh1
Jan  1 20:47:04 raindog2 kernel: sd 7:0:0:0: Attached scsi removable disk 
sdh

If you look for the words "Attached scsi removable disk" in the output of 
that tail -f, the next word in that line should be the device name of the 
drive.  I haven't hooked any USB drives up to my Ubuntu server since I 
upgraded so I don't know if every distro has logs that look like that, or 
just Mandriva.

Rob




More information about the User mailing list