并口打印机的通信
WINDOWS我做一个与爱普生并口打印机的通信,打印号票,找不到好办法希望你能帮我解决一下。
1.我查了一些资料说WINIO类。看了一下,SETPORTVAL GETPORTVAL 发现是对硬件直接操作,什么高底电这样,不知道发一个字符串过去怎么调用这两个函数,引脚高底位的流程。然后取打印机状态。
2.还有就是用CRETEFILE 可以发送数据,但是用READFILE取打印机缺纸时就阻塞没有数据返回。
有没有直接操作并口打印机的方法??我想直接发打印数据过去(例如一个函数SENDDATE()),不操作并口25个针。因为我对这个不太熟习。
如果大家有什么类最好啦。
是并口打印机。
谢谢大家 邮箱wapjia@yahoo.com.cn
[解决办法]
发错,上面是usb的了,这里是头文件
#ifndef _PRNPORT_H_VICTORCHEN_
#define _PRNPORT_H_VICTORCHEN_
/*-------------------------------------*\
| 直接控制打印口(并行口) |
| Copyright (C) Victor Chen |
| Homepage: http://www.cppfans.com/ |
\*-------------------------------------*/
#include <vcl.h>
//---------------------------------------
extern unsigned char (*inportb)(unsigned short);
extern unsigned short (*inportw)(unsigned short);
extern unsigned long (*inportd)(unsigned short);
extern void (*outportb)(unsigned short, unsigned char );
extern void (*outportw)(unsigned short, unsigned short);
extern void (*outportd)(unsigned short, unsigned long );
//---------------------------------------
class TWinIo
{
public:
__property bool IsValid = { read = fGetValid };
bool (_stdcall *InitializeWinIo)();
void (_stdcall *ShutdownWinIo)();
PBYTE (_stdcall *MapPhysToLin)(PBYTE pbPhysAddr, DWORD dwPhysSize, HANDLE *pPhysicalMemoryHandle);
bool (_stdcall *UnmapPhysicalMemory)(HANDLE PhysicalMemoryHandle, PBYTE pbLinAddr);
bool (_stdcall *GetPhysLong)(PBYTE pbPhysAddr, PDWORD pdwPhysVal);
bool (_stdcall *SetPhysLong)(PBYTE pbPhysAddr, DWORD dwPhysVal);
bool (_stdcall *GetPortVal)(WORD wPortAddr, PDWORD pdwPortVal, BYTE bSize);
bool (_stdcall *SetPortVal)(WORD wPortAddr, DWORD dwPortVal, BYTE bSize);
bool (_stdcall *InstallWinIoDriver)(PSTR pszWinIoDriverPath, bool IsDemandLoaded = false);
bool (_stdcall *RemoveWinIoDriver)();
bool LoadWinIo(char *lpFileName);
void FreeWinIo(void);
TWinIo(char *lpFileName);
~TWinIo();
private:
bool _bInitialized;
HINSTANCE hWinIo;
void InitPortFuncs(void);
bool fGetValid(void) {return hWinIo!=NULL;}
};
//---------------------------------------
// Parallel port class definition
//---------------------------------------
/*
打印口各引脚的定义:
1.选通, PC->Printer
2-9.数据(D0-D7)
10.应答(ACK),Printer->PC
11.忙(BUSY), Printer->PC
12.无纸(PE), Printer->PC
13.联机(SLCT), Printer->PC
14.自动换行(AUTO FD), PC->Printer
15.错误(ERROR), Printer->PC
16.初始化(INIT),PC->Printer
17.选择输出(SELECT IN), PC->Printer
18-25.地(GND)
*/
class TParallelPort
{
public:
int Send(unsigned char data) ;
//参数与标准 C 的 printf 相同, 返回值为错误码, 这个函数是从打印机输出数据
int printf(const char *format,...);
//参数与标准 C 的 vprintf 相同, 这个函数是从打印机输出数据
int vprintf(const char* format, va_list paramList);
//初始化打印机, 两个参数分别是初始化时间和初始化之后的延时(ms)
void InitPrinter(int iInitDelay=5000, int iInitWait=1000);
//并行口的数据 D0-D7, 可读写
__property unsigned char Data = { read=fGetData , write=fSetData };
//并行口的状态, 包括 ~BUSY, ACK, PE, ONLINE, ~ERROR 等, 这个属性是只读的
__property unsigned char Flag = { read=fGetFlag , write=fSetFlag };
//并行口的状态, 包括 ~INIT, AUTOFD, STORE 等, 这些属性是可写的。
__property unsigned char Ctrl = { read=fGetCtrl , write=fSetCtrl };
//并行口的开始地址, 默认值为 0x378
__property unsigned short Base = { read=_DataPort, write=fSetPort };
enum TPrnError
{
peNoError = 0,
peNotOnLine = -1,
peOutOfPaper = -2,
pePrinterErr = -3,
};
enum TPrnFlags
{
pfIdle = 0x80, //F7 = ~BUSY 1: idle 0 : busy
pfAck = 0x40, //F6 = ACK 1: ack 0 : no ack
pfPE = 0x20, //F5 = PE 1: paper out 0 : paper in
pfOnLine = 0x10, //F4 = ONLINE 1: select in 0 : not select
pfNoError = 0x08, //F3 = ~ERROR 1: no error 0 : error
};
enum TPrnCtrls
{
pcIRQE = 0x10, //C4
pcSelect = 0x08, //C3
pcFInit = 0x04, //C2 = ~INIT
pcAutoFD = 0x02, //C1
pcStore = 0x01, //C0 = STORE
};
//这个属性是控制打印的, 默认为 false, 如果这个属性为 true, 打印不会抛出异常, 而是返回错误码
__property bool NoException = { read = _bNoException, write = _bNoException };
//产生错误时会触发这个事件, 默认的处理是询问是否重试
__property bool __fastcall (__closure *OnMakeSure)(TPrnError ErrCode) = { read = _lpMakeSure, write = _lpMakeSure };
int PrintString(char *lpLine);
TParallelPort(unsigned short p=0x378);
unsigned char ReadStatus() ;
private:
inline unsigned char fGetData(void)
{
return inportb(_DataPort );
}
inline unsigned char fGetFlag(void)
{
return inportb(_StatusPort);
}
inline unsigned char fGetCtrl(void)
{
return inportb(_CtrlPort );
}
inline void fSetData(unsigned char d)
{
outportb(_DataPort , d);
}
inline void fSetFlag(unsigned char d)
{
outportb(_StatusPort, d);
}
inline void fSetCtrl(unsigned char d)
{
outportb(_CtrlPort , d);
}
void fSetPort(unsigned short p);
unsigned short _DataPort, _StatusPort, _CtrlPort;
bool _bNoException;
bool _fMakeSure(TPrnError ErrCode);
bool __fastcall (__closure *_lpMakeSure)(TPrnError ErrCode); //return retry;
};
//---------------------------------------
extern TWinIo WinIo;
extern TParallelPort ParallelPort;
//---------------------------------------
#endif