华中科技大学计算机学院接口课程设计.vc制作,上位机下位机模拟商场贵宾卡系统.使用access数据库.
源代码在线查看: myport.cpp
#include "stdafx.h"
#include "conio.h"
#include "windows.h"
#include
#include "myport.h"
HANDLE PortTalk_Handle = NULL; /* Handle for PortTalk Driver */
unsigned char OpenPortTalk()
{
PortTalk_Handle = CreateFile("\\\\.\\MPNP1",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(PortTalk_Handle == INVALID_HANDLE_VALUE)
{
return -1;
}
return 0;
}
void ClosePortTalk()
{
CloseHandle(PortTalk_Handle);
}
void outportb(unsigned short PortAddress, unsigned char byte)
{
DWORD dwError = 0;
unsigned int error;
DWORD BytesReturned;
unsigned char Buffer[2];
unsigned char * pBuffer;
pBuffer = (unsigned char *)&Buffer[0];
*pBuffer = (unsigned char)PortAddress&0x0f;
// *pBuffer = (unsigned char)PortAddress;
Buffer[2] = byte;
error = DeviceIoControl(PortTalk_Handle,
IOCTL_WRITE_PORT_UCHAR,
&Buffer,
3,
NULL,
0,
&BytesReturned,
NULL);
//if (!error) printf("Error occured during outportb while talking to PortTalk driver %d\n",GetLastError());
}
unsigned char inportb(unsigned short PortAddress)
{
unsigned int error;
DWORD BytesReturned;
unsigned char Buffer[3];
unsigned short * pBuffer;
pBuffer = (unsigned short *)&Buffer[0];
*pBuffer = (unsigned char)PortAddress&0x0f;
//*pBuffer = (unsigned char)PortAddress;
error = DeviceIoControl(PortTalk_Handle,
IOCTL_READ_PORT_UCHAR,
&Buffer,
2,
&Buffer,
1,
&BytesReturned,
NULL);
//if (!error) printf("Error occured during inportb while talking to PortTalk driver %d\n",GetLastError());
return(Buffer[0]);
}