473,472 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

connecting to printer port using hp printer commands in vb code??

24 New Member
hi
i am having problem in firing commands to the hp printer through vb code. i used hp printer commands in my code but when the commands are fired it gives error "failed to print the doccument". printer shows no movements.
Aug 29 '07 #1
10 3084
hariharanmca
1,977 Top Contributor
hi
i am having problem in firing commands to the hp printer through vb code. i used hp printer commands in my code but when the commands are fired it gives error "failed to print the doccument". printer shows no movements.
Whatever printer you have to use LPT1 or any Printer Port.
Will you post what you had been tryed?
Aug 29 '07 #2
suvarna
24 New Member
Whatever printer you have to use LPT1 or any Printer Port.
Will you post what you had been tryed?

the code i used is

'selects the printer model from the dropdown

Private Sub cmbPrinters_Click()

mvarPrinterName = cmbPrinters.Text
InitialisePrinter
ExitPacketMode

End Sub

Private Sub Command1_Click()

MoveHorizontal
'MoveVertical
End Sub

Private Sub Form_Load()

UpdateCmbPrinters

End Sub



Private Sub UpdateCmbPrinters()
Dim i As Integer

i = 0
For Each X In Printers
cmbPrinters.List(i) = X.DeviceName
i = i + 1
Next
cmbPrinters.ListIndex = 0

End Sub

'module2 code

Option Explicit

Public iStartHour As Integer
Public buffer As String
Public textFile As String
Public graphFile As String


Public Type TCharacterString
CharacterID As Integer
arrayCharacterMap(rowCount, colCount) As Boolean
End Type

Public Type TParaProp
mCharacterString() As TCharacterString
End Type

Public mParaProp(5) As TParaProp


Public iGraphBufferCount As Integer
Public ArrayGraphBuffer(GraphBufferCount) As String


Public gFso As New FileSystemObject, gFil As File, gTs As TextStream
Public LineCount As Long 'local copy

Public iTextBuffer(89) As Integer
Public iCmdBuffer() As Integer
Public iGraphBuffer() As Long
Public lGraphData As Long

Public mvarPrinterID As Integer
Public mvarPrinterName As String


Private Sub OpenTextFile()

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(textFile) = True Then
fso.DeleteFile (textFile)
fso.CreateTextFile (textFile)
Else
fso.CreateTextFile (textFile)
End If
Set fil = fso.GetFile(textFile)
Set ts = fil.OpenAsTextStream(ForWriting)

End Sub

Private Sub CloseTextFile()

ts.Close
Set ts = Nothing
Set fil = Nothing
Set fso = Nothing

End Sub

Public Sub InitialisePrinter()
Dim i As Integer
Dim t As Integer

iStartHour = 0
LineCount = 0

lGraphData = 2800
ReDim iGraphBuffer(lGraphData)
'ReDim iCmdBuffer(rsCmdMaster.RecordCount + lGraphData)




textFile = "C:\f" & mvarPrinterName & "." & "txt"
graphFile = "C:\g" & mvarPrinterName & "." & "txt"

iGraphBufferCount = 0


End Sub

Public Sub ExitPacketMode()

OpenTextFile
buffer = Chr(0) + Chr(0) + Chr(0) + Chr(27) + Chr(1) + "@EJL" + Chr(32) + "1284.4" + Chr(10) + "@EJL" + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(10)
ts.Write buffer
CloseTextFile
Test (textFile)

OpenTextFile
buffer = Chr(27) + "@"
ts.Write buffer
CloseTextFile
Test (textFile)

End Sub

Public Sub TerminatePrinter()

End Sub
Public Sub ResetPrinter()

OpenTextFile
buffer = Chr(27) + "E"
ts.Write buffer
CloseTextFile
Test (textFile)

End Sub
Public Sub MoveVertical()
Dim i As Integer

OpenTextFile
buffer = Chr(27) + "J" + Chr(4)
ts.Write buffer
CloseTextFile
Test (textFile)

End Sub





Public Sub MoveHorizontal()
Dim i As Integer

OpenTextFile
'ascii value for paper source
buffer = Chr(27) + Chr(38) + Chr(108) + Chr(48) + Chr(72)
ts.Write buffer
CloseTextFile
Test (textFile)

End Sub

Public Sub Home()
Dim i As Integer

OpenTextFile
buffer = Chr(13)
ts.Write buffer
CloseTextFile
Test (textFile)

End Sub

Private Sub outputBuffer()
Dim i As Integer

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(textFile) = True Then
fso.DeleteFile (textFile)
fso.CreateTextFile (textFile)
Else
fso.CreateTextFile (textFile)
End If
Set fil = fso.GetFile(textFile)
Set ts = fil.OpenAsTextStream(ForWriting)

buffer = Chr(iTextBuffer(0))
For i = 1 To 79
buffer = buffer + Chr(iTextBuffer(i))
Next i
ts.Write buffer

ts.Close
Set ts = Nothing
Set fil = Nothing
Set fso = Nothing

End Sub


Private Sub Test(ByVal FileName As String)
Dim i As Integer

i = vbSendPrnToPrinter(FileName, mvarPrinterName)

End Sub

Private Sub FillBufferBlank()
Dim i As Integer

For i = 0 To 89
iTextBuffer(i) = 32
Next i

End Sub

Private Sub FillGraphBufferBlank()
Dim i As Integer

For i = 0 To 2000
iGraphBuffer(i) = 0
Next i

End Sub


Private Sub outputGraphBuffer()
Dim i As Integer
Dim GraphBuffer As String

If iGraphBufferCount = (GraphBufferCount - 1) Then

Set gFso = CreateObject("Scripting.FileSystemObject")
If gFso.FileExists(graphFile) = True Then
gFso.DeleteFile (graphFile)
gFso.CreateTextFile (graphFile)
Else
gFso.CreateTextFile (graphFile)
End If
Set gFil = gFso.GetFile(graphFile)
Set gTs = gFil.OpenAsTextStream(ForWriting)

GraphBuffer = ArrayGraphBuffer(0)
For i = 1 To GraphBufferCount - 1
GraphBuffer = GraphBuffer & ArrayGraphBuffer(i)
Next i

gTs.Write GraphBuffer
gTs.Close
Set gTs = Nothing
Set gFil = Nothing
Set gFso = Nothing
iGraphBufferCount = 0
Test (graphFile)
Else
ArrayGraphBuffer(iGraphBufferCount) = Chr(iCmdBuffer(0))
For i = 1 To rsCmdMaster.RecordCount + lGraphData - 1
ArrayGraphBuffer(iGraphBufferCount) = ArrayGraphBuffer(iGraphBufferCount) + Chr(iCmdBuffer(i))
Next i
iGraphBufferCount = iGraphBufferCount + 1
End If
End Sub


'module1 code

Public ColumnWidth As Long
Public Const PageWidth = 360
Public Const rowCount = 7
Public Const colCount = 4
Public Const GraphBufferCount = 60
Public Const StartPos = 5

'Return codes
Public Const ERR_NOERROR = 0 'No error
Public Const ERR_DEFPRINTER = 1 'Error obtaining default printer
Public Const ERR_OPENFILE = 2 'Error opening file
Public Const ERR_OPENPRINTER = 3 'Error opening printer
Public Const ERR_PRINT = 4 'Error sending date to printer
Public Const ERR_FILE = 5 'Error reading file

'Function declarations
Public Declare Function vbSendPrnToPrinter Lib "BINPRN32.DLL" (ByVal pFilename As String, ByVal pPrinterName As String) As Integer
Public Declare Function vbGetDefPrinterName Lib "BINPRN32.DLL" (ByVal pPrinterName As String) As Integer
Public Declare Function vbSendPrnToDefPrinter Lib "BINPRN32.DLL" (ByVal pFilename As String) As Integer
Public fso As New FileSystemObject, fil As File, ts As TextStream

Public Function CNull(Variable As Variant, Value As Variant)
On Error Resume Next
If IsNull(Variable) Then
CNull = Value
Else
If IsNull(Variable) = False Then
CNull = Variable
Else
CNull = Value
End If
End If
End Function



'different values of ascii are not accepted too.
Aug 29 '07 #3
hariharanmca
1,977 Top Contributor
the code i used is

'selects the printer model from the dropdown

Private Sub cmbPrinters_Click()

mvarPrinterName = cmbPrinters.Text
InitialisePrinter
ExitPacketMode

End Sub

Private Sub Command1_Click()

MoveHorizontal
'MoveVertical
End Sub

Private Sub Form_Load()

UpdateCmbPrinters

End Sub



Private Sub UpdateCmbPrinters()
Dim i As Integer

i = 0
For Each X In Printers
cmbPrinters.List(i) = X.DeviceName
i = i + 1
Next
cmbPrinters.ListIndex = 0

End Sub

'module2 code

Option Explicit

Public iStartHour As Integer
Public buffer As String
Public textFile As String
Public graphFile As String


Public Type TCharacterString
CharacterID As Integer
arrayCharacterMap(rowCount, colCount) As Boolean
End Type

Public Type TParaProp
mCharacterString() As TCharacterString
End Type

Public mParaProp(5) As TParaProp


Public iGraphBufferCount As Integer
Public ArrayGraphBuffer(GraphBufferCount) As String


Public gFso As New FileSystemObject, gFil As File, gTs As TextStream
Public LineCount As Long 'local copy

Public iTextBuffer(89) As Integer
Public iCmdBuffer() As Integer
Public iGraphBuffer() As Long
Public lGraphData As Long

Public mvarPrinterID As Integer
Public mvarPrinterName As String


Private Sub OpenTextFile()

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(textFile) = True Then
fso.DeleteFile (textFile)
fso.CreateTextFile (textFile)
Else
fso.CreateTextFile (textFile)
End If
Set fil = fso.GetFile(textFile)
Set ts = fil.OpenAsTextStream(ForWriting)

End Sub

Private Sub CloseTextFile()

ts.Close
Set ts = Nothing
Set fil = Nothing
Set fso = Nothing

End Sub

Public Sub InitialisePrinter()
Dim i As Integer
Dim t As Integer

iStartHour = 0
LineCount = 0

lGraphData = 2800
ReDim iGraphBuffer(lGraphData)
'ReDim iCmdBuffer(rsCmdMaster.RecordCount + lGraphData)




textFile = "C:\f" & mvarPrinterName & "." & "txt"
graphFile = "C:\g" & mvarPrinterName & "." & "txt"

iGraphBufferCount = 0


End Sub

Public Sub ExitPacketMode()

OpenTextFile
buffer = Chr(0) + Chr(0) + Chr(0) + Chr(27) + Chr(1) + "@EJL" + Chr(32) + "1284.4" + Chr(10) + "@EJL" + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(10)
ts.Write buffer
CloseTextFile
Test (textFile)

OpenTextFile
buffer = Chr(27) + "@"
ts.Write buffer
CloseTextFile
Test (textFile)

End Sub

Public Sub TerminatePrinter()

End Sub
Public Sub ResetPrinter()

OpenTextFile
buffer = Chr(27) + "E"
ts.Write buffer
CloseTextFile
Test (textFile)

End Sub
Public Sub MoveVertical()
Dim i As Integer

OpenTextFile
buffer = Chr(27) + "J" + Chr(4)
ts.Write buffer
CloseTextFile
Test (textFile)

End Sub





Public Sub MoveHorizontal()
Dim i As Integer

OpenTextFile
'ascii value for paper source
buffer = Chr(27) + Chr(38) + Chr(108) + Chr(48) + Chr(72)
ts.Write buffer
CloseTextFile
Test (textFile)

End Sub

Public Sub Home()
Dim i As Integer

OpenTextFile
buffer = Chr(13)
ts.Write buffer
CloseTextFile
Test (textFile)

End Sub

Private Sub outputBuffer()
Dim i As Integer

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(textFile) = True Then
fso.DeleteFile (textFile)
fso.CreateTextFile (textFile)
Else
fso.CreateTextFile (textFile)
End If
Set fil = fso.GetFile(textFile)
Set ts = fil.OpenAsTextStream(ForWriting)

buffer = Chr(iTextBuffer(0))
For i = 1 To 79
buffer = buffer + Chr(iTextBuffer(i))
Next i
ts.Write buffer

ts.Close
Set ts = Nothing
Set fil = Nothing
Set fso = Nothing

End Sub


Private Sub Test(ByVal FileName As String)
Dim i As Integer

i = vbSendPrnToPrinter(FileName, mvarPrinterName)
End Sub

Private Sub FillBufferBlank()
Dim i As Integer

For i = 0 To 89
iTextBuffer(i) = 32
Next i

End Sub

Private Sub FillGraphBufferBlank()
Dim i As Integer

For i = 0 To 2000
iGraphBuffer(i) = 0
Next i

End Sub


Private Sub outputGraphBuffer()
Dim i As Integer
Dim GraphBuffer As String

If iGraphBufferCount = (GraphBufferCount - 1) Then

Set gFso = CreateObject("Scripting.FileSystemObject")
If gFso.FileExists(graphFile) = True Then
gFso.DeleteFile (graphFile)
gFso.CreateTextFile (graphFile)
Else
gFso.CreateTextFile (graphFile)
End If
Set gFil = gFso.GetFile(graphFile)
Set gTs = gFil.OpenAsTextStream(ForWriting)

GraphBuffer = ArrayGraphBuffer(0)
For i = 1 To GraphBufferCount - 1
GraphBuffer = GraphBuffer & ArrayGraphBuffer(i)
Next i

gTs.Write GraphBuffer
gTs.Close
Set gTs = Nothing
Set gFil = Nothing
Set gFso = Nothing
iGraphBufferCount = 0
Test (graphFile)
Else
ArrayGraphBuffer(iGraphBufferCount) = Chr(iCmdBuffer(0))
For i = 1 To rsCmdMaster.RecordCount + lGraphData - 1
ArrayGraphBuffer(iGraphBufferCount) = ArrayGraphBuffer(iGraphBufferCount) + Chr(iCmdBuffer(i))
Next i
iGraphBufferCount = iGraphBufferCount + 1
End If
End Sub


'module1 code

Public ColumnWidth As Long
Public Const PageWidth = 360
Public Const rowCount = 7
Public Const colCount = 4
Public Const GraphBufferCount = 60
Public Const StartPos = 5

'Return codes
Public Const ERR_NOERROR = 0 'No error
Public Const ERR_DEFPRINTER = 1 'Error obtaining default printer
Public Const ERR_OPENFILE = 2 'Error opening file
Public Const ERR_OPENPRINTER = 3 'Error opening printer
Public Const ERR_PRINT = 4 'Error sending date to printer
Public Const ERR_FILE = 5 'Error reading file

'Function declarations
Public Declare Function vbSendPrnToPrinter Lib "BINPRN32.DLL" (ByVal pFilename As String, ByVal pPrinterName As String) As Integer
Public Declare Function vbGetDefPrinterName Lib "BINPRN32.DLL" (ByVal pPrinterName As String) As Integer
Public Declare Function vbSendPrnToDefPrinter Lib "BINPRN32.DLL" (ByVal pFilename As String) As Integer
Public fso As New FileSystemObject, fil As File, ts As TextStream

Public Function CNull(Variable As Variant, Value As Variant)
On Error Resume Next
If IsNull(Variable) Then
CNull = Value
Else
If IsNull(Variable) = False Then
CNull = Variable
Else
CNull = Value
End If
End If
End Function



'different values of ascii are not accepted too.

One thing, For network printer you cannot give just printer name.

you have to give like

\\<System Name>\<Printer Name>
Aug 29 '07 #4
suvarna
24 New Member
One thing, For network printer you cannot give just printer name.

you have to give like

\\<System Name>\<Printer Name>


the printer is not a network printer.. it is connected to this system for testing and will remain for implementation also.
Aug 30 '07 #5
hariharanmca
1,977 Top Contributor
Public Sub InitialisePrinter()Dim i As Integer
Dim t As Integer

iStartHour = 0
LineCount = 0

lGraphData = 2800
ReDim iGraphBuffer(lGraphData)

'ReDim iCmdBuffer(rsCmdMaster.RecordCount + lGraphData)




textFile = "C:\f" & mvarPrinterName & "." & "txt"
graphFile = "C:\g" & mvarPrinterName & "." & "txt"

iGraphBufferCount = 0


End Sub

Can you explain what this method is doing?
Aug 30 '07 #6
suvarna
24 New Member
Can you explain what this method is doing?

this method is used to print a graph which takes data from gas sacnner n calculates a graph for it..
Aug 30 '07 #7
hariharanmca
1,977 Top Contributor
this method is used to print a graph which takes data from gas sacnner n calculates a graph for it..
Okay, Can you post which part of code is showing error? and what error?
It is not easy to go through your bulky code.

Expand|Select|Wrap|Line Numbers
  1. Public Sub ExitPacketMode()
  2.  
  3. OpenTextFile
  4. buffer = Chr(0) + Chr(0) + Chr(0) + Chr(27) + Chr(1) + "@EJL" + Chr(32) + "1284.4" + Chr(10) + "@EJL" + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(10)
  5. ts.Write buffer
  6. CloseTextFile
  7. Test (textFile)
  8.  
  9. OpenTextFile
  10. buffer = Chr(27) + "@"
  11. ts.Write buffer
  12. CloseTextFile
  13. Test (textFile)
  14.  
  15. End Sub
1. where is declaration of ts in ts.Write buffer
2. Is your Print output an Billing or some thing related to data report (Because your pasing .txt file to print).
2. above code seems to be there is no any image or graphical line to print.
3. If you do not have any image or graphical line there is some other way to print.
4. Just use a .bat file to print.
Aug 30 '07 #8
suvarna
24 New Member
Okay, Can you post which part of code is showing error? and what error?
It is not easy to go through your bulky code.

Expand|Select|Wrap|Line Numbers
  1. Public Sub ExitPacketMode()
  2.  
  3. OpenTextFile
  4. buffer = Chr(0) + Chr(0) + Chr(0) + Chr(27) + Chr(1) + "@EJL" + Chr(32) + "1284.4" + Chr(10) + "@EJL" + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(10)
  5. ts.Write buffer
  6. CloseTextFile
  7. Test (textFile)
  8.  
  9. OpenTextFile
  10. buffer = Chr(27) + "@"
  11. ts.Write buffer
  12. CloseTextFile
  13. Test (textFile)
  14.  
  15. End Sub
1. where is declaration of ts in ts.Write buffer
2. Is your Print output an Billing or some thing related to data report (Because your pasing .txt file to print).
2. above code seems to be there is no any image or graphical line to print.
3. If you do not have any image or graphical line there is some other way to print.
4. Just use a .bat file to print.


Expand|Select|Wrap|Line Numbers
  1. Public fso As New FileSystemObject, fil As File, ts As TextStream
  2.  
  3. end sub
i have mentioned the module code also for any doubts u can go trough.
how do i print graphics through this code??
Aug 31 '07 #9
hariharanmca
1,977 Top Contributor
Expand|Select|Wrap|Line Numbers
  1. Public fso As New FileSystemObject, fil As File, ts As TextStream
  2.  
  3. end sub
i have mentioned the module code also for any doubts u can go trough.
how do i print graphics through this code??
I cannot find error in such a huge code and i do not want that module code (Unable to find starting point and ending point).
Okay, Can you post which part of code is showing error? and what error?
It is not easy to go through your bulky code.
Aug 31 '07 #10
suvarna
24 New Member
I cannot find error in such a huge code and i do not want that module code (Unable to find starting point and ending point).
Okay, Can you post which part of code is showing error? and what error?
It is not easy to go through your bulky code.

neway thanks i found the solution for it.. it worked the same way i just included it to another class defn. thanks a lot... actually the code was not written by me it was my senior so had problems..
Aug 31 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Michael Kilpatrick | last post by:
I'm writing a command-line PHP script that gets executed by a virtual printer (a redirected port using the Redmon tool). The PHP modifies the Postscript data before forwarding it to the real...
0
by: Esmee | last post by:
Hi there, I have created an Access 2002 db which contains several reports. Some reports need to be printed on a Laserjet and others on a Labelprinter. One of my clients wants to be able to print...
1
by: Bruce Wood | last post by:
I'm having a devil of a time calling DeviceCapabilities() in order to get the list of paper names / codes / sizes for a printer. Here is my code and the input it produces: static extern Int32...
4
by: Charlie | last post by:
I need to print using the VB.net printing classes to a tractor feed printer. My client loaned me a Panasonic kx-p2123 to set up the job. I was able to print from Notepad using the Generic/Text...
2
by: up3umrmuofnz3pd | last post by:
Hi guys, I'm working on a small desktop publishing software project where I need to display the default printer's ink status/level in the status bar of the main form. For the past 2 days I've...
4
ADezii
by: ADezii | last post by:
Recently, there seems to be several questions specifically related to Printers and changing Printing characteristics for Forms and Reports. For this reason alone, I decided to dedicate this week's...
2
by: hari | last post by:
Hi all, I need to automate printer command testing, prinetr supports parallel/ serial/USB.How can i send the commands from python to printer. I have got pyparallel, as am new to python, no...
1
by: malooga | last post by:
Hello, I'm having a problem connecting to DB2 on a remote iSeries host from a Linux server, both of which reside on my company's internal network. I'm using the IBM Linux Client V9.1. When I try...
0
by: =?Utf-8?B?Q2hhcmxpZQ==?= | last post by:
Hi, This apparently is a common problem and I've yet to read a solution that actually works for my specific situation. I have a Zebra RW220 printer that I connect to via Bluetooth connection...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.