打印机如何设置超时?
POS打印机,需要直接对打印端口进行操作。
当打印机联机时,没有问题,但是一旦没有联机,程序就没有相应。
原来,缺省的时间时5分钟(GetCommTimeouts)。
使用SetCommTimeouts设置LPT总返回0(错误),设置COM没有问题。
运行环境是windows 2000。
请教如何设置超时或着还有什么其他方法解决检测是否联机。
下面是载网上找到的程序。
-----------------------
bool LptPrint(char prtdata[],int prtlen,int timeout)
{
HANDLE h;
DWORD n;
COMMTIMEOUTS t;
bool result;
h = CreateFile("LPT1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (h == INVALID_HANDLE_VALUE){
AfxMessageBox("Can not open lpt1");
return false;
}
t.ReadIntervalTimeout = 0;
t.ReadTotalTimeoutMultiplier = 0;
t.ReadTotalTimeoutConstant = 0;
t.WriteTotalTimeoutMultiplier = timeout * 1000 / prtlen;
t.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts(h,&t)){
//这里,总是不成功。但是如果将LPT1改成COM1,就可以设置。
AfxMessageBox("SetCommTimeout error");
return false;
}
result = true;
if (!WriteFile(h,prtdata,prtlen,&n,NULL)){
AfxMessageBox("Print error");
result = false;
}
CloseHandle(h);
return result;
}
下面是VB调用API的程序,也是一样
-------------------------------
Public Declare Function GetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Public Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Public Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As OVERLAPPED) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Const OPEN_EXISTING = 3
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const FILE_FLAG_OVERLAPPED = &H40000000
Public Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Type COMMTIMEOUTS
ReadIntervalTimeout As Long
ReadTotalTimeoutMultiplier As Long
ReadTotalTimeoutConstant As Long
WriteTotalTimeoutMultiplier As Long
WriteTotalTimeoutConstant As Long
End Type
Public Sub Test()
Dim str_tmp As String * 1024
Dim xATTR As SECURITY_ATTRIBUTES
Dim nTime As COMMTIMEOUTS
Dim lng_hdc As Long, i As Long
Dim lngPrn As Long
lng_hdc = CreateFile("LPT1", GENERIC_READ + GENERIC_WRITE, 0, xATTR, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0)
If lng_hdc = -1 Then
'错误
Debug.Print "Err."
Exit Sub
End If
Debug.Print "lng_hdc=" & lng_hdc
nTime.ReadIntervalTimeout = 0
nTime.ReadTotalTimeoutConstant = 0
nTime.ReadTotalTimeoutMultiplier = 0
nTime.WriteTotalTimeoutConstant = 10
nTime.WriteTotalTimeoutMultiplier = 10
Debug.Print "SetCommTimeouts=" & SetCommTimeouts(lng_hdc, nTime)
Debug.Print "GetCommTimeouts=" & GetCommTimeouts(lng_hdc, nTime)
Debug.Print "nTime.WriteTotalTimeoutConstant=" & nTime.WriteTotalTimeoutConstant
Debug.Print "nTime.WriteTotalTimeoutMultiplier=" & nTime.WriteTotalTimeoutMultiplier
str_tmp = Chr(27) + "!" + Chr(16)
Debug.Print "CloseHandle(lng_hdc)=" & CloseHandle(lng_hdc)
End Sub