并口控制

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




// 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 );
}
...全文
91 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
UltraBejing 2008-04-30
有点难度哦
回复
不明LZ在说什么
回复
可口可乐 2008-04-21
unsigned char abc='0';
unsigned char *Signal = &abc;
这一句可能有问题。
abc是个char,
Signal本意是个字符串,指向abc,但abc并不是以'\0'结尾的字符串

同时Num_Transfered = sizeof( Signal );
得到的是Signal作为指针的长度,始终为4,
本意应该是Signal中字符的个数。

改成
unsigned char *Signal = "0";
Num_Transfered = strlen(Signal);
试试看

回复
相关推荐
发帖
数据结构与算法
创建于2007-08-27

3.2w+

社区成员

数据结构与算法相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2008-04-21 08:56
社区公告
暂无公告