How to produce errors:
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 | rfidReader = { webview: null, connectionId: '', connected: false, infoInterval: null, stringReceived: '', /** * Initialize the rfid reader connection and register the onReceive event * * @return void */ init: function() { var self = this; window.addEventListener('load', function() { self.webview = document.getElementById('guestportalWebview'); self.webview.addEventListener("loadstop", function() { if (self.connected) { return; } // load all devices chrome.serial.getDevices(self.getDevicesCallback); chrome.serial.onReceive.addListener(self.onReceiveCallback); }); }); }, /** * Convert an array buffer into a string. * * @param array data * * @return String */ convertArrayBufferToString: function (data) { return String.fromCharCode.apply(null, new Uint8Array(data)); }, /** * Removes not used numbers from the serial readout and trim it. * This has to be done, otherwise the card id is not usable. * * Example: * 39383431303030313131333036393032 converts to 9841000111306902 * * @param String result * * @return String */ convertResult: function(result) { return result.replace(/\d(\d)/g, '$1').replace(/^\s+|\s+$/gm, ''); }, /** * The callback for fetching the available serial devices. * * @param array ports Array of XXX with all available devices * * @return void */ getDevicesCallback: function(ports) { var path = null; if (typeof ports === 'undefined') { rfidReader.showError('No serial devices found.'); return; } else if (ports.length === 0) { if (clientInformation.platform.match(/linux/i)) { //we do not get any information about serial devices // on linux, so we simply assume there is one path = '/dev/ttyS0'; } else { rfidReader.showError('No serial devices found.'); return; } } else { path = ports[0].path; } // connect to the first serial device chrome.serial.connect(path, {bitrate: 19200}, function(connectionInfo) { if (typeof connectionInfo === 'undefined') { rfidReader.showError('Cannot access serial device.'); return; } rfidReader.connectionId = connectionInfo.connectionId; rfidReader.connected = true; }); }, /** * The callback for fetching the data sent by the serial reader. * * @param object info The readout from the serial port * * @return void */ onReceiveCallback: function(info) { if (info.connectionId == rfidReader.connectionId && info.data) { var str = rfidReader.convertArrayBufferToString(info.data); if (str.charAt(str.length-1) === "\n") { rfidReader.stringReceived += str.substring(0, str.length-1); rfidReader.stringReceived = rfidReader.convertResult(rfidReader.stringReceived); if (rfidReader.stringReceived === '') { console.log('received an empty string'); return; } // send the retreived data to the webview content rfidReader.webview.contentWindow.postMessage({cardId: rfidReader.stringReceived}, '*'); rfidReader.stringReceived = ''; } else { rfidReader.stringReceived += str; } } }, showError: function(message) { console.error(message); document.getElementById('error').appendChild(document.createTextNode(message)); document.getElementById('error').style.display = 'block'; } }; rfidReader.init(); |