You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'use strict';/** * util function * * @param str * @returns {boolean} */functionemptyOrUndefined(str){return(str===''||str===undefined||str==='undefined');}constscale={// this must be setscaleType: undefined,scalePort: undefined,errorHandler: undefined,scaleTypes: [['adams','Adams'],['adamsNTEP','Adams NTEP'],['cardinal','Cardinal 190'],['cas','CAS'],['optima','Legend/Optima'],['optima-2','Legend/Optima version 2'],['mettler','Mettler'],['ohaus','Ohaus'],['torrey','Torrey']],modalChg: function(state){if(state==='show'){// start the scale sendingif(scale.scaleType==='cas'){// send the W with even parity to start the scaleletthat=thisqz.serial.sendData(scale.scalePort,'W','').catch(that.errorHandler);}if(scale.scaleType==='torrey'){// send the W with even parity to start the scaleletthat=thisqz.serial.sendData(scale.scalePort,'P','').catch(that.errorHandler);}}elseif(state==='hide'){// stop the scale from sendingif(scale.scaleType==='cas'){// send the 0x0d with even parity to stop the scaleletthat=thisqz.serial.sendData(scale.scalePort,'\r','').catch(that.errorHandler);}}},/** * deal with errors by logging to the console or calling the errorHandler * * @param msg */handleError: function(msg){if(this.errorHandler===undefined){console.log(msg);}else{this.errorHandler(msg);}},/** * * @param obj */fillInList: function(obj){for(leti=0;i<this.scaleTypes.length;i++){letopt=newOption(this.scaleTypes[i][1],this.scaleTypes[i][0]);obj.add(opt);if(!emptyOrUndefined(this.scaleType)){// if scaleType is set, then we want to select the scaleType that matchesif(this.scaleTypes[i][0]===this.scaleType){obj.selectedIndex=i;}}}},/** * * @param scaleType * @param scalePort */openPort: function(scaleType=this.scaleType,scalePort=this.scalePort){letproperties;if(!emptyOrUndefined(scaleType)&&this.scaleType!==scaleType){this.scaleType=scaleType;}if(!emptyOrUndefined(scalePort)&&this.scalePort!==scalePort){this.scalePort=scalePort;}if(this.scaleType==='adams'){properties={baudRate: 9600,dataBits: 8,stopBits: 1,parity: 'NONE',flowControl: 'NONE',encoding: 'UTF-8',rx: {start: 'G',end: '\x0a',width: null,lengthBytes: null,crcBytes: null,includeHeader: true,encoding: '',scaleType: this.scaleType}};}elseif(this.scaleType==='adamsNTEP'){properties={baudRate: 19200,dataBits: 8,stopBits: 1,parity: 'NONE',flowControl: 'NONE',encoding: 'UTF-8',rx: {start: null,end: null,width: null,lengthBytes: null,crcBytes: null,includeHeader: true,encoding: '',parseLines: true}};}elseif(scaleType==='cardinal'){properties={baudRate: 9600,dataBits: 8,stopBits: 1,parity: 'NONE',flowControl: 'NONE',encoding: 'UTF-8',rx: {start: '\u000a',end: '\u000d',width: null,lengthBytes: null,crcBytes: null,includeHeader: false,encoding: '',parseLines: false,debug: 1,scaleType: 'cardinal'}};}elseif(this.scaleType==='cas'){properties={baudRate: 9600,dataBits: 8,stopBits: 1,parity: 'NONE',flowControl: 'NONE',encoding: 'UTF-8',rx: {start: '\u0002',end: '\u000d',width: null,lengthBytes: null,crcBytes: null,includeHeader: false,encoding: '',parseLines: false,scaleType: this.scaleType}};}elseif(this.scaleType==='torrey'){properties={baudRate: 9600,dataBits: 8,stopBits: 1,parity: 'NONE',flowControl: 'NONE',encoding: 'UTF-8',rx: {start: null,end: '\u000d',width: null,lengthBytes: null,includeHeader: true,encoding: '',crcBytes: null,parseLines: true,scaleType: this.scaleType}};}elseif(this.scaleType==='mettler'||this.scaleType==='ohaus'){// for a real mettler it should be 7 databits 1 stop and 1 parity EVENproperties={baudRate: 9600,dataBits: 8,stopBits: 1,parity: 'NONE',flowControl: 'NONE',encoding: 'UTF-8',rx: {start: null,end: null,width: null,lengthBytes: null,crcBytes: null,includeHeader: true,encoding: '',parseLines: true,scaleType: this.scaleType}};}elseif(this.scaleType==='optima'){properties={baudRate: 9600,dataBits: 8,stopBits: 1,parity: 'NONE',flowControl: 'NONE',encoding: 'UTF-8',rx: {start: '\u0002',end: '\u000a',width: null,lengthBytes: null,crcBytes: null,includeHeader: false,encoding: '',parseLines: false,scaleType: this.scaleType}};}elseif(this.scaleType==='optima-2'){properties={baudRate: 9600,dataBits: 8,stopBits: 1,parity: 'NONE',flowControl: 'NONE',encoding: 'UTF-8',rx: {start: '\u0002',end: '\u000d',width: null,lengthBytes: null,crcBytes: null,includeHeader: false,encoding: '',parseLines: false,scaleType: this.scaleType}};}elseif(emptyOrUndefined(this.scaleType)||emptyOrUndefined(this.scalePort)){// looks like things were not setup correctly so error out// show something to the user and balethis.handleError('Either scale port or scale type is not set correctly');return;}if(this.scalePort==='COMSIM'){this.handleError('Simulating COM port');}else{if(!emptyOrUndefined(cookies.getCookie('serialDebug'))){// set the baud to 115200properties['baudRate']=115200;}// need to create variable for thisletthat=this;qz.serial.openPort(this.scalePort,properties).then(function(){// we opened port display something, somewhere to let the user know// or maybe only display when it failsthat.handleError(`${that.scaleType} port ${that.scalePort} opened`);if(!emptyOrUndefined(cookies.getCookie('serialDebugSend'))){// if we want to debug the serial then send some dataconsole.log('We sent it');qz.serial.sendData(that.scalePort,'H\r\n','').catch(displayError);}}).catch(displayError);}},/** * */closePort: function(){if(emptyOrUndefined(this.scalePort)){// looks like things were not setup correctly so error out// show something to the user and balethis.handleError('Scale port is not set');return;}letthat=this;qz.serial.closePort(this.scalePort).then(function(){that.handleError('${this.scaleType} port ${this.scalePort} closed');}).catch(displayError);},/** * This is the serial callback handler * * @param evt * @param shouldI * @param inputHandler */handleInput : function(evt,shouldI,inputHandler){if(evt.type!=='ERROR'){if(shouldI()){// we received a message from the scale.//displayMessage('Serial', evt.portName, 'received output', evt.output);// parse the inputletcurWeight;if(this.scaleType==='adams'){// parse info from adams// get the stateif(evt.output.length===2){// this is just a CR/LF coming back. We may need to keep track of these}else{//let parsedOutput = evt.output.split(' ');letparsedOutput=evt.output.replace(/\s\s+/g,' ');//let parsedOutput = evt.output.replace(/ +/g, ' ');parsedOutput=parsedOutput.split(' ');if((parsedOutput.length>=3)&&(parsedOutput[0]==='G/W:+')){curWeight=parsedOutput[1];}}}elseif(this.scaleType==='adamsNTEP'){// adams NTEP outputs a few lines, remove all multiple spacesletparsedOutput=evt.output.replace(/ +/g,' ');// now remove all CRparsedOutput=parsedOutput.replace(/\r/g,'');parsedOutput=parsedOutput.split('\n');letlastEntry=parsedOutput.length-1;if((lastEntry===6)&&parsedOutput[lastEntry].startsWith('Gross')){letgrossWeight=parsedOutput[lastEntry].split(' ');if(grossWeight.length===4){curWeight=grossWeight[2];}}}elseif(this.scaleType==='mettler'){// parse info from mettler, it seems it is one line 'Net x.yy lb \r\n'letparsedOutput=evt.output.split(' ');if((parsedOutput.length===4)&&(parsedOutput[0]==='Net')&&(parsedOutput[2]==='lb')){// parsedOutput[1] should be our weightcurWeight=parsedOutput[1];}}elseif(this.scaleType==='ohaus'){// parse info from ohaus, it seems it is one line '+/- x.yy(7) units(5) ?(1) legend(3)\r\n'letparsedOutput=evt.output.split(/\s+/);// if the weight is positive and it is stable, Then there are 3 fieldsif((parsedOutput.length===4)&&(parsedOutput[2]==='lb')&&(parsedOutput[3]==='NET')){// parsedOutput[0] should be our weightcurWeight=parsedOutput[1];}}elseif(this.scaleType==='cas'){// parse info from cas, it is 5 bytes with a leading 0// it is 0xxxx and we have to change it into xx.xxletparsedOutput=evt.output;if((parsedOutput.length===5)&&(parsedOutput.charAt(0)==='0')){curWeight=parseInt(parsedOutput)/100.0;}}elseif(this.scaleType==='torrey'){// parse info from torrey, it is 5 bytes with a leading 0// it is 0xxxx and we have to change it into xx.xxletparsedOutput=evt.output.replace(/\s\s+/g,' ');if(parsedOutput.length>=5){parsedOutput=parsedOutput.split(' ');curWeight=parsedOutput[1];}}elseif(this.scaleType==='optima'){// parse info from optimaletparsedOutput=evt.output;//alert(parsedOutput);if(((parsedOutput.length===13)||(parsedOutput.length===12))&&(parsedOutput.charAt(parsedOutput.length-1)==='\r')){parsedOutput=parsedOutput.substr(1,8);curWeight=parseFloat(parsedOutput);}}elseif(this.scaleType==='optima-2'){// parse info from optimaletparsedOutput=evt.output;if(((parsedOutput.length===14)||(parsedOutput.length===13))){parsedOutput=parsedOutput.substr(1,8);curWeight=parseFloat(parsedOutput);}}elseif(this.scaleType==='cardinal'){// parse info from consolidatedletparsedOutput=evt.output;if(parsedOutput.length===18){parsedOutput=parsedOutput.substr(5,10);curWeight=parseFloat(parsedOutput);}}if(curWeight!==undefined){inputHandler(curWeight);}}}else{this.handleError(evt.exception);}}};
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: