Skip to content

Instantly share code, notes, and snippets.

@tresf
Created August 26, 2015 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tresf/858352c584dd5bfd8737 to your computer and use it in GitHub Desktop.
Save tresf/858352c584dd5bfd8737 to your computer and use it in GitHub Desktop.
<html>
<!-- License: LGPL 2.1 or QZ INDUSTRIES SOURCE CODE LICENSE -->
<head><title>QZ Print Plugin</title>
<script type="text/javascript" src="js/qz-websocket.js"></script>
<script type="text/javascript">
deployQZ();
function startTest() {
setTimeout(100, startSerialLoop("COM5"));
setTimeout(200, startPrintLoop());
}
function stopTest() {
closeSerialPort("COM5");
var count = -1;
}
function startPrintLoop() {
qz.findPrinter("XPS");
}
var count = 0;
function qzDoneFinding() {
console.log("printing some junk...");
qz.append("foo\nbar\n");
qz.append("foo\nbar\n");
qz.printToFile("C:\\Users\\Tres\\Desktop\\print\\" + count++ + ".txt");
qzDonePrinting();
}
function qzDonePrinting() {
//alert("here!");
if (count >= 0) {
setTimeout(function() { qzDoneFinding(); }, Math.random() * 1000);
}
}
function startSerialLoop(portName) {
// Start the serial port probing...
openSerialPort(portName);
}
function getCertificate(callback) {
$.ajax({
method: 'GET',
url: 'assets/signing/public-key.txt',
async: false,
success: callback // Data returned from ajax call should be the site certificate
});
}
function signRequest(toSign, callback) {
$.ajax({
method: 'GET',
url: 'assets/signing/sign-message.php?request=' + toSign,
async: false,
success: callback // Data returned from ajax call should be the signature
});
}
function qzReady() {
}
function qzSocketClose(event) {
alert("Connection was closed:\n"+ event.reason);
}
function qzNoConnection() {
alert("Unable to connect to QZ, is it running?");
}
function openSerialPort(portName) {
qz.openPort(portName);
}
function qzDoneOpeningPort(portName) {
console.log("Port " + portName + " opened");
sendSerialData(portName);
}
function closeSerialPort(portName) {
qz.closePort(portName);
}
function qzDoneClosingPort(portName) {
console.log("Port " + portName + " closed");
alert("Port closed");
}
function sendSerialData(portName) {
//alert("here!");
qz.setSerialBegin("\x02");
qz.setSerialEnd("\r");
qz.setSerialProperties("9600", "7", "1", "even", "none");
qz.send(portName, "\nW\n");
}
function qzSerialReturned(portName, data) {
console.log("Port " + portName + " returned data:\n\t" + data);
document.getElementById("weight").value = data;
setTimeout(function() { sendSerialData(portName); }, Math.random() * 1000);
}
</script>
<script type="text/javascript" src="js/3rdparty/jquery-1.10.2.js"></script>
</head>
<body bgcolor="#FFFFFF">
<input type="button" onClick="startTest()" value="Start test"/><br />
Weight: <input id="weight" type="text" value="Please wait..."/><br />
<input type="button" onClick="stopTest()" value="Stop test"/><br />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment