阅读:2611回复:24
在2000下对并口的控制问题
在2000下对并口的控制问题,困扰了我很久了!
HANDLE hPort1 =CreateFile(_T(\"\\\\\\\\.\\\\LPT1\"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); BYTE byte; byte=0; DWORD number,num; number=1; int buffer=2; UpdateData(TRUE); HANDLE event=CreateEvent( NULL, // no security attributes TRUE, // manual-reset event TRUE, // initial state is signaled \"hoco\" // object name ); OVERLAPPED test; test.Offset=0; test.OffsetHigh=0; test.hEvent=event; int ret2=HasOverlappedIoCompleted(&test); int ret1=WriteFile(hPort1,\"7\",number,&num,&test); `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 不成功啊!怎么回事啊? if(GetLastError()==ERROR_IO_PENDING) AfxMessageBox(\"ERROR_IO_PENDING\"); 这到底是怎么回事啊? |
|
沙发#
发布于:2002-04-10 23:30
2K下面要写DRIVER呀!你的是用的DRIVER吗?
|
|
|
板凳#
发布于:2002-04-11 14:05
没有啊,在2000下除了写驱动以外还有什么办法吗?
他们说用CreateFile() 和ReadFile()、WriteFile(),但是不知道为什么我的程序总是有点问题,谁能帮忙看看吗? HANDLE hPort1 =CreateFile(\"LPT1\", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,NULL); BYTE byte; byte=0; DWORD number,num; number=1; int buffer=2; UpdateData(TRUE); HANDLE event=CreateEvent( NULL, TRUE, FALSE, NULL); OVERLAPPED test; test.Offset=0; test.OffsetHigh=0; test.hEvent=event; unsigned char ucByte; ucByte=\'7\'; int ret1=WriteFile(hPort1, (LPSTR) &ucByte,number,&num,&test); ~~~~~~~~~~~~~~~~~~`` ret1得值总是为0,向端口写数据不成功啊! if(GetLastError()==ERROR_IO_PENDING) AfxMessageBox(\"ERROR_IO_PENDING\"); |
|
地板#
发布于:2002-04-28 15:33
大家能够帮忙看看码?
能够不写驱动程序而用api在2000下直接控制 并口吗?或者是大家有什么开发包啊或者是之类的驱动,能否 给小弟呢? 急!!!!!!!!!!!! 谢谢大家了! |
|
地下室#
发布于:2002-04-28 17:13
应该是可以用你的方法从应用层访问的。
你不能,应该是你的程序问题,仔细检查 |
|
|
5楼#
发布于:2002-04-28 18:24
|
|
6楼#
发布于:2002-04-28 18:26
大虾,我试了一下,LPT1端口试打开了的,但是
用ReadFile()和WriteFile()不行啊!我觉得没有错啊! 能不能帮忙看看呢?我已经调了很久了啊!:(,救命啊! :) |
|
7楼#
发布于:2002-04-29 09:00
你先用同步IO试一下,然后再用异步IO。偶好久没有写过应用程序了,不知道你的问题是什么。
|
|
|
8楼#
发布于:2002-04-29 12:33
哦,我先用过同步的啊!但是不行,你看过我上面的程序了吗?
不行啊!:( |
|
9楼#
发布于:2002-04-29 12:36
你试一下同步,并且看每一步的返回值。自己慢慢跟踪,你的代码我看不懂了,哈哈
|
|
|
10楼#
发布于:2002-04-29 13:41
//打开端口LPT1
HANDLE hPort1 =CreateFile(\"LPT1\", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,NULL); //成功! CString buffer; buffer=\"1\"; //然后同步向端口写数据 int ret1=WriteFile(hPort1,&buffer,1,0,NULL); ~~~~~~~~~~~~~~~~~~`` ret1得值总是为0,向端口写数据不成功啊! 就是这样子的啊!:(, 现在能不能帮忙看看啊? |
|
11楼#
发布于:2002-04-29 13:55
SORRY,不知道!!!
:-( |
|
|
12楼#
发布于:2002-04-29 14:02
1 你的设备名这样用有没有问题?
2 你的CreateFile的返回值是什么?你是不是检查它为不为零?可能会返回0xFFFFFFFF呢。 3 你在WriteFile后面调用一下GetLastError看看返回值为多少 |
|
|
13楼#
发布于:2002-04-30 17:22
1. createfile 后,看看lasterror是多少。
2. 用了异步(OVERLAPPED flag),ReadFile和WriteFile一般总是立即返回0,lasterror是IO_PENDING,用waitsingleobject等待overlapped的event之后,再读getoverlappedresult。 建议没有必要不要用异步。 createfile时file attr标志直接给0就可以了。 3. 看看系统设备属性里lpt配置好了没有,bios里lpt方式是什么,建议用ecp或epp |
|
14楼#
发布于:2002-05-02 10:30
我觉得端口LPT1应该是成功的打开了的。
CreateFile的返回值是0x00000058。 writefile()返回值还是0 GetLastError()返回值为87啊! 大虾,是怎么回事呢? |
|
15楼#
发布于:2002-05-02 11:11
GetLastError()返回值为87,说明你传递的参数有问题:
87 The parameter is incorrect. ERROR_INVALID_PARAMETER |
|
|
16楼#
发布于:2002-05-02 11:24
哦,这样啊!呵呵,
那你能否帮我看看啊? int ret1=WriteFile(hPort1,&buffer,1,0,NULL); |
|
17楼#
发布于:2002-05-02 11:56
CreateFile 使用了OVERLAPPED 标志,WriteFile 中的OVERLAPPED 不应为空 ?
|
|
18楼#
发布于:2002-05-02 12:20
看看msdn是怎么说的有关于最后两个参数:
lpNumberOfBytesWritten [out] Pointer to the variable that receives the number of bytes written. WriteFile sets this value to zero before doing any work or error checking. Windows NT/2000/XP: If lpOverlapped is NULL, lpNumberOfBytesWritten cannot be NULL. If lpOverlapped is not NULL, lpNumberOfBytesWritten can be NULL. If this is an overlapped write operation, you can get the number of bytes written by calling GetOverlappedResult. If hFile is associated with an I/O completion port, you can get the number of bytes written by calling GetQueuedCompletionStatus. If I/O completion ports are used and you are using a callback routine to free the memory allocated to the OVERLAPPED structure pointed to by the lpOverlapped parameter, specify NULL as the value of this parameter to avoid a memory corruption problem during the deallocation. This memory corruption problem will cause an invalid number of bytes to be returned in this parameter. Windows 95/98/Me: This parameter cannot be NULL. lpOverlapped [in] Pointer to an OVERLAPPED structure. This structure is required if hFile was opened with FILE_FLAG_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. 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. 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. WriteFile resets the event specified by the hEvent member of the OVERLAPPED structure to a nonsignaled state when it begins the I/O operation. Therefore, there is no need for the caller to do so. Windows NT/2000/XP: 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. Windows 95/98/Me: For operations on files, disks, pipes, or mailslots, this parameter must be NULL; a pointer to an OVERLAPPED structure causes the call to fail. However, Windows 95/98/Me supports overlapped I/O on serial and parallel ports. |
|
|
19楼#
发布于:2002-05-03 07:46
哦,其实我原来也看了这个的,但是总是搞不定啊!:(,
谁能告诉我究竟要怎么写呢? 我都快郁闷死了!!!!!!! :(,得填写一个overlap struct,还得设置,一大窜的东西,搞得我的头都打了!:( |
|
上一页
下一页