Get Status of Bluetooth paired Epson printer

994 views Asked by At

The problem

I am currently working on an application which needs to send information to print on an EPSON TM-U295 printer. Said printer has Bluetooth enabled and is paired with the computer. I am currently able to send a string to it and print the needed information. However, if there is no paper in the printer the string is still sent and printed on thin air.

Current code to print

Take note that Socket is a StreamSocket using the method ConnectAsync() which allows you to connect to a paired printer.

'Print
Public Async Function Write(ByVal StrToSend As String) As Task
    Dim Bytestosend As Byte() = System.Text.Encoding.UTF8.GetBytes(StrToSend)
    Await Socket.OutputStream.WriteAsync(Bytestosend.AsBuffer())
    Await Socket.OutputStream.FlushAsync()
End Function

'Send the command to print
Private Async Sub BtnPrint_Click(sender As Object, e As RoutedEventArgs)
    Await Write(txtTextTosend.Text + vbLf)
End Sub

Code wanted

I would like to be able to verify if the paper is out with a condition similar to the following :

'Send the command to print
Private Async Sub BtnPrint_Click(sender As Object, e As RoutedEventArgs)
    if Status <> PRINTER_PAPER_OUT then
        Await Write(txtTextTosend.Text + vbLf)
    End if
End Sub

R&D

I have noticed in the Documentation (Page 10-11 of another EPSON printer model) a certain ASB Status which is defined as follows:

Auto Status Back: This is a function of the TM printer. This is a status automatically sent from the printer when the printer status changes (opening or closing the cover, out of paper, print completed, etc.).

It is later mentioned (Page 22) that ASB_RECEIPT_END is a constant which is linked with the printer having no paper

More documentation

The Question

How do we use the ASB status mentioned earlier to know if the printer is in a "Out of Paper" state ?

If the ASB status is not the way to go to obtain the information could someone point me in the correct direction ?

Note that I do not mind C# or VB.NET code

1

There are 1 answers

0
micbobo On

What needs to be done

  • Learn about and use ESC/POS commands

  • Activate the ASB (GS a command)

  • Activate the Group Separator Command GS

    This will separate the Write for the ASB and the Write for the Text avoiding to print something like : aTEXTTOPRINT

  • Read the status sent back from the printer

  • Verify if the state is out of paper

The Code

  • First a READ function is needed which will read the bytes and convert it to a string

    Dim bytes(2000) As Byte ' 2000 bytes is random I wasnt sure how many were needed 
    
    Dim result = Await Socket.InputStream.ReadAsync(bytes.AsBuffer, bytes.Length, InputStreamOptions.Partial)
    Dim lgtOfRead = result.Length
    Dim readstr As String = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length)
    Dim retstr As String = readstr.Take(lgtOfRead).ToArray()
    Return retstr
    
  • Secondly it is needed to send the commands mentioned above for this to work

    'Send the command to print
    Private Async Sub BtnPrint_Click(sender As Object, e As RoutedEventArgs)
        Dim cmd As Byte() = {29, 97} 'Send the command to enable ASB
        Await _Btcomm.Write(cmd)
    
        cmd = {29} 'Separate the information
        Await _Btcomm.Write(cmd)
    
        Dim strResult As String = Await _Btcomm.Read()
    
        'Verify if the status has a "`" character linked to paper out state
        If Not strResult.Contains("`") Then
            Await _Btcomm.Write(txtTextTosend.Text + vbLf)
        Else
            Dim md As MessageDialog = New MessageDialog("The printer has no paper", "No Paper")
            Await md.ShowAsync()
        End If
    End Sub
    

Documentation

Some commands

Some more commands

MOAR Commands

http://www.asciitable.com (ASCII table makes it easier to figure out commands)