k5web/assets/serial.dacf7476.js
2024-01-27 01:25:20 +08:00

2 lines
3.4 KiB
JavaScript

async function k(){if(!("serial"in navigator))return alert("\u5F53\u524D\u6D4F\u89C8\u5668\u4E0D\u652F\u6301\u7F51\u9875\u4E32\u53E3\u529F\u80FD\uFF0C\u8BF7\u4F7F\u7528 Chrome, Edge, Opera \u6D4F\u89C8\u5668\u3002"),null;try{const e=await navigator.serial.requestPort();return await e.open({baudRate:38400}),e}catch(e){return console.error("Error connecting to the serial port:",e),null}}async function U(e){try{e&&e.readable?(await e.close(),console.log("Serial port disconnected.")):console.warn("Serial port is not open.")}catch(n){console.error("Error closing the serial port:",n)}}function y(e){let n=new Uint8Array(e);const r=new Uint8Array([22,108,20,230,46,145,13,64,33,53,213,64,19,3,233,128]);for(let o=0;o<n.length;o++)n[o]^=r[o%r.length];return n}function g(e,n=0){for(let o=0;o<e.length;o++){n^=e[o]<<8;for(let t=0;t<8;t++)n&32768?n=n<<1^4129:n<<=1;n&=65535}return n}function b(e){const n=new Uint8Array([171,205]),r=new Uint8Array([e.length&255,e.length>>8&255]),o=new Uint8Array([g(e)&255,g(e)>>8&255]),t=new Uint8Array([...e,...o]),i=y(t),f=new Uint8Array([220,186]);return new Uint8Array([...n,...r,...i,...f])}function m(e){const n=new Uint8Array([e[2],e[3]]),r=e.slice(4,e.length-4);if(r.length!==n[0]+(n[1]<<8))throw"Packet length does not match the length field.";return y(r)}async function w(e,n,r=1e3){const o=e.readable.getReader();let t=new Uint8Array,i;try{return await new Promise((f,c)=>{function s({value:a,done:p}){if(p){c("Reader has been cancelled."),console.log("Reader has been cancelled. Current Buffer:",t,A(t));return}for(t=new Uint8Array([...t,...a]);t.length>0&&t[0]!==171;)t=t.slice(1);for(;t.length>=4&&t[0]===171&&t[1]===205;){const l=t[2]+(t[3]<<8),d=l+8;if(t.length>=d){const u=t.slice(0,d);if(u[l+6]===220&&u[l+7]===186){t=t.slice(d);const h=m(u);if(h[0]!==n){console.log("Unexpected packet received:",h);continue}f(h);return}else t=t.slice(1)}else break}o.read().then(s).catch(l=>{console.error("Error reading data from the serial port:",l),c(l)})}o.read().then(s).catch(a=>{console.error("Error reading data from the serial port:",a),c(a)}),i=setTimeout(()=>{o.cancel().then(()=>{c("Timeout: Packet not received within the specified time.")}).catch(a=>{console.error("Error cancelling reader:",a),c(a)})},r)})}finally{clearTimeout(i),o.releaseLock()}}async function x(e,n){try{const r=e.writable.getWriter(),o=b(n);await r.write(o),r.releaseLock()}catch(r){return console.error("Error sending packet:",r),console.log("Error sending packet. Aborting."),Promise.reject(r)}}function A(e){return Array.from(e).map(n=>n.toString(16).padStart(2,"0")).join("")}async function _(e){const n=new Uint8Array([20,5,4,0,255,255,255,255]);await x(e,n);const r=await w(e,21),o=new TextDecoder,t=new Uint8Array(r.slice(4,4+16));return o.decode(t.slice(0,t.indexOf(0)))}async function D(e,n,r=128){const o=(n&65280)>>8,t=n&255,i=(n&4278190080)>>24,f=(n&16711680)>>16,c=new Uint8Array([43,5,8,0,f,i,128,0,255,255,255,255,t,o]);await x(e,c);const s=await w(e,28);if(s[6]!==r)throw"eeprom read reply has wrong size.";return new Uint8Array(s.slice(8))}async function E(e,n,r,o=128){const t=(n&65280)>>8,i=n&255,f=(n&4278190080)>>24,c=(n&16711680)>>16,s=new Uint8Array([56,5,28,0,c,f,130,0,255,255,255,255,i,t]),a=new Uint8Array(s.length+r.length);return a.set(s),a.set(r,s.length),await x(e,a),await w(e,30),!0}async function P(e){const n=new Uint8Array([221,5]);return await x(e,n),!0}async function T(e){return alert("TODO"),null}export{T as a,D as b,k as c,U as d,_ as e,E as f,P as g};