我参照例子, 用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; } |
- 连DLL库了吗? (别问我怎么连 http://chinasmile.infopop.net/infopo...icon_smile.gif )
- 关COM1了吗?是不是机器启动第一次行,以后再不行了?每次要Closehandle - 别的问题就该查书了 http://chinasmile.infopop.net/infopo...icon_frown.gif |
连什么DLL? 我只是用了: #INCLUDE <WINDOWS.H> 这个是WIN32的API吧?
怎么都不行, CLOSEHANDLE也不行. 该查什么书呢? MICROSOFT的MSDN我都看了无数遍了... |
<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){ |
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. |
所有时间均为格林尼治时间 -4。现在的时间是 18:03。 |
Copyright © 1999-2024 Chinasmile