本文介绍了Windows平台C中控制串口的DTR和RTS管脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在windows平台上控制串口的DTR和RTS引脚?我希望它通过提高或降低电压来进行位冲击或操作.
How to Control DTR and RTS pin of serial port in and on a windows platform? I want it to be bitbanged or operated by raising or lowering its voltage.
推荐答案
您需要使用 EscapeCommFunction 函数,像这样:
You'll need to use the EscapeCommFunction function, like so:
// 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;
}
这篇关于Windows平台C中控制串口的DTR和RTS管脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!