Advertisement
nenad_jovanovski

seriska comm javascript

Nov 20th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function initCommand(port, command, commandData) {
  2.     let seq = ' '
  3.     const encoder = new TextEncoder('windows-1251')
  4.     const decoder = new TextDecoder('windows-1251')
  5.     const dataPrefix = `\x01%c%c%c%s\x05%c%c%c%c\x03`
  6.     let data = new Uint8Array(255)
  7.     let data2 = new Uint8Array(255)
  8.     let checkSum = 0
  9.     let len, i, bytwrite, bytread, tries
  10.     let bcc = new Uint8Array(4)
  11.  
  12.     let commandString = String.fromCharCode(seq, command, ...commandData)
  13.     let dataString = dataPrefix.replace('%c', seq).replace('%c', command).replace('%s', commandData)
  14.     data = encoder.encode(dataString)
  15.  
  16.     for (len = 1; data[len] !== 0x05; len++) checkSum += data[len]
  17.  
  18.     len += 32 - 1
  19.     checkSum = checkSum - '%' - 'c' + len + 0x05
  20.  
  21.     i = (checkSum & 0xf) + 0x30
  22.     bcc[0] = i
  23.     i = ((checkSum & 0xf0) >> 4) + 0x30
  24.     bcc[1] = i
  25.     i = ((checkSum & 0xf00) >> 8) + 0x30
  26.     bcc[2] = i
  27.     i = ((checkSum & 0xf000) >> 12) + 0x30
  28.     bcc[3] = i
  29.  
  30.     data2 = encoder.encode(dataString.replace('%c', len).replace('%c', bcc[3]).replace('%c', bcc[2]).replace('%c', bcc[1]).replace('%c', bcc[0]))
  31.  
  32.     if (seq === '#') seq = ' '
  33.     else seq = '#'
  34.  
  35.     len = data2.length
  36.  
  37.     const writer = port.writable.getWriter()
  38.     try {
  39.       for (i = 0; i < len; i++) {
  40.         await writer.write(data2.slice(i, i + 1))
  41.       }
  42.     } finally {
  43.       writer.releaseLock()
  44.     }
  45.  
  46.     await new Promise((resolve) => setTimeout(resolve, 60))
  47.  
  48.     data = new Uint8Array(255)
  49.  
  50.     const reader = port.readable.getReader()
  51.     try {
  52.       for (tries = 0; tries < 5; tries++) {
  53.         const { value, done } = await reader.read()
  54.         if (done) break
  55.         if (value[0] === 0x16) {
  56.           tries--
  57.           await new Promise((resolve) => setTimeout(resolve, 60))
  58.         } else if (value[0] === 0x01) {
  59.           len = 1
  60.           while (value[len - 1] !== 0x03) {
  61.             const { value: chunk, done: chunkDone } = await reader.read()
  62.             if (chunkDone) break
  63.             data.set(chunk, len)
  64.             len += chunk.length
  65.           }
  66.           data[len] = 0
  67.           break
  68.         }
  69.       }
  70.     } finally {
  71.       reader.releaseLock()
  72.     }
  73.  
  74.     if (data[0] === 0) console.log('ERROR: No bytes to read')
  75.     console.log(decoder.decode(data.slice(4)))
  76.  
  77.     return data
  78.   }
  79.  
  80.  
  81.  
  82. /// POVIKUVANJE NA FUNKCIJATA
  83.  
  84. const str = '2Е2'
  85. let port
  86. try {
  87.     port = await navigator.serial.requestPort()
  88.     if (port.readable || port.writable) {
  89.         await port.close()
  90.     }
  91.     await port.open({ baudRate: 9600 })
  92.  
  93.     const lines = str
  94.     .split('\n')
  95.     .map((line) => line.trim())
  96.     .filter((line) => line.length > 0)
  97.  
  98.     for (const line of lines) {
  99.         await initCommand(port, line[0], line.slice(1))
  100.     }
  101. } catch (error) {
  102.     console.error('ERROR:', error)
  103.     await port.close()
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement