c语言中can口通信可以用waitcommevent函数吗,關於串口通訊中的WaitCommEvent函數

MSDN:

Waits for an event to occur for a specified communications device. The set of events that are monitored by this function is contained in the event mask associated with the device handle.

/*

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 can perform I/O operations during the wait.

*/

#include 

#include 

#include 

void main( )

{

HANDLE hCom;

OVERLAPPED o;

BOOL fSuccess;

DWORD dwEvtMask;

hCom = CreateFile( TEXT("COM1"),

GENERIC_READ | GENERIC_WRITE,

0,    // exclusive access

NULL, // default security attributes

OPEN_EXISTING,

FILE_FLAG_OVERLAPPED,

NULL

);

if (hCom == INVALID_HANDLE_VALUE)

{

// Handle the error.

printf("CreateFile failed with error %d.\n", GetLastError());

return;

}

// Set the event mask.

fSuccess = SetCommMask(hCom, EV_CTS | EV_DSR);

if (!fSuccess)

{

// Handle the error.

printf("SetCommMask failed with error %d.\n", GetLastError());

return;

}

// Create an event object for use by WaitCommEvent.

o.hEvent = CreateEvent(

NULL,   // default security attributes

TRUE,   // manual-reset event

FALSE,  // not signaled

NULL    // no name

);

// Initialize the rest of the OVERLAPPED structure to zero.

o.Internal = 0;

o.InternalHigh = 0;

o.Offset = 0;

o.OffsetHigh = 0;

assert(o.hEvent);

if (WaitCommEvent(hCom, &dwEvtMask, &o))

{

if (dwEvtMask & EV_DSR)

{

// To do.

}

if (dwEvtMask & EV_CTS)

{

// To do.

}

}

else

{

DWORD dwRet = GetLastError();

if( ERROR_IO_PENDING == dwRet)

{

printf("I/O is pending...\n");

// To do.

}

else

printf("Wait failed with error %d.\n", GetLastError());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值