Results 1 to 6 of 6

Thread: send text to printer via LPT1

  1. #1
    MasterX is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0
    Join Date
    Jul 2012
    Posts
    4
    Reputation
    0

    send text to printer via LPT1

    hi, i'm italian.
    sorry for the English is not prefect. I'm writing an application in VB.NET
    I print to a printer epson tm 220 with escape commands or in other mode, without driver
    you can help me to write "hello world" ?
    I maybe a simple link

    thanks

  2. #2
    Jas91 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Jul 2012
    Location
    Baghdad, Italy. OK?=
    Posts
    17
    Reputation
    14
    I'm italian too. You can't print with a printer which is not installed :O

    and I don't get what you mean by "escape commands" xd

  3. #3
    MasterX is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0
    Join Date
    Jul 2012
    Posts
    4
    Reputation
    0
    Quote Originally Posted by Jas91 View Post
    and I don't get what you mean by "escape commands" xd
    I don't think you can print; it's a lpt1 printer; if you go pippo.txt > lpt1: , it's print without driver; to escape commands are commands that some printers have. you how print ?

  4. #4
    Jas91 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Jul 2012
    Location
    Baghdad, Italy. OK?=
    Posts
    17
    Reputation
    14
    Quote Originally Posted by MasterX View Post
    italian:
    non penso che non si possa stampare. รจ una stampante su lpt1. se tu da dos fai pippo.txt > lpt1: stampa senza driver ; per comandi escape ci sono dei comandi che alcune stampanti hanno. tu come stamperesti

    english:
    I don't think you can print; it's a lpt1 printer; if you go pippo.txt > lpt1: , it's print without driver; to escape commands are commands that some printers have. you how print ?
    you answered yourself. Create a batch file (*.bat) that executes your command and run it from vb with the Shell function

  5. #5
    Herman is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    466
    Reputation
    348
    Or you can use the well-known InpOut32 library... I have written a basic wrapper class for it a little while ago to communicate with a development board:

    Imports System.Runtime.InteropServices

    Public NotInheritable Class clsParPortControl
    Private m_bX64 As Boolean = False
    Private dwLastError As Int32
    Private tmp As Boolean = False

    Public Sub New()
    MyBase.New()
    End Sub

    Public ReadOnly Property LastError As Int32
    Get
    Return dwLastError
    End Get
    End Property

    Public ReadOnly Property Opened As Boolean
    Get
    tmp = IsParPortDriverOpen()
    Return tmp
    End Get
    End Property

    Public Function ReadByte(ByVal PortAddress As Short) As Byte ' ### Read a byte.
    Dim Value As Byte = Nothing
    dwLastError = Nothing
    If IsParPortDriverOpen() = True Then ' Check if InpOut32 is installed and hooked
    Try ' and set 64-bits flag if necessary.
    If m_bX64 = False Then
    Value = Convert.ToByte(NativeMethods.Inp32(PortAddress)) ' Read with 32-bits lib.
    Else
    Value = Convert.ToByte(NativeMethods.Inp32_x64(PortAddress)) ' Read with 64-bits lib.
    End If
    Catch ex As System.OverflowException ' Handle the overflow exception from conversion.
    Catch ex As Exception ' Any other exception set the error code.
    dwLastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error
    End Try
    End If
    Return Value ' Return the byte. (0 if there was an error)
    End Function

    Public Function WriteByte(ByVal PortAddress As Short, ByVal Value As Byte) As Boolean
    Dim nResult As Boolean = True
    dwLastError = Nothing
    If IsParPortDriverOpen() = True Then
    Try
    If m_bX64 = False Then
    NativeMethods.Out32(PortAddress, CShort(Value))
    Else
    NativeMethods.Out32_x64(PortAddress, CShort(Value))
    End If
    Catch ex As Exception
    dwLastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error
    nResult = False
    End Try
    End If
    Return nResult
    End Function

    Public Function ReadWord(ByVal PortAddress As Short) As UInt16 ' ### Read a word.
    Dim Value As UInt16 = Nothing
    dwLastError = Nothing
    If IsParPortDriverOpen() = True Then ' Check if InpOut32 is installed and hooked
    Try ' and set 64-bits flag if necessary.
    If m_bX64 = False Then
    Value = Convert.ToUInt16(NativeMethods.Inp32(PortAddress)) ' Read with 32-bits lib.
    Else
    Value = Convert.ToUInt16(NativeMethods.Inp32_x64(PortAddress)) ' Read with 64-bits lib.
    End If
    Catch ex As Exception ' Any exception set the error code.
    dwLastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error
    End Try
    End If
    Return Value ' Return the word. (0 if there was an error)
    End Function

    Public Function WriteWord(ByVal PortAddress As Short, ByVal Value As Short) As Boolean
    Dim nResult As Boolean = True
    dwLastError = Nothing
    If IsParPortDriverOpen() = True Then
    Try
    If m_bX64 = False Then
    NativeMethods.Out32(PortAddress, Value)
    Else
    NativeMethods.Out32_x64(PortAddress, Value)
    End If
    Catch ex As Exception
    dwLastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error
    nResult = False
    End Try
    End If
    Return nResult
    End Function

    Private Function IsParPortDriverOpen() As Boolean
    Dim nResult As UInt32
    dwLastError = Nothing
    Try
    nResult = NativeMethods.IsInpOutDriverOpen()
    Catch ex As Exception
    nResult = NativeMethods.IsInpOutDriverOpen_x64()
    If (nResult <> 0) Then
    m_bX64 = True
    End If
    End Try
    If nResult = 0 Then
    dwLastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error
    End If
    Return CBool(nResult)
    End Function
    End Class


    NativeMethods.vb:

    Imports System.Runtime.InteropServices

    Friend NotInheritable Class NativeMethods
    <DllImport("InpOut32.dll", CharSet:=CharSet.Auto, EntryPoint:="Inp32")> _
    Friend Shared Function Inp32(ByVal PortAddress As Short) As Short
    End Function

    <DllImport("InpOut32.dll", CharSet:=CharSet.Auto, EntryPoint:="Out32")> _
    Friend Shared Sub Out32(ByVal PortAddress As Short, ByVal Data As Short)
    End Sub

    <DllImport("InpOut32.dll", CharSet:=CharSet.Auto, EntryPoint:="IsInpOutDriverOpen")> _
    Friend Shared Function IsInpOutDriverOpen() As UInt32
    End Function

    <DllImport("InpOutx64.dll", CharSet:=CharSet.Auto, EntryPoint:="Inp32")> _
    Friend Shared Function Inp32_x64(ByVal PortAddress As Short) As Short
    End Function

    <DllImport("InpOutx64.dll", CharSet:=CharSet.Auto, EntryPoint:="Out32")> _
    Friend Shared Sub Out32_x64(ByVal PortAddress As Short, ByVal Data As Short)
    End Sub

    <DllImport("InpOutx64.dll", CharSet:=CharSet.Auto, EntryPoint:="IsInpOutDriverOpen")> _
    Friend Shared Function IsInpOutDriverOpen_x64() As UInt32
    End Function

    End Class
    Last edited by Herman; 07-12-2012 at 7:22 PM.

  6. #6
    Herman is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    466
    Reputation
    348
    Also, be aware that most receipt printers come with Windows drivers nowadays, and if a driver is installed you will not be able to hook with the printer via InpOut32.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking