{"id":2846,"date":"2017-11-17T15:24:31","date_gmt":"2017-11-17T15:24:31","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2846"},"modified":"2019-05-23T11:10:26","modified_gmt":"2019-05-23T11:10:26","slug":"usb-drives-2","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/file-input-and-output\/usb-drives-2","title":{"rendered":"USB Drives"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Connecting A USB Drive <\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Finding It <\/h5>\n\n\n\n<p>Note that &#8220;mydrivename&#8221; can be anything you want it to be, its the name you will use to connect to it <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string CommandResult;\nint DriveMounted = 0;\n\t\n\tCommandResult = do_console_command_get_result((char*)\"ls -l \/dev\/disk\/by-uuid\/\");\n\t\/\/cout &lt;&lt; \"Drive list result: \" &lt;&lt; CommandResult &lt;&lt; endl;\n\tif (CommandResult.find(\"sda1\") != std::string::npos)\n\t{\n\t\t\/\/----- USB DRIVE FOUND -----\n\t\t\/\/cout &lt;&lt; \"USB Device SDA1 Found\" &lt;&lt; endl;\n\n\t\t\/\/----- MOUNT IT -----\n\t\tCommandResult = do_console_command_get_result((char*)\"sudo mkdir \/media\/mydrivename 2>&amp;1\");\t\t\t\/\/\" 2>&amp;1\" so we get the error output if the command fails\n\t\tCommandResult += do_console_command_get_result((char*)\"sudo chown -R pi:pi \/media\/mydrivename 2>&amp;1\");\n\t\tCommandResult += do_console_command_get_result((char*)\"sudo mount \/dev\/sda1 \/media\/mydrivename -o uid=pi,gid=pi 2>&amp;1\");\n\t\tDriveMounted = 1;\n\t\tif (CommandResult.find(\"already mounted\") != std::string::npos)\t\/\/Check for error\n\t\t\tDriveMounted = 0;\n\t}\n\telse if (CommandResult.find(\"sdb1\") != std::string::npos)\n\t{\n\t\t\/\/----- USB DRIVE FOUND -----\n\t\t\/\/cout &lt;&lt; \"USB Device SDB1 Found\" &lt;&lt; endl;\n\n\t\t\/\/----- MOUNT IT -----\n\t\tCommandResult = do_console_command_get_result((char*)\"sudo mkdir \/media\/mydrivename 2>&amp;1\");\n\t\tCommandResult += do_console_command_get_result((char*)\"sudo chown -R pi:pi \/media\/mydrivename 2>&amp;1\");\n\t\tCommandResult += do_console_command_get_result((char*)\"sudo mount \/dev\/sdb1 \/media\/mydrivename -o uid=pi,gid=pi 2>&amp;1\");\n\t\tDriveMounted = 1;\n\t\tif (CommandResult.find(\"already mounted\") != std::string::npos)\t\/\/Check for error\n\t\t\tDriveMounted = 0;\n\t}\n\telse if (CommandResult.find(\"sdc1\") != std::string::npos)\n\t{\n\t\t\/\/----- USB DRIVE FOUND -----\n\t\t\/\/cout &lt;&lt; \"USB Device SDC1 Found\" &lt;&lt; endl;\n\n\t\t\/\/----- MOUNT IT -----\n\t\tCommandResult = do_console_command_get_result((char*)\"sudo mkdir \/media\/mydrivename 2>&amp;1\");\n\t\tCommandResult += do_console_command_get_result((char*)\"sudo chown -R pi:pi \/media\/mydrivename 2>&amp;1\");\n\t\tCommandResult += do_console_command_get_result((char*)\"sudo mount \/dev\/sdc1 \/media\/mydrivename -o uid=pi,gid=pi 2>&amp;1\");\n\t\tDriveMounted = 1;\n\t\tif (CommandResult.find(\"already mounted\") != std::string::npos)\t\/\/Check for error\n\t\t\tDriveMounted = 0;\n\t}\n\tif (!DriveMounted)\n\t{\n\t\t\/\/----- NO USB DRIVE FOUND -----\n\t\t\n\t\treturn;\n\t}\n\t\n\tstring my_file_path = \"\/media\/mydrivename\/hello.txt\";<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Releasing It After Use <\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tCommandResult = do_console_command_get_result((char*)\"sudo umount \/media\/mydrivename 2>&amp;1\");\n\tif (CommandResult.find(\"busy\") != std::string::npos)\n\t{\n\t\tbcm2835_delay(250);\n\t\tCommandResult = do_console_command_get_result((char*)\"sudo umount \/media\/mydrivename 2>&amp;1\");\n\t}\n\tCommandResult = do_console_command_get_result((char*)\"sudo rm -r \/media\/mydrivename\");\t\t\/\/Remove the directory ready for next time\t<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Connecting A USB Drive Finding It Note that &#8220;mydrivename&#8221; can be anything you want it to be, its the name you will use to connect to it Releasing It After Use<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-2846","post","type-post","status-publish","format-standard","hentry","category-file-input-and-output"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2846"}],"version-history":[{"count":4,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2846\/revisions"}],"predecessor-version":[{"id":3088,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2846\/revisions\/3088"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}