首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

VB 串口8015异常 MSComm 获得打印机的返回异常

2011-12-29 
VB 串口8015错误 MSComm 获得打印机的返回错误再次被卡,,,,各位前辈高人,帮忙啊实时错误8015 不能设置comm

VB 串口8015错误 MSComm 获得打印机的返回错误
再次被卡,,,,各位前辈高人,帮忙啊

实时错误8015 不能设置comm状态 可能有一个或多个无效的通讯参数 代码如下:
Private Sub OpenProt()
  MSComm1.CommPort = 1
  MSComm1.Settings = "9600,N,8,1"
  MSComm1.OutBufferSize = 1024
  MSComm1.InBufferSize = 512
  MSComm1.RThreshold = 1
  MSComm1.SThreshold = 1
  MSComm1.InputLen = 0
  MSComm1.DTREnable = True
  MSComm1.RTSEnable = True
  MSComm1.PortOpen = True
End Sub

Private Sub Form_Load()
  OpenProt
End Sub

相关情况如下:在控制打印机打印消费票据的时候需要获得打印机的状态(是否缺纸等),打印机为爱普生针式打印机。发送命令检测打印机的状态的时候,需要取出打印机返回的信息来判断。我想用MSComm控件来获得,但是8015错误。打印机相关代码如下:

Private Sub Command1_Click() '打印

Open "COM1" For Output As #1
Print #1, "adbcdeerfshandong" ’打印
Print #1, Chr(16); Chr(4); Chr(4) '获得打印机状态的命令
Print #1, Chr(27); Chr(100); Chr(2)
Close #1
End Sub


[解决办法]
把上面的代码注释掉,不用那个控件,只用后面这段代码试试
[解决办法]
这里有个模块:

VB code
Public Declare Function lstrcpy Lib "kernel32" _   Alias "lstrcpyA" _   (ByVal lpString1 As String, _   ByVal lpString2 As String) _   As LongPublic Declare Function OpenPrinter Lib "winspool.drv" _   Alias "OpenPrinterA" _   (ByVal pPrinterName As String, _   phPrinter As Long, _   pDefault As PRINTER_DEFAULTS) _   As LongPublic Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" _   (ByVal hPrinter As Long, _   ByVal Level As Long, _   pPrinter As Byte, _   ByVal cbBuf As Long, _   pcbNeeded As Long) _   As LongPublic Declare Function ClosePrinter Lib "winspool.drv" _   (ByVal hPrinter As Long) _   As LongPublic Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _   (Destination As Any, _   Source As Any, _   ByVal Length As Long)Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" _   (ByVal hPrinter As Long, _   ByVal FirstJob As Long, _   ByVal NoJobs As Long, _   ByVal Level As Long, _   pJob As Byte, _   ByVal cdBuf As Long, _   pcbNeeded As Long, _   pcReturned As Long) _   As Long   ' constants for PRINTER_DEFAULTS structurePublic Const PRINTER_ACCESS_USE = &H8Public Const PRINTER_ACCESS_ADMINISTER = &H4' constants for DEVMODE structurePublic Const CCHDEVICENAME = 32Public Const CCHFORMNAME = 32Public Type PRINTER_DEFAULTS   pDatatype As String   pDevMode As Long   DesiredAccess As LongEnd TypePublic Type DEVMODE   dmDeviceName As String * CCHDEVICENAME   dmSpecVersion As Integer   dmDriverVersion As Integer   dmSize As Integer   dmDriverExtra As Integer   dmFields As Long   dmOrientation As Integer   dmPaperSize As Integer   dmPaperLength As Integer   dmPaperWidth As Integer   dmScale As Integer   dmCopies As Integer   dmDefaultSource As Integer   dmPrintQuality As Integer   dmColor As Integer   dmDuplex As Integer   dmYResolution As Integer   dmTTOption As Integer   dmCollate As Integer   dmFormName As String * CCHFORMNAME   dmLogPixels As Integer   dmBitsPerPel As Long   dmPelsWidth As Long   dmPelsHeight As Long   dmDisplayFlags As Long   dmDisplayFrequency As LongEnd TypeType SYSTEMTIME   wYear As Integer   wMonth As Integer   wDayOfWeek As Integer   wDay As Integer   wHour As Integer   wMinute As Integer   wSecond As Integer   wMilliseconds As IntegerEnd TypeType JOB_INFO_2   JobId As Long   pPrinterName As Long   pMachineName As Long   pUserName As Long   pDocument As Long   pNotifyName As Long   pDatatype As Long   pPrintProcessor As Long   pParameters As Long   pDriverName As Long   pDevMode As Long   pStatus As Long   pSecurityDescriptor As Long   Status As Long   Priority As Long   Position As Long   StartTime As Long   UntilTime As Long   TotalPages As Long   Size As Long   Submitted As SYSTEMTIME   time As Long   PagesPrinted As LongEnd TypeType PRINTER_INFO_2   pServerName As Long   pPrinterName As Long   pShareName As Long   pPortName As Long   pDriverName As Long   pComment As Long   pLocation As Long   pDevMode As Long   pSepFile As Long   pPrintProcessor As Long   pDatatype As Long   pParameters As Long   pSecurityDescriptor As Long   Attributes As Long   Priority As Long   DefaultPriority As Long   StartTime As Long   UntilTime As Long   Status As Long   cJobs As Long   AveragePPM As LongEnd TypePublic Const ERROR_INSUFFICIENT_BUFFER = 122Public Const PRINTER_STATUS_BUSY = &H200Public Const PRINTER_STATUS_DOOR_OPEN = &H400000Public Const PRINTER_STATUS_ERROR = &H2Public Const PRINTER_STATUS_INITIALIZING = &H8000Public Const PRINTER_STATUS_IO_ACTIVE = &H100Public Const PRINTER_STATUS_MANUAL_FEED = &H20Public Const PRINTER_STATUS_NO_TONER = &H40000Public Const PRINTER_STATUS_NOT_AVAILABLE = &H1000Public Const PRINTER_STATUS_OFFLINE = &H80Public Const PRINTER_STATUS_OUT_OF_MEMORY = &H200000Public Const PRINTER_STATUS_OUTPUT_BIN_FULL = &H800Public Const PRINTER_STATUS_PAGE_PUNT = &H80000Public Const PRINTER_STATUS_PAPER_JAM = &H8Public Const PRINTER_STATUS_PAPER_OUT = &H10Public Const PRINTER_STATUS_PAPER_PROBLEM = &H40Public Const PRINTER_STATUS_PAUSED = &H1Public Const PRINTER_STATUS_PENDING_DELETION = &H4Public Const PRINTER_STATUS_PRINTING = &H400Public Const PRINTER_STATUS_PROCESSING = &H4000Public Const PRINTER_STATUS_TONER_LOW = &H20000Public Const PRINTER_STATUS_USER_INTERVENTION = &H100000Public Const PRINTER_STATUS_WAITING = &H2000Public Const PRINTER_STATUS_WARMING_UP = &H10000Public Const JOB_STATUS_PAUSED = &H1Public Const JOB_STATUS_ERROR = &H2Public Const JOB_STATUS_DELETING = &H4Public Const JOB_STATUS_SPOOLING = &H8Public Const JOB_STATUS_PRINTING = &H10Public Const JOB_STATUS_OFFLINE = &H20Public Const JOB_STATUS_PAPEROUT = &H40Public Const JOB_STATUS_PRINTED = &H80Public Const JOB_STATUS_DELETED = &H100Public Const JOB_STATUS_BLOCKED_DEVQ = &H200Public Const JOB_STATUS_USER_INTERVENTION = &H400Public Const JOB_STATUS_RESTART = &H800Public Function GetString(ByVal PtrStr As Long) As String   Dim StrBuff As String * 256      'Check for zero address   If PtrStr = 0 Then      GetString = " "      Exit Function   End If      'Copy data from PtrStr to buffer.   CopyMemory ByVal StrBuff, ByVal PtrStr, 256      'Strip any trailing nulls from string.   GetString = StripNulls(StrBuff)End FunctionPublic Function StripNulls(OriginalStr As String) As String   'Strip any trailing nulls from input string.   If (InStr(OriginalStr, Chr(0)) > 0) Then      OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)   End If   'Return modified string.   StripNulls = OriginalStrEnd FunctionPublic Function PtrCtoVbString(Add As Long) As String    Dim sTemp As String * 512    Dim x As Long    x = lstrcpy(sTemp, Add)    If (InStr(1, sTemp, Chr(0)) = 0) Then         PtrCtoVbString = ""    Else         PtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)    End IfEnd FunctionPublic Function CheckPrinterStatus(PI2Status As Long) As String   Dim tempStr As String      If PI2Status = 0 Then   ' Return "Ready"      CheckPrinterStatus = "Printer Status = Ready" & vbCrLf   Else      tempStr = ""   ' Clear      If (PI2Status And PRINTER_STATUS_BUSY) Then         tempStr = tempStr & "Busy  "      End If            If (PI2Status And PRINTER_STATUS_DOOR_OPEN) Then         tempStr = tempStr & "Printer Door Open  "      End If            If (PI2Status And PRINTER_STATUS_ERROR) Then         tempStr = tempStr & "Printer Error  "      End If            If (PI2Status And PRINTER_STATUS_INITIALIZING) Then         tempStr = tempStr & "Initializing  "      End If            If (PI2Status And PRINTER_STATUS_IO_ACTIVE) Then         tempStr = tempStr & "I/O Active  "      End If      If (PI2Status And PRINTER_STATUS_MANUAL_FEED) Then         tempStr = tempStr & "Manual Feed  "      End If            If (PI2Status And PRINTER_STATUS_NO_TONER) Then         tempStr = tempStr & "No Toner  "      End If            If (PI2Status And PRINTER_STATUS_NOT_AVAILABLE) Then         tempStr = tempStr & "Not Available  "      End If            If (PI2Status And PRINTER_STATUS_OFFLINE) Then         tempStr = tempStr & "Off Line  "      End If            If (PI2Status And PRINTER_STATUS_OUT_OF_MEMORY) Then         tempStr = tempStr & "Out of Memory  "      End If            If (PI2Status And PRINTER_STATUS_OUTPUT_BIN_FULL) Then         tempStr = tempStr & "Output Bin Full  "      End If            If (PI2Status And PRINTER_STATUS_PAGE_PUNT) Then         tempStr = tempStr & "Page Punt  "      End If            If (PI2Status And PRINTER_STATUS_PAPER_JAM) Then         tempStr = tempStr & "Paper Jam  "      End If      If (PI2Status And PRINTER_STATUS_PAPER_OUT) Then         tempStr = tempStr & "Paper Out  "      End If            If (PI2Status And PRINTER_STATUS_OUTPUT_BIN_FULL) Then         tempStr = tempStr & "Output Bin Full  "      End If            If (PI2Status And PRINTER_STATUS_PAPER_PROBLEM) Then         tempStr = tempStr & "Page Problem  "      End If            If (PI2Status And PRINTER_STATUS_PAUSED) Then         tempStr = tempStr & "Paused  "      End If      If (PI2Status And PRINTER_STATUS_PENDING_DELETION) Then         tempStr = tempStr & "Pending Deletion  "      End If            If (PI2Status And PRINTER_STATUS_PRINTING) Then         tempStr = tempStr & "Printing  "      End If            If (PI2Status And PRINTER_STATUS_PROCESSING) Then         tempStr = tempStr & "Processing  "      End If            If (PI2Status And PRINTER_STATUS_TONER_LOW) Then         tempStr = tempStr & "Toner Low  "      End If      If (PI2Status And PRINTER_STATUS_USER_INTERVENTION) Then         tempStr = tempStr & "User Intervention  "      End If            If (PI2Status And PRINTER_STATUS_WAITING) Then         tempStr = tempStr & "Waiting  "      End If            If (PI2Status And PRINTER_STATUS_WARMING_UP) Then         tempStr = tempStr & "Warming Up  "      End If            'Did you find a known status?      If Len(tempStr) = 0 Then         tempStr = "Unknown Status of " & PI2Status      End If            'Return the Status      CheckPrinterStatus = "Printer Status = " & tempStr & vbCrLf   End IfEnd Function 


[解决办法]
暂先不说 串口问题

打印机检测 它本身会返回打印状态的信息 建议换个思路

下面代码仅提供你另个思路

'添加 Command1 Timer1
Option Explicit
Dim aa$, bb$, starttm&, i%
Dim objWMIService, objProcess, colProcess, strComputer, strList

Private Sub Form_Load()
Timer1.Interval = 500
Timer1.Enabled = False
Command1.Caption = "打 印"
End Sub

Private Sub Command1_Click()
If Command1.Caption = "打 印" Then
If Printers.Count > 0 And Printer.DriverName <> "" Then
Command1.Caption = "取 消"
Open "c:\tt.txt" For Input As #1
starttm = Timer
Timer1.Enabled = True
Me.Caption = "打印机检测中"
While Not EOF(1)
Line Input #1, aa
Printer.Print aa
Wend
Close #1
Printer.EndDoc
Else
MsgBox "您未设置打印机或未安装驱动程序"
End If
Else
Command1.Caption = "打 印"
Timer1.Enabled = False
Printer.KillDoc
End If
End Sub

Private Sub Timer1_Timer()
strComputer = "."
strComputer = "."
Set objWMIService = GetObject("winmgmts:" + "{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer")
bb = ""
For Each objPrinter In colInstalledPrinters
bb = bb & objPrinter.PrinterStatus
If InStr(bb, "4") > 0 Then Timer1.Enabled = False: Command1_Click: Exit Sub
Next
If Timer - starttm > 15 Then
Timer1.Enabled = False
MsgBox "打印机未联线或缺纸或予热中"
Command1_Click
End If
End Sub

热点排行