代码如下:
function WaitForResponceBTPR2() {
try {
var printComplete = false;
var recvBuffer = new String();
var sendBuffer = new String();
var objRecv = new Bt.LibDef.CommSppRecvData("");
for (var loopCount = 0; loopCount < 10; loopCount++) {
if (SPPRecv(objRecv, 1, 1) != Bt.LibDef.BT_OK) {
//continue;
}
recvBuffer = objRecv.data;
// ACK receive=>Xmit ENQ and re-receive
if (recvBuffer.charAt(0) == ACK) {
sendBuffer = "";
sendBuffer = ENQ;
if (SPPSend(sendBuffer) != Bt.LibDef.BT_OK) {
window.alert("error:1");
continue;
}
}
// NACK reception=>End response processing
if (recvBuffer.charAt(0) == NAK) {
window.alert("error:NAK");
break;
}
// Check STX
if (recvBuffer.charAt(0) != STX) {
loopCount = 0;
continue;
}
// STX reception=>Start telegram reception
if (SPPRecv(objRecv, 10, 10) != Bt.LibDef.BT_OK) {
window.alert("error:3");
continue;
}
recvBuffer = objRecv.data;
// Check ETX
if (recvBuffer.charAt(9) != ETX) {
window.alert(recvBuffer.charAt(9) + 2);
continue;
}
// Response processing 1 (ENQ transmission)
if ((recvBuffer.charAt(2) == "\x47") ||
(recvBuffer.charAt(2) == "\x48") ||
(recvBuffer.charAt(2) == "\x53") ||
(recvBuffer.charAt(2) == "\x54")) {
Sleep(200);
sendBuffer = "";
sendBuffer = ENQ;
if (SPPSend(sendBuffer) != Bt.LibDef.BT_OK) {
window.alert("error:5");
continue;
}
}
// Response processing 2
// recvBuffer.charAt(2) 打印机状态代码,打印时获取
if ((recvBuffer.charAt(2) != "\x00") &&
(recvBuffer.charAt(2) != "\x01") &&
(recvBuffer.charAt(2) != "\x41") &&
(recvBuffer.charAt(2) != "\x42") &&
(recvBuffer.charAt(2) != "\x4E") &&
(recvBuffer.charAt(2) != "\x4D")) {
continue;
}
printComplete = true;
break;
}
if (printComplete) {
window.alert("The printing operation was completed successfully.");
return;
}
}
catch (e) {
window.alert("error:" + e);
}
window.alert("Printing failed.");
return;
}