diff --git a/internal.js b/internal.js index 8a9514d..97c6573 100644 --- a/internal.js +++ b/internal.js @@ -31,15 +31,19 @@ var p7 = { } /* Convert an ascii array into a string */ - p7.asciiToString = (array) => String.fromCharCode.apply(null, array); + Uint8Array.prototype.asciiToString = function () { + let string = []; + this.forEach((element) => string.push(String.fromCharCode(element))); + return string.join(''); + } /* Convert an ascii array representing an hex number into a number */ - //Uint8Array.prototype.fromAsc - p7.asciiToHex = (array) => Number('0x' + p7.asciiToString(array)); - + Uint8Array.prototype.asciiToHex = function () { + return Number('0x' + this.asciiToString()); + } p7.sendBuffer = new Uint8Array(1032); - p7.receiveBuffer = undefined;//new Uint8Array(1032 + 1); + p7.receiveBuffer = undefined; //new Uint8Array(1032 + 1); /* ---------------------Packet making--------------------- */ @@ -128,96 +132,77 @@ var p7 = { /* Return all the informations of the device, and trim string's overage (255) */ p7.decode.extendedAckPacket = () => { return { - hardwareIdentifier : p7.asciiToString(p7ReceiveBuffer.slice(8, 16)), - processorIdentifier : p7.asciiToString(p7ReceiveBuffer.slice(16, 32)), - preprogrammedROMCapacity : p7.asciiToString(p7ReceiveBuffer.slice(32, 40)), - flashROMCapacity : p7.asciiToString(p7ReceiveBuffer.slice(40, 48)), - ramCapacity : p7.asciiToString(p7ReceiveBuffer.slice(48, 56)), - prepogrammedROMVersion : p7.asciiToString(p7ReceiveBuffer.slice(56, 72).filter(number => number !== 255)), - bootCodeVersion : p7.asciiToString(p7ReceiveBuffer.slice(72, 88).filter(number => number !== 255)), - bootCodeOffset : p7.asciiToString(p7ReceiveBuffer.slice(88, 96).filter(number => number !== 255)), - bootCodeSize : p7.asciiToString(p7ReceiveBuffer.slice(96, 104).filter(number => number !== 255)), - osCodeVersion : p7.asciiToString(p7ReceiveBuffer.slice(104, 120).filter(number => number !== 255)), - osCodeOffset : p7.asciiToString(p7ReceiveBuffer.slice(120, 128)), - osCodeSize : p7.asciiToString(p7ReceiveBuffer.slice(128, 136)), - protocolVersion : p7.asciiToString(p7ReceiveBuffer.slice(136, 140)), - productID : p7.asciiToString(p7ReceiveBuffer.slice(140, 156).filter(number => number !== 255)), - username : p7.asciiToString(p7ReceiveBuffer.slice(156, 172).filter(number => number !== 255)) + hardwareIdentifier : p7ReceiveBuffer.slice(8, 16).asciiToString(), + processorIdentifier : p7ReceiveBuffer.slice(16, 32).asciiToString(), + preprogrammedROMCapacity : p7ReceiveBuffer.slice(32, 40).asciiToString(), + flashROMCapacity : p7ReceiveBuffer.slice(40, 48).asciiToString(), + ramCapacity : p7ReceiveBuffer.slice(48, 56).asciiToString(), + prepogrammedROMVersion : p7ReceiveBuffer.slice(56, 72).filter(number => number !== 255).asciiToString(), + bootCodeVersion : p7ReceiveBuffer.slice(72, 88).filter(number => number !== 255).asciiToString(), + bootCodeOffset : p7ReceiveBuffer.slice(88, 96).filter(number => number !== 255).asciiToString(), + bootCodeSize : p7ReceiveBuffer.slice(96, 104).filter(number => number !== 255).asciiToString(), + osCodeVersion : p7ReceiveBuffer.slice(104, 120).filter(number => number !== 255).asciiToString(), + osCodeOffset : p7ReceiveBuffer.slice(120, 128).asciiToString(), + osCodeSize : p7ReceiveBuffer.slice(128, 136).asciiToString(), + protocolVersion : p7ReceiveBuffer.slice(136, 140).asciiToString(), + productID : p7ReceiveBuffer.slice(140, 156).filter(number => number !== 255).asciiToString(), + username : p7ReceiveBuffer.slice(156, 172).filter(number => number !== 255).asciiToString() } }; - /* Return the value (as an number) of the Filesize (FS) subfield of the Data (D) field of an extended command packet */ - p7.decode.commandPacketFilesize = () => p7.asciiToHex(p7.receiveBuffer.slice(12, 20)); + /* Rturn the value (as an number) of the Filesize (FS) subfield of the Data (D) field of an extended command packet */ + p7.decode.commandPacketFilesize = () => p7.receiveBuffer.slice(12, 20).asciiToHex(); /* ---------------------Packet receiving--------------------- */ - /* Doesn't work for now on */ p7.receive.packet = async function () { - /*try { - let array = undefined; - do { - array = await p7.device.transferIn(2, 64); - console.log(array); - } while (array.data.byteLength == 0) - - p7.receiveBuffer = Uint8Array(await p7.device.transferIn(2, 64).data.buffer); - } catch (err) { - throw new Error("Couldn't receive the first 64 bytes of the packet : " + err.message); - } + let checksumError = 0; + try { + let transferInResult = undefined; + while (! (transferInResult = await p7.device.transferIn(2, 64)).data.byteLength) + console.log('The buffer was empty, trying again!'); + p7.receiveBuffer = new Uint8Array(transferInResult.data.buffer); + } catch (err) { + throw new Error("Couldn't receive the first 64 bytes of the packet: " + err.message); + } + console.log(p7.receiveBuffer); let packetInfo = {}; + packetInfo.length = 6; /* Type (T), Subtype (S), Extended (EX) and Checksum (CS) fields */ + //Set Type (T) field packetInfo.type = p7.receiveBuffer[0]; //Set Subtype (ST) field - packetInfo.subtype = p7.asciiToHex(p7.receiveBuffer.slice(1, 2)); + packetInfo.subtype = p7.receiveBuffer.slice(1, 2).asciiToHex(); //Set Extended (EX) field packetInfo.extended = (p7.receiveBuffer[3] === 0x31); + if (!packetInfo.extended) { + /* Compute checksum */ + checksumError = ((packetInfo.checksum = p7.receiveBuffer.slice(4, 5).asciiToHex) !== p7.receiveBuffer.slice(1, 4).checksum()); + } else { + //Set Data size (DS) field + packetInfo.dataSize = p7.receiveBuffer.slice(4, 8).asciiToHex(); + + packetInfo.length += packetInfo.dataSize + 4 /* Data size field */; + + + } console.log(packetInfo); -*/ - - let transferInResult = 0; - let err = 0; - //Recieve and parse the packet - do { - transferInResult = await p7.device.transferIn(2, 64).then( - (result) => { - return result; - }, (error) => { - err = error; - }); - if (err) - return "Couldn't receive the first 64 bytes of the packet : " + err; - console.log('retrying'); - } while(!transferInResult.data.byteLength); - - - //p7ReceivedPacketInfo.packetSize = 4; /*Type, Subtype and Extended field*/ - - //Copy the received buffer to p7ReceiveBuffer - p7.receiveBuffer.set(new Uint8Array(transferInResult.data.buffer), 0); - console.log(p7.receiveBuffer.slice(0, 25)); } /* ---------------------Packet sending--------------------- */ /* Doesn't work for now on */ p7.send.packet = async function (length) { - - /*try { + try { await p7.device.transferOut(1, p7.sendBuffer.slice(0, length)); } catch (err) { - throw new Error("Couldn't send the packet : " + err.message); - }*/ - - let err = 0; - - //Send the packet and return the error on failure - if (err = await p7.device.transferOut(1, p7.sendBuffer.slice(0, length)).then((success) => 0, (failure) => failure)) - return "Couldn't send the packet : " + err; + throw new Error("Couldn't send the packet: " + err.message); + } }; /* ---------------------Initialization and exiting--------------------- */ @@ -230,7 +215,7 @@ var p7 = { {'vendorId': 0x07cf , 'productId': 0x6102}] /* fx-CP400 */ }); } catch (err) { - throw new Error("Couldn't find the calculator : " + err.message); + throw new Error("Couldn't find the calculator: " + err.message); } await p7.device.open(); @@ -254,7 +239,8 @@ var p7 = { - document.getElementById('connect').addEventListener('click', async function () { + document.getElementById('connect').addEventListener('click', + async function () { try { await p7.init(); } catch (err) { @@ -364,22 +350,19 @@ var p7 = { console.log(err); } */ - p7.make.basicPacket(0x06, 0x00); - console.log(p7.sendBuffer.slice(0, 8)); -/* try { - await p7.send.packet(6); + await p7.send.packet(p7.make.basicPacket(p7PacketType.check, checkSubtype.initialization)); + await p7.receive.packet(); } catch (err) { - console.log('err'); - }*/ + console.log(err); + } - let err = 0; - if (err = await p7.send.packet(6)) - console.log('err' + err); + + + await p7.send.packet(p7.make.basicPacket(p7PacketType.check, checkSubtype.default)); - /* Error: Stay in an endless loop */ await p7.receive.packet(); - }); - - + } + ); + })(); \ No newline at end of file