返回   华枫论坛 > ◆ 工作学习◆ > IT交流



发表新主题 回复
 
只看楼主 主题工具
旧 Apr 2nd, 2004, 06:09     #1
rainzw
Senior Member
级别:0 | 在线时长:0小时 | 升级还需:5小时
 
注册日期: Jul 2004
帖子: 390
rainzw will become famous soon enough
默认

我参照例子, 用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;
}
rainzw 当前离线  
回复时引用此帖
旧 Apr 2nd, 2004, 08:39   只看该作者   #2
lungao
级别:35 | 在线时长:1401小时 | 升级还需:39小时级别:35 | 在线时长:1401小时 | 升级还需:39小时级别:35 | 在线时长:1401小时 | 升级还需:39小时
 
lungao 的头像
 
注册日期: Jul 2004
帖子: 1,241
积分:3
精华:1
声望: 48024
lungao has a reputation beyond reputelungao has a reputation beyond reputelungao has a reputation beyond reputelungao has a reputation beyond reputelungao has a reputation beyond reputelungao has a reputation beyond reputelungao has a reputation beyond reputelungao has a reputation beyond reputelungao has a reputation beyond reputelungao has a reputation beyond reputelungao has a reputation beyond repute
默认

- 连DLL库了吗? (别问我怎么连 http://chinasmile.infopop.net/infopop/emoticons/icon_smile.gif )

- 关COM1了吗?是不是机器启动第一次行,以后再不行了?每次要Closehandle

- 别的问题就该查书了 http://chinasmile.infopop.net/infopop/emoticons/icon_frown.gif
lungao 当前离线  
回复时引用此帖
旧 Apr 2nd, 2004, 12:14   只看该作者   #3
rainzw
Senior Member
级别:0 | 在线时长:0小时 | 升级还需:5小时
 
注册日期: Jul 2004
帖子: 390
声望: 92
rainzw will become famous soon enough
默认

连什么DLL? 我只是用了: #INCLUDE <WINDOWS.H> 这个是WIN32的API吧?

怎么都不行, CLOSEHANDLE也不行.

该查什么书呢? MICROSOFT的MSDN我都看了无数遍了...
rainzw 当前离线  
回复时引用此帖
旧 Apr 2nd, 2004, 17:43   只看该作者   #4
please
Member
级别:0 | 在线时长:0小时 | 升级还需:5小时
 
注册日期: Jul 2004
帖子: 56
声望: 0
please is an unknown quantity at this point
默认

<BLOCKQUOTE class="ip-ubbcode-quote"><font size="-1">quote:</font><HR>Originally posted by rainzw:
连什么DLL? 我只是用了: #INCLUDE &lt;WINDOWS.H&gt; 这个是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){
please 当前离线  
回复时引用此帖
旧 Apr 2nd, 2004, 17:51   只看该作者   #5
please
Member
级别:0 | 在线时长:0小时 | 升级还需:5小时
 
注册日期: Jul 2004
帖子: 56
声望: 0
please is an unknown quantity at this point
默认

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.
please 当前离线  
回复时引用此帖
发表新主题 回复

主题工具

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码



所有时间均为格林尼治时间 -4。现在的时间是 10:02

请尊重文章原创者,转帖请注明来源及原作者。
凡是本站用户自行发布的任何信息,皆不代表本站的立场,
华枫网站不确保各类信息的正确性和可靠性,也不承担由此而导致的任何直接或间接损失以及任何法律责任。

Copyright © 1999-2024 Chinasmile