哪位大侠有VC++ API编写的串口通信例子!
本人试了很多次连打开com口都没成功!:(
没办法用mscomm32.ocx还好!
参考文章:
http://www.chinaithero.com/down2/neteork/serialcom.zip
楼上的就是!
我一直用www.codeproject.com上的一个类,它是用事件来控制读或写动作的,用消息来通知数据到达,稍微改一下就可以完全搞定了
建议给你一本本书《 串口通信编程实例导航〉》
人民邮电出版社出版!
同意楼上的观点
啸风工作室上面有很多文章,并且有串口调试助手的源代码。
to 楼主:
cserialport类确实有这种问题,我以前解决过下面的贴子有解决,
http://expert.csdn.net/expert/topic/1505/1505867.xml?temp=.2344019
/////////////////////////////////////////////////////////////////////
回复人: athlonxpx86(一滴水) ( ) 信誉:106 2003-3-8 17:01:07 得分:0
你把void cserialport::receivechar稍微改一下,我觉得他的效率有些差,他每接收到一个字节数据就会给你的程序发消息,这样你的程序每秒钟要处理19200个消息,这样当然慢。我改了一下这个程序,你看看怎样,当然你的程序也需要改变,你自己的代码还是你改比较合适,我只提醒你注意long cxstrendctrl::oncommunication(wparam ch, lparam port)的第一个参数将变成一个指向30个字符的字符传指针。后面就看你的了
void cserialport::receivechar(cserialport* port, comstat comstat)
{
bool bread = true;
bool bresult = true;
dword dwerror = 0;
dword bytesread = 0;
char *rxbuff=new char[30];//改成30个字符
for (;;)
{
// gain ownership of the comm port critical section.
// this process guarantees no other part of this program
// is using the port object.
entercriticalsection(&port->m_cscommunicationsync);
// clearcommerror() will update the comstat structure and
// clear any other errors.
bresult = clearcommerror(port->m_hcomm, &dwerror, &comstat);
leavecriticalsection(&port->m_cscommunicationsync);
// start forever loop. i use this type of loop because i
// do not know at runtime how many loops this will have to
// run. my solution is to start a forever loop and to
// break out of it when i have processed all of the
// data available. be careful with this approach and
// be sure your loop will exit.
// my reasons for this are not as clear in this sample
// as it is in my production code, but i have found this
// solutiion to be the most efficient way to do this.
if (comstat.cbinque == 0)
{
// break out when all bytes have been read
break;
}
entercriticalsection(&port->m_cscommunicationsync);
if (bread)
{
bresult = readfile(port->m_hcomm, // handle to comm port
rxbuff, // rx buffer pointer
30, // read 30 byte
&bytesread, // stores number of bytes read
&port->m_ov); // pointer to the m_ov structure
// deal with the error code
if (!bresult)
{
switch (dwerror = getlasterror())
{
case error_io_pending:
{
// asynchronous i/o is still in progress
// proceed on to getoverlappedresults();
bread = false;
break;
}
default:
{
// another error has occured. process this error.
port->processerrormessage("readfile()");
break;
}
}
}
else
{
// readfile() returned complete. it is not necessary to call getoverlappedresults()
bread = true;
}
} // close if (bread)
if (!bread)
{
bread = true;
bresult = getoverlappedresult(port->m_hcomm, // handle to comm port
&port->m_ov, // overlapped structure
&bytesread, // stores number of bytes read
true); // wait flag
// deal with the error code
if (!bresult)
{
port->processerrormessage("getoverlappedresults() in readfile()");
}
} // close if (!bread)
leavecriticalsection(&port->m_cscommunicationsync);
// notify parent that a byte was received
::sendmessage((port->m_powner)->m_hwnd, wm_comm_rxchar, (wparam) rxbuff, (lparam) port->m_nportnr);
} // end forever loop
}
//////////////////////////////////////////////////
不过按照你的要求还要注意一些问题,在你的消息函数中要是释放指针,
Posted in noname. Edit