计算机串口通信中,如何监测上一次IO写操作是否已经完成?或者如何监测串口已无数据,可以发送后续数据
解决方案 »
- 高考失利,我该如何选择?【原创】
- static CBitmap 如何初始化?
- 问有没有人有DLL有关的书!!!!
- 怎么给写好的VC程序打包呀?
- 如何获取一个空文档客户区的句柄或者指针,谢谢
- 送分问题:如何对某一范围的图像进行复制?(50分)
- 终于Double stars了,高兴ing,送大家一份薄利,祝大家新年快乐!
- IHTMLDocument2::get_selection(IHTMLSelectionObject**)函数可以获得HTML文件当前选定的对象的指针,但不知如何得到对象的名称(ID)及属性
- FTP的传输数据流分析
- 为什么dll调试跟踪不进去
- Cdatagrid创建失败。。。。。。。。。。。。。。。急急
- 求助:为什么我用异步串口通信时,写串口返回总是0,即不成功?
缓冲区
----------------------------------------
相信自己,相信明天!快给我分,不然我抢的啦^_^
----------------------------------------
时光.漫步
ComStat.cbInQue看它是否是空。
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM2"; hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
); if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
} // Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm. fSuccess = GetCommState(hCom, &dcb); if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
} // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit. dcb.BaudRate = CBR_57600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
dcb.fAbortOnError=TRUE; //出错调用ClearCommError(); fSuccess = SetCommState(hCom, &dcb); if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
} printf ("Serial port %s successfully reconfigured.\n", pcCommPort); //出错时候的代码:
COMSTAT comsat;
DWORD dwError;
ClearCommError(hComm,&dwError,comsat);
int m_nDataRemain=comsat.cbOutQue;//获得没有发送的数据个数
TRY
{
//发送数据
WriteFile(hComm,databuf,datalen,nSentCount,NULL);}
CATCH(CException, e)
{
COMSTAT comsat;
DWORD dwError;
ClearCommError(hComm,&dwError,comsat);
int m_nDataRemain=comsat.cbOutQue;//获得没有发送的数据个数
}
END_CATCH
我使用的是CSerialPort类,它发送串口数据的时候,没有检查串口缓冲区是否有数据,而是直接发送,不管覆不覆盖。
http://www.vckbase.com/SYS/script/viewcomment.asp?gclsid=100&itemid=612
要查看是否已经发送完,用SetWaitMask,在一个线程里用WaitCommEvent