打印机命令

时间:2015-02-14 20:59:07

标签: swift printing command

我正在尝试将配方发送到热敏打印机tsp650II。会发生什么是我无法发送命令我只能发信。 这是代码:

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
}

1 个答案:

答案 0 :(得分:-1)

也许有点晚了但是你的打印机是在ESC / POS模式下操作吗?您必须切换到此模式,然后才能使用ESC-Sequences ...