2013-08-30 194 views

回答

4
Try this: 


// winserial_io.cpp : Win32 test program to control RTS and DTS output lines 
// Originator: Steven Woon 
// Creation Date: 2007-12-15 

#include "stdafx.h" 
#include "windows.h" 
#include <conio.h> 

//#include "winbase.h" 

int main(int argc, char* argv[]) 
{ 
    HANDLE hComm; 
    char ch; 

    for (int i = 0; i < argc; i++) 
     printf("%s\n", argv[i]); 

    hComm = CreateFileA(argv[1],GENERIC_READ | GENERIC_WRITE, 
         0, 
         0, 
         OPEN_EXISTING, 
         NULL, 
         0); 

    if (hComm == INVALID_HANDLE_VALUE) 
    { 
     printf("Cannot open %s\n", argv[1]);  //error occured alert user about error 
     return -1; 
    } 

    printf("Press the following keys:\n"); 
    printf("1: Set DTR\n"); 
    printf("2: Clear DTR\n"); 
    printf("3: Set RTS\n"); 
    printf("4: Clear RTS\n"); 
    printf("q: End Program\n"); 

    do 
    { 
     ch = _getch(); 
     switch (ch) 
     { 
     case '1': if (EscapeCommFunction(hComm,SETDTR) == 0) 
         printf ("Error Setting DTR\n"); 
        break; 
     case '2': if (EscapeCommFunction(hComm,CLRDTR) == 0) 
         printf ("Error Clearing DTR\n"); 
        break; 
     case '3': if (EscapeCommFunction(hComm,SETRTS) == 0) 
         printf ("Error Setting CTS\n"); 
        break; 
     case '4': if (EscapeCommFunction(hComm,CLRRTS) == 0) 
         printf ("Error Clearing CTS\n"); 
        break; 
     } 
    } while (ch != 'q'); 


    return 0; 
}