找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 32247|回复: 1

[文件操作] 批处理能操作串口吗?

[复制链接]
发表于 2013-3-29 22:34:35 | 显示全部楼层 |阅读模式
如题
想向串口1发送一个小的二进制文件,有什么命令能实现吗?

类似串口调试助手和超级终端。但我要自己设定间隔时间,自动上传。
发表于 2013-3-30 00:39:25 | 显示全部楼层
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.        
  80.         // do not use the original cmd check , here
  81.         //leave blank
  82.    
  83.         data_file = fopen(cfg_file_name, "rb");       
  84.         if (data_file == NULL)
  85.         {
  86.                 fprintf(stderr, "Could not open file %s\n", cfg_file_name);
  87.                 exit(0);
  88.         }
  89.         i = (unsigned long)fread((void *)port_name, 1, 10, data_file);
  90.        
  91.         /* Open up a handle to the serial port */
  92.         serial_port = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

  93.         /* Make sure port was opened */
  94.         if (serial_port == INVALID_HANDLE_VALUE)
  95.         {
  96.                 fprintf(stderr, "Error opening port\n");
  97.                 CloseHandle(serial_port);
  98.                 exit(0);
  99.         }

  100.         /* Set up the serial port */
  101.         set_up_serial_port(serial_port, baud_rate);
  102. //        printf("%s opned!\n" ,port_name);
  103. //        printf("argc=%d \n" ,argc);
  104. /*        if (argc >3)
  105.         {
  106.                 printf("argv[1]=%s\n" ,argv[1]);
  107.                 printf("argv[2]=%s\n" ,argv[2]);
  108.                 printf("argv[3]=%s\n" ,argv[3]);
  109.                 printf("argv[4]=%s\n" ,argv[4]);
  110.         }*/

  111.         //        execute cmd

  112.         // send hex data eg. cmdline : exe -s 00008002
  113.         if (argc == 3  && argv[1][1] == 's')
  114.         {       
  115.                 if((i=String2Hex(argv[2],buffer))>0)
  116.                 {                       
  117.                         WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
  118.                         printf("Send data= %s datalen=%d\n",argv[2],i);
  119.                 }
  120.         }

  121.         // send hex data and get the data send back by the CPU eg. cmdline : exe -s 00008002
  122.         if (argc == 3  && argv[1][1] == 'r')
  123.         {       
  124.                 if((i=String2Hex(argv[2],buffer))>0)
  125.                 {                       
  126.                         WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
  127.                         printf("Send data= %s datalen=%d\n",argv[2],i);
  128.                 }
  129.         Sleep(1000);
  130.                 i=ReadFile(serial_port,buffer,255,&bytes_read,NULL);
  131.                 if(i)
  132.                 {
  133.                         printf("Read data from serial port:\n");
  134.                         for(i=0;i<bytes_read;i++)
  135.                         {
  136.                                 if(((unsigned char)buffer[i])<0x10)
  137.                                         printf("0%X ",(unsigned char)buffer[i]);
  138.                                 else
  139.                                         printf("%X ",(unsigned char)buffer[i]);
  140.                         }
  141.                         printf("\n");
  142.                 }
  143.         }


  144.         //test send 10 times data over the cable eg. cmdline : exe -t
  145.         if (argc ==2  && argv[1][1] == 't' )
  146.         {
  147.                 for( i=0;i<10;i++)
  148.                 {
  149.                         WriteFile(serial_port, (void *)buffer, strlen(buffer), &bytes_written, NULL);       
  150.                         Sleep(500);
  151.                 }
  152.         }

  153.         //WR Rigister  eg. cmdline : exe -w 0000 -d 0102030405
  154.         if (argc ==5  && argv[1][1] == 'w'  && argv[3][1]== 'd')
  155.         {
  156.                 i=String2Hex(argv[4],cmdbuffer);
  157.                 databuffer=i;
  158.                 if(i>0)
  159.                 {
  160.                         lowByte=i & (0xff);
  161.                         highByte=(1<<7) + (i>>8);
  162.                         sprintf(cmdbuffer,"%2s%02x%02x%s",argv[2],highByte,lowByte,argv[4]);         
  163.                 }
  164.         if((i=String2Hex(cmdbuffer,buffer))>0)
  165.                         WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
  166.                 printf("Write to Register(%s) data=%s datelen=%d Bytes\n",argv[2],argv[4],databuffer);
  167.         }

  168.         //WR Rigister but the data is reversed  eg. cmdline : exe -p 0000 -d 0102030405 ==> exe -p 0000 -d 0504030201
  169.         if (argc ==5  && argv[1][1] == 'p'  && argv[3][1]== 'd')
  170.         {

  171.                 i=String2Hex(argv[4],cmdbuffer);
  172.                 databuffer=i;

  173.                 if(i>0)
  174.                 {
  175.                         lowByte=i & (0xff);
  176.                         highByte=(1<<7) + (i>>8);
  177.                         sprintf(cmdbuffer,"%2s%02x%02x%s",argv[2],highByte,lowByte,argv[4]);         
  178.                 }               

  179.         if((i=String2Hex(cmdbuffer,buffer))>0)
  180.                 {
  181.                         //reserved
  182.                         for(counttmp=0;counttmp<(int)(databuffer/2);counttmp++)
  183.                         {
  184.                                 // data begin :buffer[4]-->
  185.                                 lowByte=buffer[4+counttmp];
  186.                                 buffer[4+counttmp]=buffer[4+ databuffer-1-counttmp];
  187.                                 buffer[4+ databuffer-1-counttmp]=lowByte;
  188.                         }
  189.                         WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
  190.                 }
  191.                 printf("Write to Register(%s) data(Reversed)=%s datelen=%d Bytes\n",argv[2],argv[4],databuffer);
  192.         }

  193.         //WR Rigister and get the data send back  eg. cmdline : exe -e 0000 -d 0102030405
  194.         if (argc ==5  && argv[1][1] == 'e'  && argv[3][1]== 'd')
  195.         {
  196.                 i=String2Hex(argv[4],cmdbuffer);
  197.                 databuffer=i;
  198.                 if(i>0)
  199.                 {
  200.                         lowByte=i & (0xff);
  201.                         highByte=(1<<7) + (i>>8);
  202.                         sprintf(cmdbuffer,"%2s%02x%02x%s",argv[2],highByte,lowByte,argv[4]);         
  203.                 }
  204.         if((i=String2Hex(cmdbuffer,buffer))>0)
  205.                         WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
  206.                 printf("Write to Register(%s) data=%s datelen=%d Bytes\n",argv[2],argv[4],databuffer);

  207.                 Sleep(500);
  208.                 i=ReadFile(serial_port,buffer,255,&bytes_read,NULL);
  209.                 if(i)
  210.                 {
  211.                         printf("Read data from serial port:\n");
  212.                         for(i=0;i<bytes_read;i++)
  213.                         {
  214.                                 if(((unsigned char)buffer[i])<0x10)
  215.                                         printf("0%X ",(unsigned char)buffer[i]);
  216.                                 else
  217.                                         printf("%X ",(unsigned char)buffer[i]);
  218.                         }
  219.                         printf("\n");
  220.                 //        if(bytes_read>0)
  221.                 //                printf("\nRigister=0x%X%X\n",buffer[0],buffer[1]);
  222.                 }
  223.         }

  224.        
  225.         /* Get the file size
  226.         file_size = get_file_size(argv[1]);*/

  227.         /* Print out information
  228.         printf("Preparing to transmit file %s: %lu bytes\n", argv[1], file_size);*/

  229.         /* Write file name size to serial port
  230.         file_name_size = (unsigned long)strlen(argv[1]);
  231.         WriteFile(serial_port, (void *)&file_name_size, sizeof(unsigned long), &bytes_written, NULL);
  232.         if (bytes_written != sizeof(unsigned long))
  233.         {
  234.                 fprintf(stderr, "Error writing file name size.\n");
  235.                 CloseHandle(serial_port);
  236.                 exit(0);
  237.         }*/

  238.         /* Write file name to serial port
  239.         WriteFile(serial_port, (void *)argv[1], file_name_size, &bytes_written, NULL);
  240.         if (bytes_written != file_name_size)
  241.         {
  242.                 fprintf(stderr, "Error writing file name.\n");
  243.                 CloseHandle(serial_port);
  244.                 exit(0);
  245.         }*/


  246.         /* Write file size to serial port
  247.        
  248.         WriteFile(serial_port, (void *)&file_size, sizeof(unsigned long), &bytes_written, NULL);
  249.         if (bytes_written != sizeof(unsigned long))
  250.         {
  251.                 fprintf(stderr, "Error writing file size.\n");
  252.                 CloseHandle(serial_port);
  253.                 exit(0);
  254.         }*/

  255.         /* Write file to serial port
  256.         write_file_to_serial_port(serial_port, argv[1], file_size);

  257.         printf("\n%lu bytes successfully transmitted.\n", file_size);*/
  258.      
  259.         /* Close the handle */
  260.         CloseHandle(serial_port);

  261.         return 0;
  262. }

  263. void set_up_serial_port(HANDLE h, long baud)
  264. {
  265.         DCB properties;                        /* Properties of serial port */

  266.         /* Get the properties */
  267.         GetCommState(h, &properties);

  268.         /* Set the baud rate */
  269.         switch(baud)
  270.         {
  271.         case 1200:
  272.                 properties.BaudRate = CBR_1200;
  273.                 break;
  274.         case 2400:
  275.                 properties.BaudRate = CBR_2400;
  276.                 break;
  277.         case 4800:
  278.                 properties.BaudRate = CBR_4800;
  279.                 break;
  280.         case 9600:
  281.                 properties.BaudRate = CBR_9600;
  282.                 break;
  283.         case 14400:
  284.                 properties.BaudRate = CBR_14400;
  285.                 break;
  286.         case 19200:
  287.                 properties.BaudRate = CBR_19200;
  288.                 break;
  289.         case 38400:
  290.                 properties.BaudRate = CBR_38400;
  291.                 break;
  292.         default:
  293.                 fprintf(stderr, "Invalid baud rate: %ld", baud);
  294.                 usage();
  295.                 exit(0);
  296.                 break;
  297.         }
  298.        
  299.         /* Set the other properties */
  300.         properties.Parity = NOPARITY;
  301.         properties.ByteSize = 8;
  302.         properties.StopBits = ONESTOPBIT;

  303.         SetCommState(h, &properties);

  304.         return;
  305. }

  306. void usage(void)
  307. {
  308.         fprintf(stderr, "Usage:\n");
  309.         fprintf(stderr, "\tsertrans file [-b baud_rate]\n");
  310.         fprintf(stderr, "\tDefault baud rate is 9600\n");
  311.         fprintf(stderr, "\tSupported baud rates: 1200, 2400, 4800, 9600, 14400, 19200, 38400\n");
  312.         return;
  313. }

  314. unsigned long get_file_size(char *file_name)
  315. {
  316.         FILE *data_file;
  317.         unsigned long size = 0;
  318.         size_t bytes_read;
  319.         char byte_read;

  320.         /* Open the file */
  321.         data_file = fopen(file_name, "rb");

  322.         /* Quit if file couldn't be opened */
  323.         if (data_file == NULL)
  324.         {
  325.                 fprintf(stderr, "Could not open file %s\n", file_name);
  326.                 exit(0);
  327.         }

  328.         /* Read in data one byte at a time until we reach the end */
  329.         while (1)
  330.         {
  331.                 bytes_read = fread((void *)&byte_read, 1, 1, data_file);
  332.                 if (bytes_read <= 0)
  333.                 {
  334.                         break;
  335.                 }
  336.                 ++size;
  337.         }

  338.         /* Close file */
  339.         fclose(data_file);

  340.         return size;
  341. }

  342. void write_file_to_serial_port(HANDLE h, char *file_name, unsigned long file_size)
  343. {
  344.         FILE *data_file;
  345.         unsigned long bytes_left = file_size;
  346.         unsigned long bytes_sent;
  347.         unsigned long bytes_read;
  348.         unsigned long total_bytes_sent = 0;
  349.         size_t bytes_to_send;
  350.         char buffer[200];

  351.         /* Open the file */
  352.         data_file = fopen(file_name, "rb");

  353.         /* Quit if file couldn't be opened */
  354.         if (data_file == NULL)
  355.         {
  356.                 fprintf(stderr, "Could not open file %s\n", file_name);
  357.                 exit(0);
  358.         }

  359.         while (1)
  360.         {
  361.                 /* Determine how many bytes to send */
  362.                 if (bytes_left == 0)
  363.                 {
  364.                         break;
  365.                 }
  366.                 else if (bytes_left < 200)
  367.                 {
  368.                         bytes_to_send = bytes_left;
  369.                 }
  370.                 else
  371.                 {
  372.                         bytes_to_send = 200;
  373.                 }

  374.                 /* Read in specified number of bytes */
  375.                 bytes_read = (unsigned long)fread((void *)buffer, 1, bytes_to_send, data_file);

  376.                 /* Send data over serial cable */
  377.                 WriteFile(h, (void *)buffer, bytes_read, &bytes_sent, NULL);
  378.                 if (bytes_sent != bytes_read)
  379.                 {
  380.                         fprintf(stderr, "Error writing file.\n");
  381.                         CloseHandle(h);
  382.                         exit(0);
  383.                 }
  384.                
  385.                 /* Decrement number of bytes left */
  386.                 bytes_left -= bytes_sent;

  387.                 /* Increment number of bytes sent */
  388.                 total_bytes_sent += bytes_sent;

  389.                 /* Print out progress */
  390.                 printf("\r%5lu bytes transmitted.", total_bytes_sent);
  391.         }

  392.         fclose(data_file);

  393.         return;
  394. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 02:26 , Processed in 0.020275 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表