title: pyserial read variable byte array
body: <p>I am writing a code that can flexibly read a mutable array (4 byte array and 7 byte array).</p>
  <p>I don't think it's a good idea to read 7 bytes continuously and judge it by the number of bytes sent in the 2nd byte.
  Reading an array that is sent in 4 bytes as 7 bytes results in slow reception.</p>
  <pre><code>serial_port = serial.Serial(
      port=&quot;/dev/ttyUSB0&quot;,
      baudrate=9600,
      bytesize=serial.EIGHTBITS,
      parity=serial.PARITY_NONE,
      stopbits=serial.STOPBITS_ONE,
  )
  if __name__ == '__main__':
      while True :
          
          time.sleep(1)
          if serial_port.readable():
              serial_port.flushInput()
              serial_port.timeout = None
              data =serial_port.read(7)
              l_data=list(data)
                     
              print(&quot;Header : &quot; , str.join(&quot;&quot;, (&quot;0x%02X &quot; % i for i in l_data)))
  </code></pre>
  <p>Results received from connected equipment</p>
  <p>ouput:</p>
  <pre><code>Header :  0x2B 0x04 0x53 0x7C 0x2B 0x04 0x53   #0x04 is the actual total number of byte
  Header :  0x2b 0x07 0x51 0x06 0x07 0x46 0x3a   #0x07 is the actual total number of byte
  </code></pre>
  <p>If I replace <code>read()</code> with <code>readline()</code>, nothing comes up.
  So did <code>read_until()</code>.
  (Actually, it is because of my lack of skills.)</p>
  <p>I tried to find a solution to find the end of the array (<code>\n</code> or <code>\r</code>) and truncate it, but it didn't work.</p>
  <p>Is there a way to flexibly receive 4 bytes and 7 bytes?</p>