串口状态检测?急

zhutaozhi53 2003-08-11 04:26:59
串口状态检测,
我要写一个程序,检测串口的各个脚的变化,我以前是可以设置,但我不知道是怎么检测它的变化?
...全文
157 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhutaozhi53 2003-08-15
  • 打赏
  • 举报
回复
谢谢了,我已经做好了,只是检测一下各种脚的电平的状态
如果发现电平有变化,则发生事件就好了!
zhutaozhi53 2003-08-12
  • 打赏
  • 举报
回复
NowCan
能不能把你的程序说清楚一下了?》
zhutaozhi53 2003-08-12
  • 打赏
  • 举报
回复
GetCommModemStatus

我用了,取出来的状态好像没有变化过。
是用EscapeCommFunction设置的。
Jagen在路上 2003-08-11
  • 打赏
  • 举报
回复
GetCommModemStatus
参看MSDN它的调用方式
NowCan 2003-08-11
  • 打赏
  • 举报
回复
WaitCommEvent

EV_BREAK A break was detected on input.
* EV_CTS The CTS (clear-to-send) signal changed state.
* EV_DSR The DSR (data-set-ready) signal changed state.
EV_ERR A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY.
* EV_RING A ring indicator was detected.
* EV_RLSD The RLSD (receive-line-signal-detect) signal changed state.
EV_RXCHAR A character was received and placed in the input buffer.
EV_RXFLAG The event character was received and placed in the input buffer. The event character is specified in the device's DCB structure, which is applied to a serial port by using the SetCommState function.
EV_TXEMPTY The last character in the output buffer was sent.

Reference from MSDN.

Monitoring Communications Events
The following example code opens the serial port for overlapped I/O, creates an event mask to monitor CTS and DSR signals, and then waits for an event to occur. The WaitCommEvent function should be executed as an overlapped operation so the other threads of the process cannot perform I/O operations during the wait.

HANDLE hCom;
OVERLAPPED o;
BOOL fSuccess;
DWORD dwEvtMask;

hCom = CreateFile( "COM1",
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);

if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
}

// Set the event mask.

fSuccess = SetCommMask(hCom, EV_CTS | EV_DSR);

if (!fSuccess)
{
// Handle the error.
}

// Create an event object for use in WaitCommEvent.

o.hEvent = CreateEvent(
NULL, // no security attributes
FALSE, // auto reset event
FALSE, // not signaled
NULL // no name
);

assert(o.hEvent);

if (WaitCommEvent(hCom, &dwEvtMask, &o))
{
if (dwEvtMask & EV_DSR)
{
// To do.
}

if (dwEvtMask & EV_CTS)
{
// To do.
}
}
winapp 2003-08-11
  • 打赏
  • 举报
回复
HANDLE h = INVALID_HANDLE_VALUE;
AnsiString Comname = "\\\\.\\COM1";
h = CreateFile( Comname.c_str(),GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_aTTRIBUTE,0);
if ( h = INVALID_HANDLE_VALUE )
{
DWORD erro = ::GetLastErro();
if (erro != 2)
Label1->Caption = "未使用!";
}
else
Label1->Caption = "使用中!";
CloseHandle(h);

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧