<< to CrossControl homepage

Support & Service Center

How to detect an USB stick from an application

Printer-friendly versionPrinter-friendly versionPDF versionPDF version

The linux command ‘fdisk –l’ can be used to detect a newly inserted (connected) USB-stick on the CrossControl displays. Iside a Qt-application you could use the following code:

for (i = 0x61; i < (0x61 + MAX_NUM_DEVICES); i++)
     {     
          sprintf(cmd, "fdisk -l | grep /dev/sd%c", i);
          if (system(cmd) == 0)	// NOTE! 0(zero) means NO error!
          {    // Check if device is mounted anywhere
               sprintf(cmd, "mount | grep /dev/sd%c", i);
               if (system(cmd) == 0) 	// NOTE! 0(zero) means NO error!
               {    // Device is mounted
                    printLog("Found device /dev/sd%c, device is already mounted.\n");
                    // The USB is present! Use it as you wish!}

There is no event connected to this solution, so to be able to detect when an USB-stick inserted, you need to have a poll-function that continuously checks the device-list for new arrivals.

How Linux name their drives

The following information has been extracted from a Linux page on Internet. The inserted USB-stick will usually be given names like: sda1, sdb1, sdbc1, etc

Drive name

The drive names will always start with ‘sd’. From the beginning, this identified a SCSI device, 
but since the wildgrowth of USB (and other removable) data carriers it became a catch-all for 
any block device (another unix term; in this context, anything capable of carrying data) that 
was not already accessible via IDE. When SATA came around, the developers figured it would 
be much easier and much more convenient for everyone to add it into the existing framework 
rather than write a whole new framework. 

Drive letter

The letter immediately after sd signifies the order in which it was first found:
        a,b,c...z, Aa...Az... etc. 
(Not that there are many situations in the real world where more than 26 discrete block 
devices are on the same bus...). 

Drive number

Finally, the number after that signifies the partition on the device. Note that because of the 
rather haphazard way PCs handle partitioning there are only four "primary" partitions, so 
the numbering will be slightly off from the actual count. This is not a terrible problem as the 
main purpose for the naming scheme is to have a unique and recognizable identifier for 
each partition found in this manner... 

If the display unit has more than one USB port, the first USB-stick that is inserted (irrespective of the port used) will get the name 'sda etc. There is no event connected to this solution, so to be able to detect when an USB-stick inserted, you need to have a poll-function that continuously checks if a new USB has been inserted.

See the attached documents for more information about the handling of USB devices for the different display devices.


Environment and Versions: 
CCP XA/XS
CCP VC/VA
CCP VS
Category: 
whatever