Board logo

标题: [文件操作] 批处理能操作串口吗? [打印本页]

作者: zhifuz    时间: 2013-3-29 22:34     标题: 批处理能操作串口吗?

如题
想向串口1发送一个小的二进制文件,有什么命令能实现吗?

类似串口调试助手和超级终端。但我要自己设定间隔时间,自动上传。
作者: CrLf    时间: 2013-3-30 00:39

pudn 下载的 SerialCmd 源代码,共享者的介绍是:
命令行串口工具 这样就可以通过在Windows下用记事本编辑bat批处理文件我灵活的向串口发送和接收数据,简单点他就是一个命令行的串口调试助手,这样就支持bat比较灵活.可以批量向单片机或者ARM , DSP 发命令-Serial command-line tool can then be used in the Windows Notepad to edit the next bat batch file I am flexible to the serial port to send and receive data, a simple point, he is a serial command-line debugging assistants, so that support more flexible bat . Can bulk to the single-chip, or ARM, DSP order

gcc 编译通过,共享出来,免得你们没分下不了,里面定死了端口和文件,自己仿着改吧:
  1. /* sertrans.c */
  2. /* Transmits a file to another computer over a serial cable */
  3. /* Last modified: September 20, 2005 */
  4. /* http://www.gomorgan89.com */
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. /* Function to set up the serial port settings with the specified baud rate,
  10.    no parity, and one stop bit */
  11. void set_up_serial_port(HANDLE h, long baud);
  12. /* Function to print out usage */
  13. void usage(void);
  14. /* Function to get size of file */
  15. unsigned long get_file_size(char *file_name);
  16. /* Function to write the file to the serial port */
  17. void write_file_to_serial_port(HANDLE h, char *file_name, unsigned long file_size);
  18. char ConvertHexChar(char ch)
  19. {
  20. if((ch>='0')&&(ch<='9'))
  21. return ch-0x30;
  22. else if((ch>='A')&&(ch<='F'))
  23. return ch-'A'+10;
  24. else if((ch>='a')&&(ch<='f'))
  25. return ch-'a'+10;
  26. else return (-1);
  27. }
  28. int String2Hex(char * str, char* senddata)
  29. {
  30. int hexdata,lowhexdata;
  31. int hexdatalen=0;
  32. int len=strlen(str);
  33. int i;
  34. // senddata.SetSize(len/2);
  35. for(i=0;i<len;)
  36. {
  37. char lstr,hstr=str[i];
  38. if(hstr==' ')
  39. {
  40. i++;
  41. continue;
  42. }
  43. i++;
  44. if(i>=len)
  45. break;
  46. lstr=str[i];
  47. hexdata=ConvertHexChar(hstr);
  48. lowhexdata=ConvertHexChar(lstr);
  49. if((hexdata==16)||(lowhexdata==16))
  50. break;
  51. else
  52. hexdata=hexdata*16+lowhexdata;
  53. i++;
  54. senddata[hexdatalen]=(char)hexdata;
  55. hexdatalen++;
  56. }
  57. // senddata.SetSize(hexdatalen);
  58. return hexdatalen;
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. HANDLE serial_port; /* Handle to the serial port */
  63. long baud_rate = 9600; /* Specified baud rate */
  64. char port_name[] = "COM3:"; /* Name of the serial port */
  65. unsigned long file_size; /* File size to transmit in bytes */
  66. unsigned long bytes_written; /* Bytes written to serial port */
  67. unsigned long file_name_size; /* Size of file name */
  68. unsigned long bytes_read;
  69. char buffer[255]="Test data";
  70. char cmdbuffer[255]="";
  71. //if (argc>0)
  72. char cfg_file_name[]="c:\\testtesttest1231231retrewerhgdrffhgsdfsadfaesj.6dfy";
  73. cfg_file_name=argv[1];
  74. printf("%s",cfg_file_name[]);
  75. int databuffer;
  76. unsigned char lowByte,highByte;
  77. int i=0,counttmp=0;
  78. FILE *data_file;
  79. // do not use the original cmd check , here
  80. //leave blank
  81.    
  82. data_file = fopen(cfg_file_name, "rb");
  83. if (data_file == NULL)
  84. {
  85. fprintf(stderr, "Could not open file %s\n", cfg_file_name);
  86. exit(0);
  87. }
  88. i = (unsigned long)fread((void *)port_name, 1, 10, data_file);
  89. /* Open up a handle to the serial port */
  90. serial_port = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
  91. /* Make sure port was opened */
  92. if (serial_port == INVALID_HANDLE_VALUE)
  93. {
  94. fprintf(stderr, "Error opening port\n");
  95. CloseHandle(serial_port);
  96. exit(0);
  97. }
  98. /* Set up the serial port */
  99. set_up_serial_port(serial_port, baud_rate);
  100. // printf("%s opned!\n" ,port_name);
  101. // printf("argc=%d \n" ,argc);
  102. /* if (argc >3)
  103. {
  104. printf("argv[1]=%s\n" ,argv[1]);
  105. printf("argv[2]=%s\n" ,argv[2]);
  106. printf("argv[3]=%s\n" ,argv[3]);
  107. printf("argv[4]=%s\n" ,argv[4]);
  108. }*/
  109. // execute cmd
  110. // send hex data eg. cmdline : exe -s 00008002
  111. if (argc == 3  && argv[1][1] == 's')
  112. {
  113. if((i=String2Hex(argv[2],buffer))>0)
  114. {
  115. WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
  116. printf("Send data= %s datalen=%d\n",argv[2],i);
  117. }
  118. }
  119. // send hex data and get the data send back by the CPU eg. cmdline : exe -s 00008002
  120. if (argc == 3  && argv[1][1] == 'r')
  121. {
  122. if((i=String2Hex(argv[2],buffer))>0)
  123. {
  124. WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
  125. printf("Send data= %s datalen=%d\n",argv[2],i);
  126. }
  127.         Sleep(1000);
  128. i=ReadFile(serial_port,buffer,255,&bytes_read,NULL);
  129. if(i)
  130. {
  131. printf("Read data from serial port:\n");
  132. for(i=0;i<bytes_read;i++)
  133. {
  134. if(((unsigned char)buffer[i])<0x10)
  135. printf("0%X ",(unsigned char)buffer[i]);
  136. else
  137. printf("%X ",(unsigned char)buffer[i]);
  138. }
  139. printf("\n");
  140. }
  141. }
  142. //test send 10 times data over the cable eg. cmdline : exe -t
  143. if (argc ==2  && argv[1][1] == 't' )
  144. {
  145. for( i=0;i<10;i++)
  146. {
  147. WriteFile(serial_port, (void *)buffer, strlen(buffer), &bytes_written, NULL);
  148. Sleep(500);
  149. }
  150. }
  151. //WR Rigister  eg. cmdline : exe -w 0000 -d 0102030405
  152. if (argc ==5  && argv[1][1] == 'w'  && argv[3][1]== 'd')
  153. {
  154. i=String2Hex(argv[4],cmdbuffer);
  155. databuffer=i;
  156. if(i>0)
  157. {
  158. lowByte=i & (0xff);
  159. highByte=(1<<7) + (i>>8);
  160. sprintf(cmdbuffer,"%2s%02x%02x%s",argv[2],highByte,lowByte,argv[4]);         
  161. }
  162.         if((i=String2Hex(cmdbuffer,buffer))>0)
  163. WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
  164. printf("Write to Register(%s) data=%s datelen=%d Bytes\n",argv[2],argv[4],databuffer);
  165. }
  166. //WR Rigister but the data is reversed  eg. cmdline : exe -p 0000 -d 0102030405 ==> exe -p 0000 -d 0504030201
  167. if (argc ==5  && argv[1][1] == 'p'  && argv[3][1]== 'd')
  168. {
  169. i=String2Hex(argv[4],cmdbuffer);
  170. databuffer=i;
  171. if(i>0)
  172. {
  173. lowByte=i & (0xff);
  174. highByte=(1<<7) + (i>>8);
  175. sprintf(cmdbuffer,"%2s%02x%02x%s",argv[2],highByte,lowByte,argv[4]);         
  176. }
  177.         if((i=String2Hex(cmdbuffer,buffer))>0)
  178. {
  179. //reserved
  180. for(counttmp=0;counttmp<(int)(databuffer/2);counttmp++)
  181. {
  182. // data begin :buffer[4]-->
  183. lowByte=buffer[4+counttmp];
  184. buffer[4+counttmp]=buffer[4+ databuffer-1-counttmp];
  185. buffer[4+ databuffer-1-counttmp]=lowByte;
  186. }
  187. WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
  188. }
  189. printf("Write to Register(%s) data(Reversed)=%s datelen=%d Bytes\n",argv[2],argv[4],databuffer);
  190. }
  191. //WR Rigister and get the data send back  eg. cmdline : exe -e 0000 -d 0102030405
  192. if (argc ==5  && argv[1][1] == 'e'  && argv[3][1]== 'd')
  193. {
  194. i=String2Hex(argv[4],cmdbuffer);
  195. databuffer=i;
  196. if(i>0)
  197. {
  198. lowByte=i & (0xff);
  199. highByte=(1<<7) + (i>>8);
  200. sprintf(cmdbuffer,"%2s%02x%02x%s",argv[2],highByte,lowByte,argv[4]);         
  201. }
  202.         if((i=String2Hex(cmdbuffer,buffer))>0)
  203. WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
  204. printf("Write to Register(%s) data=%s datelen=%d Bytes\n",argv[2],argv[4],databuffer);
  205. Sleep(500);
  206. i=ReadFile(serial_port,buffer,255,&bytes_read,NULL);
  207. if(i)
  208. {
  209. printf("Read data from serial port:\n");
  210. for(i=0;i<bytes_read;i++)
  211. {
  212. if(((unsigned char)buffer[i])<0x10)
  213. printf("0%X ",(unsigned char)buffer[i]);
  214. else
  215. printf("%X ",(unsigned char)buffer[i]);
  216. }
  217. printf("\n");
  218. // if(bytes_read>0)
  219. // printf("\nRigister=0x%X%X\n",buffer[0],buffer[1]);
  220. }
  221. }
  222. /* Get the file size
  223. file_size = get_file_size(argv[1]);*/
  224. /* Print out information
  225. printf("Preparing to transmit file %s: %lu bytes\n", argv[1], file_size);*/
  226. /* Write file name size to serial port
  227. file_name_size = (unsigned long)strlen(argv[1]);
  228. WriteFile(serial_port, (void *)&file_name_size, sizeof(unsigned long), &bytes_written, NULL);
  229. if (bytes_written != sizeof(unsigned long))
  230. {
  231. fprintf(stderr, "Error writing file name size.\n");
  232. CloseHandle(serial_port);
  233. exit(0);
  234. }*/
  235. /* Write file name to serial port
  236. WriteFile(serial_port, (void *)argv[1], file_name_size, &bytes_written, NULL);
  237. if (bytes_written != file_name_size)
  238. {
  239. fprintf(stderr, "Error writing file name.\n");
  240. CloseHandle(serial_port);
  241. exit(0);
  242. }*/
  243. /* Write file size to serial port
  244. WriteFile(serial_port, (void *)&file_size, sizeof(unsigned long), &bytes_written, NULL);
  245. if (bytes_written != sizeof(unsigned long))
  246. {
  247. fprintf(stderr, "Error writing file size.\n");
  248. CloseHandle(serial_port);
  249. exit(0);
  250. }*/
  251. /* Write file to serial port
  252. write_file_to_serial_port(serial_port, argv[1], file_size);
  253. printf("\n%lu bytes successfully transmitted.\n", file_size);*/
  254.      
  255. /* Close the handle */
  256. CloseHandle(serial_port);
  257. return 0;
  258. }
  259. void set_up_serial_port(HANDLE h, long baud)
  260. {
  261. DCB properties; /* Properties of serial port */
  262. /* Get the properties */
  263. GetCommState(h, &properties);
  264. /* Set the baud rate */
  265. switch(baud)
  266. {
  267. case 1200:
  268. properties.BaudRate = CBR_1200;
  269. break;
  270. case 2400:
  271. properties.BaudRate = CBR_2400;
  272. break;
  273. case 4800:
  274. properties.BaudRate = CBR_4800;
  275. break;
  276. case 9600:
  277. properties.BaudRate = CBR_9600;
  278. break;
  279. case 14400:
  280. properties.BaudRate = CBR_14400;
  281. break;
  282. case 19200:
  283. properties.BaudRate = CBR_19200;
  284. break;
  285. case 38400:
  286. properties.BaudRate = CBR_38400;
  287. break;
  288. default:
  289. fprintf(stderr, "Invalid baud rate: %ld", baud);
  290. usage();
  291. exit(0);
  292. break;
  293. }
  294. /* Set the other properties */
  295. properties.Parity = NOPARITY;
  296. properties.ByteSize = 8;
  297. properties.StopBits = ONESTOPBIT;
  298. SetCommState(h, &properties);
  299. return;
  300. }
  301. void usage(void)
  302. {
  303. fprintf(stderr, "Usage:\n");
  304. fprintf(stderr, "\tsertrans file [-b baud_rate]\n");
  305. fprintf(stderr, "\tDefault baud rate is 9600\n");
  306. fprintf(stderr, "\tSupported baud rates: 1200, 2400, 4800, 9600, 14400, 19200, 38400\n");
  307. return;
  308. }
  309. unsigned long get_file_size(char *file_name)
  310. {
  311. FILE *data_file;
  312. unsigned long size = 0;
  313. size_t bytes_read;
  314. char byte_read;
  315. /* Open the file */
  316. data_file = fopen(file_name, "rb");
  317. /* Quit if file couldn't be opened */
  318. if (data_file == NULL)
  319. {
  320. fprintf(stderr, "Could not open file %s\n", file_name);
  321. exit(0);
  322. }
  323. /* Read in data one byte at a time until we reach the end */
  324. while (1)
  325. {
  326. bytes_read = fread((void *)&byte_read, 1, 1, data_file);
  327. if (bytes_read <= 0)
  328. {
  329. break;
  330. }
  331. ++size;
  332. }
  333. /* Close file */
  334. fclose(data_file);
  335. return size;
  336. }
  337. void write_file_to_serial_port(HANDLE h, char *file_name, unsigned long file_size)
  338. {
  339. FILE *data_file;
  340. unsigned long bytes_left = file_size;
  341. unsigned long bytes_sent;
  342. unsigned long bytes_read;
  343. unsigned long total_bytes_sent = 0;
  344. size_t bytes_to_send;
  345. char buffer[200];
  346. /* Open the file */
  347. data_file = fopen(file_name, "rb");
  348. /* Quit if file couldn't be opened */
  349. if (data_file == NULL)
  350. {
  351. fprintf(stderr, "Could not open file %s\n", file_name);
  352. exit(0);
  353. }
  354. while (1)
  355. {
  356. /* Determine how many bytes to send */
  357. if (bytes_left == 0)
  358. {
  359. break;
  360. }
  361. else if (bytes_left < 200)
  362. {
  363. bytes_to_send = bytes_left;
  364. }
  365. else
  366. {
  367. bytes_to_send = 200;
  368. }
  369. /* Read in specified number of bytes */
  370. bytes_read = (unsigned long)fread((void *)buffer, 1, bytes_to_send, data_file);
  371. /* Send data over serial cable */
  372. WriteFile(h, (void *)buffer, bytes_read, &bytes_sent, NULL);
  373. if (bytes_sent != bytes_read)
  374. {
  375. fprintf(stderr, "Error writing file.\n");
  376. CloseHandle(h);
  377. exit(0);
  378. }
  379. /* Decrement number of bytes left */
  380. bytes_left -= bytes_sent;
  381. /* Increment number of bytes sent */
  382. total_bytes_sent += bytes_sent;
  383. /* Print out progress */
  384. printf("\r%5lu bytes transmitted.", total_bytes_sent);
  385. }
  386. fclose(data_file);
  387. return;
  388. }
复制代码





欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2