1 | #define _CRT_SECURE_NO_WARNINGS
|
2 | #include <windows.h>
|
3 | #include <stdio.h>
|
4 |
|
5 | static const char *title = "\n\tSPI-tester \n";
|
6 | static const char *usage = "\t\tusage: spitest port_number";
|
7 |
|
8 |
|
9 | static int hex2bin( unsigned char *bin, char *hex )
|
10 | {
|
11 | int n;
|
12 |
|
13 | for( n=0; *hex; ) {
|
14 | if( *hex>' ' )
|
15 | bin[n++] = (unsigned char)strtol( hex, &hex, 16 );
|
16 | else
|
17 | hex++;
|
18 | }
|
19 | return n;
|
20 | }
|
21 |
|
22 | static void bin2hex( char *hex, unsigned char *bin, int n )//bin2dec wandlung
|
23 | {
|
24 |
|
25 | for( ; n>0; n-- )
|
26 | hex += sprintf( hex, " %04i", *bin++ ); //"%02X" X in Hex "%03i" für Intiger als 4 stelle
|
27 |
|
28 | *hex = 0;
|
29 | }
|
30 |
|
31 | int main(int argc, char *argv[])
|
32 | {
|
33 | HANDLE hCom;
|
34 | DWORD rwlen;
|
35 | int n, v, quit;
|
36 | char *ptr, text[1024]; // war auf auf 256 eingestellt
|
37 | unsigned char data[1]; //war auf 32 gestellt
|
38 |
|
39 | puts( title );
|
40 |
|
41 | if( argc!=2 ) {
|
42 | puts( usage );
|
43 | return -1;
|
44 | }
|
45 |
|
46 | /* Open COM device */
|
47 | sprintf( text, "\\\\.\\COM%s", argv[1] );
|
48 | hCom = CreateFile( text, GENERIC_READ|GENERIC_WRITE,
|
49 | 0, 0, OPEN_EXISTING, 0, 0 );
|
50 | if( hCom==INVALID_HANDLE_VALUE ) {
|
51 | printf( "\terror: COM%s is not available.", argv[1] );
|
52 | return -2;
|
53 | }
|
54 |
|
55 | for( quit=0; !quit; ) {
|
56 |
|
57 | printf( " com%s < ", argv[1] );
|
58 | gets( text );
|
59 |
|
60 | for( ptr=text; *ptr<=' '; ptr++ )
|
61 | ;
|
62 |
|
63 | switch( *ptr ) {
|
64 | case 'Q':
|
65 | case 'q': quit = 1;
|
66 | break;
|
67 | case 'S':
|
68 | case 's': for( ptr++; *ptr && *ptr!='0' && *ptr!='1'; ptr++ )
|
69 | ;
|
70 | if( *ptr ) {
|
71 | n = *ptr - '0';
|
72 | for( ptr++; *ptr && *ptr!='0' && *ptr!='1'; ptr++ )
|
73 | ;
|
74 | if( *ptr ) {
|
75 | v = *ptr - '0';
|
76 | EscapeCommFunction( hCom,
|
77 | n? (v? SETDTR:CLRDTR):(v? SETBREAK:CLRBREAK) );
|
78 | }
|
79 | }
|
80 | break;
|
81 | default:
|
82 | n = hex2bin( data, text );
|
83 | WriteFile( hCom, data, n, &rwlen, 0 );
|
84 | Sleep(100 );
|
85 | ReadFile( hCom, data, rwlen, &rwlen, 0 );
|
86 |
|
87 | bin2hex( text, data, n );
|
88 | printf( " com%s >%s\n", argv[1], text );
|
89 | }
|
90 | }
|
91 |
|
92 | CloseHandle( hCom );
|
93 | return 0;
|
94 | }
|