此方法使用VB中的串口通信控件MSComm给打印机发送指令,然后打印机自动返回其状态值,根据不同的返回值
就可以判断打印机是正常、缺纸、卡纸等状态。其中的指令适用于Star SP500系列。注意在使用此方法时不要安装打印机的驱动,或者在驱动中设置的串口端口与此程序中的要不能相同,否则程序会提示参数设置错误,其实就是端口已经被占用的原因。
Option Explicit
Dim Status As String
Dim StatusString As String
Dim TimeUp As Boolean
Private Sub Command1_Click() Sub Command1_Click()Sub Command1_Click()Sub Command1_Click()
On Error Resume Next
Dim i As Long
Status = "" '状态返回值
StatusString = ""
MSComm1.CommPort = 1 'COM端口号:如果是COM1则为1,以此类推;
MSComm1.Settings = "38400,N,8,1" '参数设置:波特率,奇偶校验,数据位,停止位;
MSComm1.InputLen = 0
MSComm1.PortOpen = True '打开端口
'给Star打印机发送“ENQ”指令或“ESC ACK SOH”指令,打印机自动返回状态值
MSComm1.Output = Chr$(5) + Chr$(&HA) + Chr$(0)
'MSComm1.Output = Chr$(&H1B) + Chr$(&H6) + Chr$(&H1)
' Poll for 8 byte status
Do While Len(Status) < 8
DoEvents
Status = MSComm1.Input
Loop
For i = 1 To 9
StatusString = StatusString & Right("00" & " " & Hex(Asc(Mid$(Status, i, 1))), 2)
'StatusString = StatusString & Hex(Asc(Mid$(Status, i, 1))) & " "
Next i
Text1.Text = StatusString
MSComm1.PortOpen = False
End Sub
Private Sub Form_Unload()Sub Form_Unload()Sub Form_Unload()Sub Form_Unload(Cancel As Integer)
Close
End Sub
Private Sub Wait()Sub Wait()Sub Wait()Sub Wait(Interval As Long)
Timer1.Interval = Interval
Timer1.Enabled = True
TimeUp = False
Do
DoEvents
Loop While Not TimeUp
End Sub
Private Sub Timer1_Timer()Sub Timer1_Timer()Sub Timer1_Timer()Sub Timer1_Timer()
Timer1.Interval = 0
Timer1.Enabled = False
TimeUp = True
End Sub
Dim Status As String
Dim StatusString As String
Dim TimeUp As Boolean
Private Sub Command1_Click() Sub Command1_Click()Sub Command1_Click()Sub Command1_Click()
On Error Resume Next
Dim i As Long
Status = "" '状态返回值
StatusString = ""
MSComm1.CommPort = 1 'COM端口号:如果是COM1则为1,以此类推;
MSComm1.Settings = "38400,N,8,1" '参数设置:波特率,奇偶校验,数据位,停止位;
MSComm1.InputLen = 0
MSComm1.PortOpen = True '打开端口
'给Star打印机发送“ENQ”指令或“ESC ACK SOH”指令,打印机自动返回状态值
MSComm1.Output = Chr$(5) + Chr$(&HA) + Chr$(0)
'MSComm1.Output = Chr$(&H1B) + Chr$(&H6) + Chr$(&H1)
' Poll for 8 byte status
Do While Len(Status) < 8
DoEvents
Status = MSComm1.Input
Loop
For i = 1 To 9
StatusString = StatusString & Right("00" & " " & Hex(Asc(Mid$(Status, i, 1))), 2)
'StatusString = StatusString & Hex(Asc(Mid$(Status, i, 1))) & " "
Next i
Text1.Text = StatusString
MSComm1.PortOpen = False
End Sub
Private Sub Form_Unload()Sub Form_Unload()Sub Form_Unload()Sub Form_Unload(Cancel As Integer)
Close
End Sub
Private Sub Wait()Sub Wait()Sub Wait()Sub Wait(Interval As Long)
Timer1.Interval = Interval
Timer1.Enabled = True
TimeUp = False
Do
DoEvents
Loop While Not TimeUp
End Sub
Private Sub Timer1_Timer()Sub Timer1_Timer()Sub Timer1_Timer()Sub Timer1_Timer()
Timer1.Interval = 0
Timer1.Enabled = False
TimeUp = True
End Sub