Android Question Pos printer always prints a 0

Bernaert Dominique

Member
Licensed User
Hello,

I'm trying to integrate a Star TSP650 II.
I can print and I can send the command to cut the paper, thats all working fine.
But when a ticket is printed, the printer always prints a first line with the decimal symbol 0 and I have no idea why it does that.

Does anybody have an idea?
Here below is the routine I use for printing:

Public Sub DirectPrint(aData As String)
BtAdmin.Initialize("BlueTooth")
BTConnection.Initialize("Printer")
Dim PairedDevices As Map
PairedDevices = BTConnection.GetPairedDevices
Dim l As List
Dim DeviceName, MacAddress As String
l.Initialize
For i = 0 To PairedDevices.Size - 1 'Check all devices
l.Add(PairedDevices.GetKeyAt(i))
DeviceName=PairedDevices.Getkeyat(i)
MacAddress=PairedDevices.GetValueAt(i)
Log(DeviceName & " -> " & MacAddress)
If DeviceName.Contains("Star") Then 'Insert the BT-Name of the printer or use the MAC address
Exit
End If
Next

BTConnection.Connect(MacAddress)
Wait For Printer_Connected (Success As Boolean)
If Success Then
Printer.Initialize2(BTConnection.OutputStream,"windows-1252") 'important to print f.e. German/French chars
PrintBuffer= aData
Printer.WriteLine(PrintBuffer)
Printer.Flush
Printer.Close
BTConnection.Disconnect 'disable this if you like
End If

End Sub
 

Bernaert Dominique

Member
Licensed User
Hi Erel,

this is the code that prints the receipt at the end of an order:


B4X:
'Save the records to the history table and clear all information
Private Query As String
Private Cursor1 As Cursor
Private WorkString As String

If CounterZero = 3 Then
CounterZero = 0
StartActivity("Settings")
Return
Else
CounterZero = 0
End If

Starter.SQL1.BeginTransaction
Try
  Query = "SELECT * FROM WorkTable"
  Cursor1 = Starter.SQL1.ExecQuery(Query)
  If Cursor1.RowCount > 0 Then
   For row = 0 To Cursor1.RowCount - 1
Cursor1.Position = row

'HistoryTime
WorkString = "INSERT INTO History(ProductId,ProductName,ProductPrice,ProductTotal,ProductNumber) " _
          & "VALUES(" _
           & Cursor1.GetInt("ProductId") & ",""" _
  & Cursor1.GetString("ProductName") & """," _
  & Cursor1.GetDouble("ProductPrice") & "," _
  & Cursor1.GetDouble("ProductTotal") & "," _
  & Cursor1.GetInt("ProductNumber") _
          & ")" 
Starter.SQL1.ExecNonQuery(WorkString)
   Next
   Starter.SQL1.TransactionSuccessful
  End If
Catch
MsgboxAsync("Fout", "Error")
End Try
Cursor1.Close
Starter.SQL1.EndTransaction

'Print the ticket
Dim Output As String
Dim Line As String
Dim WorkField As String
Dim Now As Long
Dim TotalAmount As Double
Private Query As String
Private Cursor1 As Cursor

Output = ""
TotalAmount = 0

'Header afdrukken
Now  = DateTime.Now
Line = "Tijdstip: " & DateTime.Date(Now) & "  " & DateTime.Time(Now)
Line = Line & Chr(10) & Chr(10)
Output = Output & Line
Query = "SELECT * FROM WorkTable"
Cursor1 = Starter.SQL1.ExecQuery(Query)
If Cursor1.RowCount > 0 Then
For row = 0 To Cursor1.RowCount - 1
Cursor1.Position = row
Line = Cursor1.GetInt("ProductNumber")
Do While Line.Length < 6
Line = Line & " "
Loop

Line = Line & Cursor1.GetString("ProductName")
Do While Line.Length < 35
Line = Line & " "
Loop

TotalAmount = TotalAmount + Cursor1.GetDouble("ProductTotal")
WorkField = NumberFormat(Cursor1.GetDouble("ProductTotal"), 1, 2)
Do While WorkField.Length < 10
WorkField = " " & WorkField
Loop

Line = Line & WorkField
Output = Output & Line & Chr(10)
Next
End If
Cursor1.Close


'Print the total amount
Output = Output & Chr(10) & Chr(10)
Line  = "Totaal bedrag:"
Do While Line.Length < 35
Line = Line & " "
Loop
WorkField = NumberFormat(TotalAmount, 1, 2)
Do While WorkField.Length < 10
WorkField = " " & WorkField
Loop

Line = Line & WorkField
Output = Output & Line
Output = Output&Chr(27)&Chr(97)&Chr(5)&Chr(10)&Chr(27)&Chr(100)&Chr(0)&Chr(48)
DirectPrint(Output)

'Clear the information
Starter.SQL1.BeginTransaction
Try
Starter.SQL1.ExecNonQuery("Delete from Worktable")
Starter.SQL1.TransactionSuccessful
Catch
MsgboxAsync("Fout", "Error")
End Try
Starter.SQL1.EndTransaction
ListViewOrder.Clear
LblNumber2.Text = 1
Calculate_Total
HideAllPanels
PanelFrieten.Visible = True
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
WorkString = "INSERT INTO History(ProductId,ProductName,ProductPrice,ProductTotal,ProductNumber) " _
&
"VALUES(" _
& Cursor1.GetInt(
"ProductId") & ",""" _
& Cursor1.GetString(
"ProductName") & """," _
& Cursor1.GetDouble(
"ProductPrice") & "," _
& Cursor1.GetDouble(
"ProductTotal") & "," _
& Cursor1.GetInt(
"ProductNumber") _
&
")"
Several bad practices:
1. Use smart string literal.
2. Use ExecNonQuery2

What is the output of Log(output) ?

Can you post a picture of the printer output?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
B4X:
Output = Output&Chr(27)&Chr(97)&Chr(5)&Chr(10)&Chr(27)&Chr(100)&Chr(0)&Chr(48)

chr(48) = 0x30 = "0"
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
some pos printers (epson for example) allow you to configure the behaviour of start feed, end feed and cut so that you don't need to send it as extra codes.
 
Upvote 0

Bernaert Dominique

Member
Licensed User
Hi,

removing the chr(48) seems to have solved the 0 printing.
But I'm still having problems.
When trying to print a second ticket, the printer does nothing anymore.

Should I keep the bluetooth connection active?
And if yes, how do I do this?

Thx,
Bernaert Dominique
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
as I see it you send...

5 feeds
1 extra linefeed
cut

maybe you need to send an extra cancel command?

try adding chr(24) right after the chr(0)
 
Upvote 0

rkmoray

Active Member
Licensed User
Longtime User
you said in your first post " I can send the command to cut the paper, thats all working fine."
What code are you using to cut the paper?
 
Upvote 0
Top