[程序源码] 纯C语言写的自动识别U盘插入并放入文件

[复制链接]
3049|4
 楼主| shipeng1989 发表于 2021-9-9 16:07 | 显示全部楼层 |阅读模式
本帖最后由 shipeng1989 于 2021-9-9 16:22 编辑

最近有个需求:当插入U盘后自动放入指定文件。功能是很简单的但是无奈我只会单片机编程不懂上位机编程,写起来也算是废了不少周折不过最终还是写出来了并且达到了预期的效果。下面我把源代码分享出来方便大家量体裁衣各取所需:(用微软VC编译)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <io.h>
  4. #include <string.h>
  5. #include <windows.h>


  6. int main(void)
  7. {
  8.     FILE* fps = 0;
  9.     UINT32 counter1 = 0,counter2 = 0;
  10.     DWORD gld_res1 = 0, gld_res2 = 0;
  11.     char DiskPath[MAX_PATH] = "A:\\SP89X.bin\0";
  12.     char cmd1str[200] = "cmd.exe /c xcopy C:\\SP89123\\SP89X.bin ?:\\ /y /c /h /r /u";//修改复制到地址,注意转义字符
  13.     char cmd2str[200] = "cmd.exe /c xcopy C:\\SP89123\\SP89XW25Q16\\*.* ?:\\ /e /q /h /d";

  14.     while (1)
  15.     {
  16.         do { Sleep(100); gld_res1 = GetLogicalDrives(); }
  17.         while (gld_res1 == gld_res2); printf_s("\n"); cmd2str[33+11] = '?';
  18.         for (UINT16 i=0,j=0; i < 26 && (gld_res1>>i)!=0; i++)
  19.         {
  20.             if ((gld_res1 & (1 << i)) != 0 && (gld_res2 & (1 << i)) == 0)
  21.             {
  22.                 DiskPath[0] = 'A'+ i; DiskPath[3] = '\0';
  23.                 if (GetDriveTypeA(DiskPath) == DRIVE_REMOVABLE)//判断是否是可移动存储设备
  24.                 {
  25.                     cmd1str[27 + 11] = DiskPath[0];
  26.                     if (cmd1str[21 + 11] == '1' || cmd1str[21 + 11] == '2' || cmd1str[21 + 11] == '3')
  27.                     {
  28.                         cmd2str[21+11] = DiskPath[7]; cmd2str[33+11] = DiskPath[0]; counter2++;
  29.                         printf_s("%d.Copying "SP89%cW25Q16"\n", counter2, DiskPath[7]);
  30.                         system(cmd2str); break;
  31.                     }
  32.                     else
  33.                     {
  34.                         UINT16 k; DiskPath[3] = 'S';
  35.                         for (k = 0; k < 3; k++)
  36.                         {
  37.                             DiskPath[7] = '1' + k;
  38.                             if (0 == fopen_s(&fps, DiskPath, "rb"))break;
  39.                         }
  40.                         if (k < 3)
  41.                         {
  42.                             if (fps!=NULL)fclose(fps); cmd1str[21 + 11] = DiskPath[7]; counter1++;
  43.                             printf_s("%d.Copying "SP89%c.bin"\n", counter1, DiskPath[7]); system(cmd1str);
  44.                             if (cmd2str[33+11] != '?') i = j - 1;
  45.                         }
  46.                     }
  47.                     cmd2str[33+11] = DiskPath[0]; j = i;
  48.                 }
  49.             }
  50.         }
  51.         cmd1str[21 + 11] = 'X'; cmd1str[27 + 11] = cmd2str[33+11] = '?';
  52.         if (gld_res1 > gld_res2)
  53.         {
  54.             printf_s("Total Copied "SP89x.bin"   : %d.\n", counter1);
  55.             printf_s("Total Copied "SP89xW25Q16" : %d.\n", counter2);
  56.             printf_s("USB PORT DETECTING...");
  57.         }
  58.         else printf_s("=========================================================");
  59.         gld_res2 = gld_res1;
  60.     }
  61. }


 楼主| shipeng1989 发表于 2021-9-9 16:14 | 显示全部楼层
忘记说了我这个U盘是用STM32做的,有两个盘符一个盘用来更新固件,另一个盘用来读写SPI FLASH:W25Q16.在固件盘里面放有初始文件SP891.BIN可通过此文件判断SP89x.bin和SP89xW25Q16中的x值,x可能为1,2,3三个值
 楼主| shipeng1989 发表于 2021-9-9 16:18 | 显示全部楼层
有一个功能很好用:只识别新插入U盘,不识别僵尸U盘
goyhuan 发表于 2021-9-10 17:13 | 显示全部楼层
这是可执行文件吗?
 楼主| shipeng1989 发表于 2021-9-11 08:27 | 显示全部楼层
goyhuan 发表于 2021-9-10 17:13
这是可执行文件吗?

你用微软的VS编译后可生成exe文件
您需要登录后才可以回帖 登录 | 注册

本版积分规则

31

主题

141

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部