并口控制程序

tommy851213 2008-04-21 08:54:20
一下程序是控制并口 数据存储器的
我打开并口成功了
但是写入失败
请问问题出在哪儿?




// 44.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<windows.h>
#include<iostream.h>

void main()
{
unsigned long Num_Transfered;
OVERLAPPED overlapped;
unsigned char abc='0';
unsigned char *Signal = &abc;
unsigned char Read = 0;
HANDLE hPort;

overlapped.Internal = 0;
overlapped.InternalHigh = 0;
overlapped.Offset = 0;
overlapped.OffsetHigh = 0;
overlapped.hEvent = NULL;

hPort = CreateFile( "LPT1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, NULL );
Sleep( 10 );
if( hPort==INVALID_HANDLE_VALUE )
{
cout<<"Port Open Failed!"<<endl;
//exit(0);
}
else
{

//Signal = 0;
BOOL b=false;
Num_Transfered = sizeof( Signal );
b=WriteFile( hPort,Signal, Num_Transfered, &Num_Transfered, &overlapped );
Sleep( 250 );
cout<<b<<endl;
b=ReadFile( hPort, &Read, Num_Transfered, &Num_Transfered, &overlapped );
cout<<b<<endl;
Sleep( 250 );
int a;
scanf("%d",&a);
/*Signal = 1;
WriteFile( hPort, &Signal, Num_Transfered, &Num_Transfered, &overlapped );
Sleep( 250 );
ReadFile( hPort, &Read, Num_Transfered, &Num_Transfered, &overlapped );
cout<<int(Read)<<endl;
Sleep( 250 ); */
}

CloseHandle( hPort );
}
...全文
159 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
子正 2008-04-24
  • 打赏
  • 举报
回复
为什么你的Overlapped struct没有给hEvent创建事件对象?
另外你既然选择了异步读写,为什么没有异步读写的处理代码?

Windows API 中 WriteFile的参数说明:
lpOverlapped

Points to an OVERLAPPED structure. This structure is required if hFile was opened with FILE_FLAG_OVERLAPPED.
(这个结构体当hFile是以FILE_FALG_OVERLAPPED模式打开时,必须)

If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a valid OVERLAPPED structure. If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the function can incorrectly report that the write operation is complete.
(如果lpOverlapped是NULL,那么程序的返回值可能始终为TRUE)

If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile may return before the write operation has been completed. In this case, WriteFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue processing while the write operation is being completed. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the write operation.
(如果lpOverlapped非空,写操作可能在写过程完成之前就返回FALSE,然后GetLastError()返回ERROR_IO_PENDING,此时调用进程可以处理其他的事物,OVERLAPPED中的事件在读写完成后会置为Signaled。)

If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the write operation starts at the current file position and WriteFile does not return until the operation has been completed.
(如果hFile并不是用FILE_FLAG_OVERLAPPED打开的,并且lpOverlapped为空,此时WriteFile的行为是标准的同步读写行为。)

If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile does not return until the write operation has been completed.
(如果hFile并不是用FILE_FLAG_OVERLAPPED打开的,并且lpOverlapped不为空,则写操作会参照OVERLAPPED结构体中定义的偏移址来展开同步读写工作。)


现在是不是清楚一点了?如果你之前没有用过异步传输,那么建议你先采用同步的方式实验一下。不过异步读写对于通讯而言几乎是必须的方式,仔细研究一下吧。等待事件要用到WaitForSingleObject,这个函数在多线程的环境中也会经常用到。
过客猫2022 2008-04-24
  • 打赏
  • 举报
回复
路过,帮顶

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧