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 编译通过,共享出来,免得你们没分下不了,里面定死了端口和文件,自己仿着改吧:- /* sertrans.c */
- /* Transmits a file to another computer over a serial cable */
- /* Last modified: September 20, 2005 */
- /* http://www.gomorgan89.com */
-
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- /* Function to set up the serial port settings with the specified baud rate,
- no parity, and one stop bit */
- void set_up_serial_port(HANDLE h, long baud);
-
- /* Function to print out usage */
- void usage(void);
-
- /* Function to get size of file */
- unsigned long get_file_size(char *file_name);
-
- /* Function to write the file to the serial port */
- void write_file_to_serial_port(HANDLE h, char *file_name, unsigned long file_size);
-
-
- char ConvertHexChar(char ch)
- {
- if((ch>='0')&&(ch<='9'))
- return ch-0x30;
- else if((ch>='A')&&(ch<='F'))
- return ch-'A'+10;
- else if((ch>='a')&&(ch<='f'))
- return ch-'a'+10;
- else return (-1);
- }
-
- int String2Hex(char * str, char* senddata)
- {
- int hexdata,lowhexdata;
- int hexdatalen=0;
- int len=strlen(str);
- int i;
- // senddata.SetSize(len/2);
- for(i=0;i<len;)
- {
- char lstr,hstr=str[i];
- if(hstr==' ')
- {
- i++;
- continue;
- }
- i++;
- if(i>=len)
- break;
- lstr=str[i];
- hexdata=ConvertHexChar(hstr);
- lowhexdata=ConvertHexChar(lstr);
- if((hexdata==16)||(lowhexdata==16))
- break;
- else
- hexdata=hexdata*16+lowhexdata;
- i++;
- senddata[hexdatalen]=(char)hexdata;
- hexdatalen++;
- }
- // senddata.SetSize(hexdatalen);
- return hexdatalen;
- }
-
- int main(int argc, char **argv)
- {
- HANDLE serial_port; /* Handle to the serial port */
- long baud_rate = 9600; /* Specified baud rate */
- char port_name[] = "COM3:"; /* Name of the serial port */
- unsigned long file_size; /* File size to transmit in bytes */
- unsigned long bytes_written; /* Bytes written to serial port */
- unsigned long file_name_size; /* Size of file name */
- unsigned long bytes_read;
- char buffer[255]="Test data";
- char cmdbuffer[255]="";
- //if (argc>0)
- char cfg_file_name[]="c:\\testtesttest1231231retrewerhgdrffhgsdfsadfaesj.6dfy";
- cfg_file_name=argv[1];
- printf("%s",cfg_file_name[]);
- int databuffer;
- unsigned char lowByte,highByte;
- int i=0,counttmp=0;
- FILE *data_file;
-
- // do not use the original cmd check , here
- //leave blank
-
- data_file = fopen(cfg_file_name, "rb");
- if (data_file == NULL)
- {
- fprintf(stderr, "Could not open file %s\n", cfg_file_name);
- exit(0);
- }
- i = (unsigned long)fread((void *)port_name, 1, 10, data_file);
-
- /* Open up a handle to the serial port */
- serial_port = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
-
- /* Make sure port was opened */
- if (serial_port == INVALID_HANDLE_VALUE)
- {
- fprintf(stderr, "Error opening port\n");
- CloseHandle(serial_port);
- exit(0);
- }
-
- /* Set up the serial port */
- set_up_serial_port(serial_port, baud_rate);
- // printf("%s opned!\n" ,port_name);
- // printf("argc=%d \n" ,argc);
- /* if (argc >3)
- {
- printf("argv[1]=%s\n" ,argv[1]);
- printf("argv[2]=%s\n" ,argv[2]);
- printf("argv[3]=%s\n" ,argv[3]);
- printf("argv[4]=%s\n" ,argv[4]);
- }*/
-
- // execute cmd
-
- // send hex data eg. cmdline : exe -s 00008002
- if (argc == 3 && argv[1][1] == 's')
- {
- if((i=String2Hex(argv[2],buffer))>0)
- {
- WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
- printf("Send data= %s datalen=%d\n",argv[2],i);
- }
- }
-
- // send hex data and get the data send back by the CPU eg. cmdline : exe -s 00008002
- if (argc == 3 && argv[1][1] == 'r')
- {
- if((i=String2Hex(argv[2],buffer))>0)
- {
- WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
- printf("Send data= %s datalen=%d\n",argv[2],i);
- }
- Sleep(1000);
- i=ReadFile(serial_port,buffer,255,&bytes_read,NULL);
- if(i)
- {
- printf("Read data from serial port:\n");
- for(i=0;i<bytes_read;i++)
- {
- if(((unsigned char)buffer[i])<0x10)
- printf("0%X ",(unsigned char)buffer[i]);
- else
- printf("%X ",(unsigned char)buffer[i]);
- }
- printf("\n");
- }
- }
-
-
- //test send 10 times data over the cable eg. cmdline : exe -t
- if (argc ==2 && argv[1][1] == 't' )
- {
- for( i=0;i<10;i++)
- {
- WriteFile(serial_port, (void *)buffer, strlen(buffer), &bytes_written, NULL);
- Sleep(500);
- }
- }
-
- //WR Rigister eg. cmdline : exe -w 0000 -d 0102030405
- if (argc ==5 && argv[1][1] == 'w' && argv[3][1]== 'd')
- {
- i=String2Hex(argv[4],cmdbuffer);
- databuffer=i;
- if(i>0)
- {
- lowByte=i & (0xff);
- highByte=(1<<7) + (i>>8);
- sprintf(cmdbuffer,"%2s%02x%02x%s",argv[2],highByte,lowByte,argv[4]);
- }
- if((i=String2Hex(cmdbuffer,buffer))>0)
- WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
- printf("Write to Register(%s) data=%s datelen=%d Bytes\n",argv[2],argv[4],databuffer);
- }
-
- //WR Rigister but the data is reversed eg. cmdline : exe -p 0000 -d 0102030405 ==> exe -p 0000 -d 0504030201
- if (argc ==5 && argv[1][1] == 'p' && argv[3][1]== 'd')
- {
-
- i=String2Hex(argv[4],cmdbuffer);
- databuffer=i;
-
- if(i>0)
- {
- lowByte=i & (0xff);
- highByte=(1<<7) + (i>>8);
- sprintf(cmdbuffer,"%2s%02x%02x%s",argv[2],highByte,lowByte,argv[4]);
- }
-
- if((i=String2Hex(cmdbuffer,buffer))>0)
- {
- //reserved
- for(counttmp=0;counttmp<(int)(databuffer/2);counttmp++)
- {
- // data begin :buffer[4]-->
- lowByte=buffer[4+counttmp];
- buffer[4+counttmp]=buffer[4+ databuffer-1-counttmp];
- buffer[4+ databuffer-1-counttmp]=lowByte;
- }
- WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
- }
- printf("Write to Register(%s) data(Reversed)=%s datelen=%d Bytes\n",argv[2],argv[4],databuffer);
- }
-
- //WR Rigister and get the data send back eg. cmdline : exe -e 0000 -d 0102030405
- if (argc ==5 && argv[1][1] == 'e' && argv[3][1]== 'd')
- {
- i=String2Hex(argv[4],cmdbuffer);
- databuffer=i;
- if(i>0)
- {
- lowByte=i & (0xff);
- highByte=(1<<7) + (i>>8);
- sprintf(cmdbuffer,"%2s%02x%02x%s",argv[2],highByte,lowByte,argv[4]);
- }
- if((i=String2Hex(cmdbuffer,buffer))>0)
- WriteFile(serial_port, (void *)buffer, i, &bytes_written, NULL);
- printf("Write to Register(%s) data=%s datelen=%d Bytes\n",argv[2],argv[4],databuffer);
-
- Sleep(500);
- i=ReadFile(serial_port,buffer,255,&bytes_read,NULL);
- if(i)
- {
- printf("Read data from serial port:\n");
- for(i=0;i<bytes_read;i++)
- {
- if(((unsigned char)buffer[i])<0x10)
- printf("0%X ",(unsigned char)buffer[i]);
- else
- printf("%X ",(unsigned char)buffer[i]);
- }
- printf("\n");
- // if(bytes_read>0)
- // printf("\nRigister=0x%X%X\n",buffer[0],buffer[1]);
- }
- }
-
-
- /* Get the file size
- file_size = get_file_size(argv[1]);*/
-
- /* Print out information
- printf("Preparing to transmit file %s: %lu bytes\n", argv[1], file_size);*/
-
- /* Write file name size to serial port
- file_name_size = (unsigned long)strlen(argv[1]);
- WriteFile(serial_port, (void *)&file_name_size, sizeof(unsigned long), &bytes_written, NULL);
- if (bytes_written != sizeof(unsigned long))
- {
- fprintf(stderr, "Error writing file name size.\n");
- CloseHandle(serial_port);
- exit(0);
- }*/
-
- /* Write file name to serial port
- WriteFile(serial_port, (void *)argv[1], file_name_size, &bytes_written, NULL);
- if (bytes_written != file_name_size)
- {
- fprintf(stderr, "Error writing file name.\n");
- CloseHandle(serial_port);
- exit(0);
- }*/
-
-
- /* Write file size to serial port
-
- WriteFile(serial_port, (void *)&file_size, sizeof(unsigned long), &bytes_written, NULL);
- if (bytes_written != sizeof(unsigned long))
- {
- fprintf(stderr, "Error writing file size.\n");
- CloseHandle(serial_port);
- exit(0);
- }*/
-
- /* Write file to serial port
- write_file_to_serial_port(serial_port, argv[1], file_size);
-
- printf("\n%lu bytes successfully transmitted.\n", file_size);*/
-
- /* Close the handle */
- CloseHandle(serial_port);
-
- return 0;
- }
-
- void set_up_serial_port(HANDLE h, long baud)
- {
- DCB properties; /* Properties of serial port */
-
- /* Get the properties */
- GetCommState(h, &properties);
-
- /* Set the baud rate */
- switch(baud)
- {
- case 1200:
- properties.BaudRate = CBR_1200;
- break;
- case 2400:
- properties.BaudRate = CBR_2400;
- break;
- case 4800:
- properties.BaudRate = CBR_4800;
- break;
- case 9600:
- properties.BaudRate = CBR_9600;
- break;
- case 14400:
- properties.BaudRate = CBR_14400;
- break;
- case 19200:
- properties.BaudRate = CBR_19200;
- break;
- case 38400:
- properties.BaudRate = CBR_38400;
- break;
- default:
- fprintf(stderr, "Invalid baud rate: %ld", baud);
- usage();
- exit(0);
- break;
- }
-
- /* Set the other properties */
- properties.Parity = NOPARITY;
- properties.ByteSize = 8;
- properties.StopBits = ONESTOPBIT;
-
- SetCommState(h, &properties);
-
- return;
- }
-
- void usage(void)
- {
- fprintf(stderr, "Usage:\n");
- fprintf(stderr, "\tsertrans file [-b baud_rate]\n");
- fprintf(stderr, "\tDefault baud rate is 9600\n");
- fprintf(stderr, "\tSupported baud rates: 1200, 2400, 4800, 9600, 14400, 19200, 38400\n");
- return;
- }
-
- unsigned long get_file_size(char *file_name)
- {
- FILE *data_file;
- unsigned long size = 0;
- size_t bytes_read;
- char byte_read;
-
- /* Open the file */
- data_file = fopen(file_name, "rb");
-
- /* Quit if file couldn't be opened */
- if (data_file == NULL)
- {
- fprintf(stderr, "Could not open file %s\n", file_name);
- exit(0);
- }
-
- /* Read in data one byte at a time until we reach the end */
- while (1)
- {
- bytes_read = fread((void *)&byte_read, 1, 1, data_file);
- if (bytes_read <= 0)
- {
- break;
- }
- ++size;
- }
-
- /* Close file */
- fclose(data_file);
-
- return size;
- }
-
- void write_file_to_serial_port(HANDLE h, char *file_name, unsigned long file_size)
- {
- FILE *data_file;
- unsigned long bytes_left = file_size;
- unsigned long bytes_sent;
- unsigned long bytes_read;
- unsigned long total_bytes_sent = 0;
- size_t bytes_to_send;
- char buffer[200];
-
- /* Open the file */
- data_file = fopen(file_name, "rb");
-
- /* Quit if file couldn't be opened */
- if (data_file == NULL)
- {
- fprintf(stderr, "Could not open file %s\n", file_name);
- exit(0);
- }
-
- while (1)
- {
- /* Determine how many bytes to send */
- if (bytes_left == 0)
- {
- break;
- }
- else if (bytes_left < 200)
- {
- bytes_to_send = bytes_left;
- }
- else
- {
- bytes_to_send = 200;
- }
-
- /* Read in specified number of bytes */
- bytes_read = (unsigned long)fread((void *)buffer, 1, bytes_to_send, data_file);
-
- /* Send data over serial cable */
- WriteFile(h, (void *)buffer, bytes_read, &bytes_sent, NULL);
- if (bytes_sent != bytes_read)
- {
- fprintf(stderr, "Error writing file.\n");
- CloseHandle(h);
- exit(0);
- }
-
- /* Decrement number of bytes left */
- bytes_left -= bytes_sent;
-
- /* Increment number of bytes sent */
- total_bytes_sent += bytes_sent;
-
- /* Print out progress */
- printf("\r%5lu bytes transmitted.", total_bytes_sent);
- }
-
- fclose(data_file);
-
- return;
- }
复制代码
|