Apr 2nd, 2004, 06:09 | #1 |
Senior Member
注册日期: Jul 2004
帖子: 390
|
我参照例子, 用CREATEFILE取的COM1的HANDLE, 然后设置其配置, 最后, 用READFILE来获取COM1的数据, 但是, 每次一到READFILE, 就会报 INVALID HANDLE的错误, why? 大致代码: //取得COM1的HANDLE HANDLE m_Port = CreateFile("COM1:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); if (m_Port == INVALID_HANDLE_VALUE){ // Handle the error. return false ; } // 配置COM1 DCB dcb; if (!GetCommState(m_Port, &dcb)){ return false; } dcb.BaudRate = CBR_9600; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit if (!SetCommState(m_Port, &dcb)){ return false; } //读取数据 char input[200]; unsigned int lenBuff = 200; LPDWORD lenMessage; String *strResult; if(ReadFile(m_Port,input,lenBuff,lenMessage,NULL)==0){ //这里会有EXCEPTION return false; } |
|
Apr 2nd, 2004, 17:43 | 只看该作者 #4 |
Member
注册日期: Jul 2004
帖子: 56
声望: 0
|
<BLOCKQUOTE class="ip-ubbcode-quote"><font size="-1">quote:</font><HR>Originally posted by rainzw: 连什么DLL? 我只是用了: #INCLUDE <WINDOWS.H> 这个是WIN32的API吧? 怎么都不行, CLOSEHANDLE也不行. 该查什么书呢? MICROSOFT的MSDN我都看了无数遍了... <HR></BLOCKQUOTE> Hehe. MSDN is so big library. Countless? Beat me hard. oK. if(ReadFile(m_Port,input,lenBuff,lenMessage,NULL)==0){ //这里会有EXCEPTION return false; } THE ERROR:::: lenMessage IS A POINTER. Initialize before invocation of the function. Changed to: DWORD lenMessage; String *strResult; if(ReadFile(m_Port,input,lenBuff,&lenMessage,NULL)==0){ |
|
Apr 2nd, 2004, 17:51 | 只看该作者 #5 |
Member
注册日期: Jul 2004
帖子: 56
声望: 0
|
Another thing: I am seasoned windows programmer. I wrote this kind of programs countless times. But I just forgot it when I didn't write this program for months. I once wrote serial driver for MS. I know there is still a bug in MS windows. But I just cannot remmember anything about the syntax of COMM usage. But I can guess the place where you are wrong, because you are not a competent programmer. Suggest you consider your future as a C programmer. |
|