2

如何在windows平台上控制串口的DTR和RTS引脚?我希望通过提高或降低其电压来对其进行 bitbanged 或操作。

4

1 回答 1

7

您需要使用EscapeCommFunction函数,如下所示:

// 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;
}
于 2013-08-30T19:41:23.203 回答