最近在搞一个用网页控制串口打印机打印产品条码, 用的斑马打印机。
最开始用的IE 浏览器,用StrokeReader的ocx控件,安装控件后可以正常使用。
后来想是否用firefox及chrome也可用,查了一下,下载了一个ffactivex-setup-r39.exe安装后,也都是可以用了。只是在html页面代码里,IE和Firefox,chorm的代码有所区别。
IE的代码如下:
1 |
<object id="thePort" classid="CLSID:7E42B8C5-73BE-4806-8904-FF4080A696EC" width="0" height="0"></object> |
Firefox,chorm:
1 |
<object id="thePort" clsid="{7E42B8C5-73BE-4806-8904-FF4080A696EC}" TYPE="application/x-itst-activex" width="0" height="0"></object> |
html文件放在github:https://github.com/allenmo/web232
全部html如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html" charset="utf-8"> <title>复制条码</title> <link rel="icon" href="../../favicon.ico" type="image/x-icon" /> <link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon" /> <script type="text/javascript"> function init(){ createPortObject(); var thePort = document.getElementById('thePort'); var PortsAvailable = thePort.PortsAvailable; // alert(PortsAvailable); var arrPorts = PortsAvailable.split(","); var portList = document.getElementById('portList'); var arrPortsLength = arrPorts.length; if(PortsAvailable != ""){ for (var i = 0; i < arrPortsLength; i++) { portList.options.add(new Option("COM" + arrPorts[i], arrPorts[i])); } } else { document.getElementById("initResultStr").innerHTML = "没检测到串口或没有安装打印用ActiveX控件"; } document.getElementById("sn").value = ""; document.getElementById("sn").focus(); } function createPortObject(){ var browserVerStr = getBrowserVer(true); // alert(browserVerStr); if(browserVerStr.indexOf("IE")>=0){ document.getElementById("thePortObject").innerHTML = '<object id="thePort" classid="CLSID:7E42B8C5-73BE-4806-8904-FF4080A696EC" width="0" height="0"></object>'; } else if(browserVerStr.indexOf("Firefox")>=0){ document.getElementById("thePortObject").innerHTML = '<object id="thePort" clsid="{7E42B8C5-73BE-4806-8904-FF4080A696EC}" TYPE="application/x-itst-activex" width="0" height="0"></object>'; } else if(browserVerStr.indexOf("Chrome")>=0){ document.getElementById("thePortObject").innerHTML = '<object id="thePort" clsid="{7E42B8C5-73BE-4806-8904-FF4080A696EC}" TYPE="application/x-itst-activex" width="0" height="0"></object>'; } else { document.getElementById("thePortObject").innerHTML = '<object id="thePort" classid="CLSID:7E42B8C5-73BE-4806-8904-FF4080A696EC" width="0" height="0"></object>'; } } function getBrowserVer(mainBrowserOnly) { var nua = navigator.userAgent; if(!mainBrowserOnly) { if(nua.indexOf("BIDUBrowser/")>=0){return "BIDUBrowser";} if(nua.indexOf("UCBrowser/")>=0){return "UCBrowser";} if(nua.indexOf("UBrowser/")>=0){return "UBrowser";} if(nua.indexOf("TaoBrowser")>=0){return "TaoBrowser";} if(nua.indexOf("LBBROWSER")>=0){return "LBBROWSER";} if(nua.indexOf("Maxthon/")>=0){return "Maxthon";} if(nua.indexOf("QQBrowser/")>=0){return "QQBrowser";} if(nua.indexOf("SE")>=0){return "SE";}//Sogo Explorer } if(nua.indexOf("Trident")>=0)//IE11 { if(nua.indexOf("MSIE 8.0")>=0){return "IE8";} if(nua.indexOf("MSIE 9.0")>=0){return "IE9";} if(nua.indexOf("Xbox")>=0){return "IE XBOX";} if(nua.indexOf("MSIE 10.0")>=0){return "IE10";} if(nua.indexOf("rv:11.0")>=0){return "IE11";} return"Trident"; } if(nua.indexOf("MSIE")>=0)//IE6,7 { if(nua.indexOf("MSIE 6.0")>=0){return "IE6";} if(nua.indexOf("MSIE 7.0")>=0){return "IE7";} return "MSIE"; } if(nua.indexOf("Gecko/")>=0)//Mozilla Firefox series { if(nua.indexOf("Firefox")>=0){return "Firefox";} return "Gecko"; } if(nua.indexOf("WebKit")>=0)//chrome, safari series { if(nua.indexOf("Edge")>=0){return "Edge";} if(nua.indexOf("OPR/")>=0){return "Opera";} if(nua.indexOf("Chrome")>=0){return "Chrome";} if(nua.indexOf("Android")>=0){return "WebKit";} if(nua.indexOf("Safari")>=0){return "Safari";} } if(nua.indexOf("Presto")>=0)//opera series { if(nua.indexOf("Opera")>=0){return "Opera";} return "Presto"; } if(nua.indexOf("Camino")>=0)//ff on mac,stoped development { return "Camino"; } } function printSN(){ document.getElementById("countResultStr").innerHTML = ""; document.getElementById("printResultStr").innerHTML = ""; var portNo = document.getElementById("portList").value; //alert("portNo: " + portNo); if(portNo != ""){ //alert("in print"); var countInput = document.getElementById("count").value.trim() * 1; if(countInput<=10){ var sn = document.getElementById(("sn")).value.trim().toUpperCase(); document.getElementById("sn").value = sn; if(checkSnFormat(sn) == true){ SetPort(portNo); BarCodePrint(portNo, sn, countInput); // setTimeout(function(){alert("SN条码已打印,请对比检查!");}, 1000); alert("SN条码已打印,请对比检查!"); ClosePort(portNo); document.getElementById(("sn")).select(); } else { document.getElementById("printResultStr").innerHTML = "条码格式错误,正确格式为:YYDDDMXXXXX. 其中M为A"; } } else { document.getElementById("countResultStr").innerHTML = "打印条码数量过多,请检查。"; } } else { document.getElementById("printResultStr").innerHTML = "没检测到串口, 本页只支持IE内核浏览器。"; } document.getElementById(("sn")).focus(); } function checkSnFormat(SN){ var Patt1 = new RegExp("^[1-2][0-9][0-3][0-9][0-9]A[0-9]{5}$"); return(Patt1.test(SN)) } function GenZ105SLZPLCode(SN,Qty){ var printerType_600DPI = document.getElementById("600DPI"); var printerType_300DPI = document.getElementById("300DPI"); // var printerType_300DPIX2 = document.getElementById("300DPIX2"); // var printerType_300DPIQAD = document.getElementById("300DPIQAD"); if(printerType_600DPI.checked == true){ var subSN1 = SN.substring(0,4); var subSN2 = SN.substring(4,7); var subSN3 = SN.substring(7,11); $code ="^XA^SZ2^JMA^MCY^PMN^PW578~JSN^MD30^PR3,3,4^JZY^LH0,0^LRN^XZ"; $code +="^XA^FO49,23^BY3^BCN,61,N,N^FD>;" + subSN1 + ">6" + subSN2 + ">5" + subSN3 + "^FS^FT65,127^CI0^A0N,51,70^FD" + SN + "^FS^PQ" + Qty + ",0,1,Y^XZ"; } else if (printerType_300DPI.checked == true) { var subSN1 = SN.substring(0,4); var subSN2 = SN.substring(4,7); var subSN3 = SN.substring(7,11); $code ="^XA^SZ2^JMA^MCY^PMN^PW289~JSN^JZY^LH0,0^LRN^XZ"; $code +="^XA^FO27,5^BY1^BCN,28,N,N^FD>:" + SN + "^FS^FT40,53^CI0^A0N,17,23^FD" + SN + "^FS^PQ" + Qty + ",0,1,Y^XZ"; } // else if (printerType_300DPI.checked == true) { // var subSN1 = SN.substring(0,4); // var subSN2 = SN.substring(4,7); // var subSN3 = SN.substring(7,11); // $code ="^XA^SZ2^JMA^MCY^PMN^PW289~JSN^JZY^LH0,0^LRN^XZ"; // $code +="^XA^FO55,12^BY1^BCN,30,N,N^FD>;" + // subSN1 + ">6" + subSN2 + ">5" + subSN3 + // "^FS^FT27,64^CI0^A0N,25,34^FD" + SN + "^FS^PQ" + Qty + ",0,1,Y^XZ"; // } else if (printerType_300DPIX2.checked == true) { // var subSN1 = SN.substring(0,4); // var subSN2 = SN.substring(4,7); // var subSN3 = SN.substring(7,11); // $code ="^XA^SZ2^JMA^MCY^PMN^PW289~JSN^JZY^LH0,0^LRN^XZ"; // $code +="^XA^FO5,13^BY2^BCN,30,N,N^FD>;" + // subSN1 + ">6" + subSN2 + ">5" + subSN3 + // "^FS^FT52,67^CI0^A0N,25,34^FD" + SN + "^FS^PQ" + Qty + ",0,1,Y^XZ"; // }//300DPIQAD // else if (printerType_300DPIQAD.checked == true) { // var subSN1 = SN.substring(0,4); // var subSN2 = SN.substring(4,7); // var subSN3 = SN.substring(7,11); // $code ="^XA^SZ2^JMA^MCY^PMN^PW289~JSN^JZY^LH0,0^LRN^XZ"; // $code +="^XA^FO27,5^BY1^BCN,28,N,N^FD>:" + // SN + // "^FS^FT40,53^CI0^A0N,17,23^FD" + SN + "^FS^PQ" + Qty + ",0,1,Y^XZ"; // } // $code ="^XA^SZ2^JMA^MCY^PMN^PW289~JSN^JZY^LH0,0^LRN^XZ"; // //Label 217362 // $code +="^XA^FO24,28^BY2^BCN,30,N,N^FD>;"+SN+"^FS^FT45,80^CI0^A0,N,21,29^FD"+SN+"^FS^PQ"+Qty+",0,1,Y^XZ"; // Label 217362 2D // $code +="^XA^FO15,8^BQN,3,3,^FDMM,A>;"+SN+"^FS^FT80,70^CI0^A0N,21,29^FD"+SN+"^FS^PQ"+Qty+",0,1,Y^XZ"; // Label 229855 // $code +="^XA^FO10,60^BQN,2,2,^FDMM,A>;"+SN+"^FS^FT60,40^CI0^A0N,21,29^FD"+SN+"^FS^PQ"+Qty+",0,1,Y^XZ"; // Label 229855 // $code+="^XA^FO88,208^BY2^BCN,30,N,N^FD>;"+SN+"^FS^FT88,100^CI0^A0N,21,29^FD"+SN+""; // $code+="^FS^FO88,48^BY2^BCN,30,N,N^FD>;"+SN+"^FS^FT88,258^CI0^A0N,21,29^FD"+SN+"^FS^PQ"+Qty+",0,1,Y^XZ"; //GK888T print ZPL II Code //$code="^XA^SZ2^JMA^MCY^PMN^PW289~JSN^JZY^LH0,0^LRN^XZ^XA^FO48,42^BY2^BCN,30,N,N^FD>;"+SN+"^FS^FT85,90^CI0^A0N,15,20^FD"+SN+"^FS"; //$code+="^FO48,142^BY2^BCN,30,N,N^FD>;"+SN+"^FS^FT85,190^CI0^A0N,15,20^FD"+SN+"^FS^PQ"+Qty+",0,1,Y^XZ"; //alert($code); return($code); } function BarCodePrint(portNo, SN, Qty){ // SetPort(portNo); // var SN=Request("SN"); // var Qty=Request("Qty"); SendData(GenZ105SLZPLCode(SN,Qty)); } function SendData(data){ var thePort = document.getElementById('thePort'); thePort.Send(data + "\n"); } function SetPort(portNo){ var thePort = document.getElementById('thePort'); thePort.Port = portNo; thePort.BaudRate = 9600; thePort.DataBits = 8; thePort.Parity = 0; //NOPARITY thePort.StopBits = 0; //one stop bit thePort.DataMode = 1; thePort.Connected = 1; if(thePort.error>0){//Display errors if found alert(thePort.ErrorDescription); } } function ClosePort(portNo){ var thePort = document.getElementById('thePort'); thePort.Connected = 0; } function EnterPress(e){ var e = e || window.event; if(e.keyCode == 13){ printSN(); } } //ie6不认识trim方法 if(typeof String.prototype.trim !== 'function') { String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); } } var split; // Avoid running twice; that would break the `nativeSplit` reference split = split || function (undef) { var nativeSplit = String.prototype.split, compliantExecNpcg = /()??/.exec("")[1] === undef, // NPCG: nonparticipating capturing group self; self = function (str, separator, limit) { // If `separator` is not a regex, use `nativeSplit` if (Object.prototype.toString.call(separator) !== "[object RegExp]") { return nativeSplit.call(str, separator, limit); } var output = [], flags = (separator.ignoreCase ? "i" : "") + (separator.multiline ? "m" : "") + (separator.extended ? "x" : "") + // Proposed for ES6 (separator.sticky ? "y" : ""), // Firefox 3+ lastLastIndex = 0, // Make `global` and avoid `lastIndex` issues by working with a copy separator = new RegExp(separator.source, flags + "g"), separator2, match, lastIndex, lastLength; str += ""; // Type-convert if (!compliantExecNpcg) { // Doesn't need flags gy, but they don't hurt separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); } /* Values for `limit`, per the spec: * If undefined: 4294967295 // Math.pow(2, 32) - 1 * If 0, Infinity, or NaN: 0 * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; * If negative number: 4294967296 - Math.floor(Math.abs(limit)) * If other: Type-convert, then use the above rules */ limit = limit === undef ? -1 >>> 0 : // Math.pow(2, 32) - 1 limit >>> 0; // ToUint32(limit) while (match = separator.exec(str)) { // `separator.lastIndex` is not reliable cross-browser lastIndex = match.index + match[0].length; if (lastIndex > lastLastIndex) { output.push(str.slice(lastLastIndex, match.index)); // Fix browsers whose `exec` methods don't consistently return `undefined` for // nonparticipating capturing groups if (!compliantExecNpcg && match.length > 1) { match[0].replace(separator2, function () { for (var i = 1; i < arguments.length - 2; i++) { if (arguments[i] === undef) { match[i] = undef; } } }); } if (match.length > 1 && match.index < str.length) { Array.prototype.push.apply(output, match.slice(1)); } lastLength = match[0].length; lastLastIndex = lastIndex; if (output.length >= limit) { break; } } if (separator.lastIndex === match.index) { separator.lastIndex++; // Avoid an infinite loop } } if (lastLastIndex === str.length) { if (lastLength || !separator.test("")) { output.push(""); } } else { output.push(str.slice(lastLastIndex)); } return output.length > limit ? output.slice(0, limit) : output; }; // For convenience String.prototype.split = function (separator, limit) { return self(this, separator, limit); }; return self; }(); </script> </head> <body onload="{init();}"> <h2>本页面可打印条码</h2> <label for="printerType">请打印选择打印机类型:</label> <input type="radio" name="printerType" value="600DPI" id="600DPI" ></input><label for="600DPI">600DPI</label> <input type="radio" name="printerType" value="300DPI" id="300DPI" checked="checked"></input><label for="300DPI">300DPI</label> <!--input type="radio" name="printerType" value="300DPIX2" id="300DPIX2"></input><label for="300DPIX2">300DPIX2</label> <input type="radio" name="printerType" value="300DPIQAD" id="300DPIQAD"></input><label for="300DPIQAD">300DPIQAD</label--> <br/> <br/> <label for="portList">请选择连接打印机的串口: </label><select id="portList" style="width: 100px;"></select> <span id="initResultStr" style="color: red; padding-left: 20px;"></span><br/> <br/> <br/> <label for="count">打印条码数量: </label><input type="text" id="count" style="width: 181px; " value="1"></input><span>(1 ~ 10)</span> <span id="countResultStr" style="color: red; padding-left: 20px;"></span> <br/> <br/> <label for="sn">请输入SN: </label> <input type="text" id="sn" style="width: 208px;" onkeypress="EnterPress(event)" "></input><!--onkeydown="EnterPress()--> <input type="button" value="打印" style="margin-left: 10px; padding-left: 50px; padding-right: 50px;" onclick="printSN()"></input> <span id="printResultStr" style="color: red; padding-left: 20px;"></span> <span id="snFormatPrompt" style="margin-right: 0px;"> <br/><br/> 格式:YYDDDMXXXXX<br/> "YY": Manufacture year<br/> "DDD": Manufacture day<br/> "M": Manufacturing facility. Value:"A" for Nortek Linear<br/> "XXXXX": Production sequence<br/> </span> <br/> <br/> <a href="../../download/exe/StrokeReaderSetup.exe">打印用ActiveX控件,点击下载后安装,请务必安装</a> <br/> <span>默认支持IE内核浏览器.如IE6-IE11, <a href="../../download/explorers/360se6_6.3.1.196.exe">360浏览器</a></span><br/> <span> <a href="../../download/explorers/Firefox-full-latest.exe">Firefox</a> , <a href="../../download/explorers/44.0.2403.157_chrome_installer.exe">Chrome</a> 浏览器请下载<a href="../../download/explorers/ffactivex-setup-r39.exe">Firefox Activex PlugIn</a>插件安装后可支持打印 </span> <br/> <div id="thePortObject"></div> <!-- <object id="thePort" classid="CLSID:7E42B8C5-73BE-4806-8904-FF4080A696EC" width="0" height="0"></object> --> <!-- <object id="thePort" clsid="{7E42B8C5-73BE-4806-8904-FF4080A696EC}" TYPE="application/x-itst-activex" width="0" height="0"></object> --> </body> </html> |
github上还有开源的跨平台的方案:https://github.com/billhsu/jUART
有时间可以编译一下,看能不能在树莓派上用。