0

I am trying to send a recipe to the thermal printer tsp650II. What happens is that I can not send commands I can only send letters. This is the code:

func PrintSampleReceipt3Inch(portName : NSString, portSettings : NSString, barca: String, precio: Int, nombreVendedor : String ) -> Bool {

var commands = NSMutableData()

var cmd : [UInt8] = [ 0x1b, 0x1d, 0x61, 0x01 ]

commands.appendBytes(cmd, length: 4)



var str = barca
var datos : NSData? = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
commands.appendData(datos!)

cmd = [ 0x09 ]

commands.appendBytes(cmd, length: 1)

str = String(precio)
datos = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
commands.appendData(datos!)

cmd = [ 0x1b, 0x64, 0x62 ] // Corta el papel

commands.appendBytes(cmd, length: 3)

str = nombreVendedor
datos = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
commands.appendData(datos!)

cmd = [ 0x07 ] // Abre la caja

commands.appendBytes(cmd, length: 1)
println("Commands: \(commands)")
return (sendCommand(commands,portName, portSettings,1000))
}

// This method sends commands to print
func sendCommand(commandsToPrint : NSData, portName : String, portSettings: String, timeoutMillis : UInt32) -> Bool{

var starPort : SMPort? = nil
var commandSize : Int = commandsToPrint.length as Int
println("Tamaño datos a imprimir: \(commandSize)" )

var dataToSentToPrinter = [CUnsignedChar](count: commandSize, repeatedValue: 0)

commandsToPrint.getBytes(&dataToSentToPrinter)


println("commandstoPrint: \(commandsToPrint)")

if let starPort = SMPort.getPort(portName, portSettings, timeoutMillis) {

    var status : StarPrinterStatus_2? = nil
    starPort.beginCheckedBlock(&status, 2)

    if status?.offline == 1 {
        println("Error: La impresora no esta en linea")
        return false
    }

    var endTime : timeval = timeval(tv_sec: 0, tv_usec: 0)
    gettimeofday(&endTime, nil)
    endTime.tv_sec += 60

    println("commandSize : \(commandSize). dataToSEntToPrinter: \(dataToSentToPrinter)")
    var totalAmountWritten : UInt32 = 0
    while (totalAmountWritten < UInt32(commandSize)) {
        var remaining : UInt32  = UInt32(commandSize) - totalAmountWritten
        var amountWritten : UInt32 = starPort.writePort(dataToSentToPrinter, totalAmountWritten, remaining)
        totalAmountWritten += amountWritten

        var now : timeval = timeval(tv_sec: 0, tv_usec: 0)
        gettimeofday(&now, nil)
        if (now.tv_sec > endTime.tv_sec) {
            break
        }


    }

    if totalAmountWritten < UInt32(commandSize) {
        println("Error: Impresion fuera de tiempo")
        return false
    }

    starPort.endCheckedBlockTimeoutMillis = 30000
    starPort.endCheckedBlock(&status!, 2)

    if (status!.offline == 1) {
        println("Error: Printer is offline")
        return false
    }
    SMPort.releasePort(starPort)
} else {
    println("Error: Writte port timed out")
    return false
}


return true
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Chus
  • 57
  • 4
  • This may be unrelated to your problem (and have no experience with that printer), but note that your writing loop is not correct because you always write the *initial* data. For example, if only 50 of 200 bytes were written, then the next loop iteration writes the first bytes again, instead of starting at offset 50. See http://stackoverflow.com/a/25080834/1187415 for an example how it can be done correctly. – Martin R Feb 14 '15 at 22:23
  • Thanks but I get no figure it out. I only get printed numbers and letters but commands that control the printer. If anyone knows what I'm talking and I can help I would appreciate it because I is urgent. II think the line "totalAmountWritten += amountWritten" increases the number of characters sent, is it truth? – Chus Feb 15 '15 at 21:59

1 Answers1

-1

Maybe a little late: but is your printer operation in ESC/POS Mode? You have to switch to this Mode, before you can use ESC-Sequences...