Connecting A USB Drive

Finding It

Note that “mydrivename” can be anything you want it to be, its the name you will use to connect to it

string CommandResult;
int DriveMounted = 0;
	
	CommandResult = do_console_command_get_result((char*)"ls -l /dev/disk/by-uuid/");
	//cout << "Drive list result: " << CommandResult << endl;
	if (CommandResult.find("sda1") != std::string::npos)
	{
		//----- USB DRIVE FOUND -----
		//cout << "USB Device SDA1 Found" << endl;

		//----- MOUNT IT -----
		CommandResult = do_console_command_get_result((char*)"sudo mkdir /media/mydrivename 2>&1");			//" 2>&1" so we get the error output if the command fails
		CommandResult += do_console_command_get_result((char*)"sudo chown -R pi:pi /media/mydrivename 2>&1");
		CommandResult += do_console_command_get_result((char*)"sudo mount /dev/sda1 /media/mydrivename -o uid=pi,gid=pi 2>&1");
		DriveMounted = 1;
		if (CommandResult.find("already mounted") != std::string::npos)	//Check for error
			DriveMounted = 0;
	}
	else if (CommandResult.find("sdb1") != std::string::npos)
	{
		//----- USB DRIVE FOUND -----
		//cout << "USB Device SDB1 Found" << endl;

		//----- MOUNT IT -----
		CommandResult = do_console_command_get_result((char*)"sudo mkdir /media/mydrivename 2>&1");
		CommandResult += do_console_command_get_result((char*)"sudo chown -R pi:pi /media/mydrivename 2>&1");
		CommandResult += do_console_command_get_result((char*)"sudo mount /dev/sdb1 /media/mydrivename -o uid=pi,gid=pi 2>&1");
		DriveMounted = 1;
		if (CommandResult.find("already mounted") != std::string::npos)	//Check for error
			DriveMounted = 0;
	}
	else if (CommandResult.find("sdc1") != std::string::npos)
	{
		//----- USB DRIVE FOUND -----
		//cout << "USB Device SDC1 Found" << endl;

		//----- MOUNT IT -----
		CommandResult = do_console_command_get_result((char*)"sudo mkdir /media/mydrivename 2>&1");
		CommandResult += do_console_command_get_result((char*)"sudo chown -R pi:pi /media/mydrivename 2>&1");
		CommandResult += do_console_command_get_result((char*)"sudo mount /dev/sdc1 /media/mydrivename -o uid=pi,gid=pi 2>&1");
		DriveMounted = 1;
		if (CommandResult.find("already mounted") != std::string::npos)	//Check for error
			DriveMounted = 0;
	}
	if (!DriveMounted)
	{
		//----- NO USB DRIVE FOUND -----
		
		return;
	}
	
	string my_file_path = "/media/mydrivename/hello.txt";
Releasing It After Use
	CommandResult = do_console_command_get_result((char*)"sudo umount /media/mydrivename 2>&1");
	if (CommandResult.find("busy") != std::string::npos)
	{
		bcm2835_delay(250);
		CommandResult = do_console_command_get_result((char*)"sudo umount /media/mydrivename 2>&1");
	}
	CommandResult = do_console_command_get_result((char*)"sudo rm -r /media/mydrivename");		//Remove the directory ready for next time	
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *