title: python - How to read data fully from serial port?
body: <p>I'm trying to read a JSON string written to serial port, using the following code based on <code>PySerial</code> library:</p>
  
  <pre><code>while True:
      if serial_port.in_waiting &gt; 0:
          buffer = serial_port.readline()
          print('buffer=', buffer)
          ascii = buffer.decode('ascii')
          print('ascii=', ascii)
  </code></pre>
  
  <p>I tried to sniff the data on the port, and got sure that the data gets written fully without any scattering:</p>
  
  <pre><code>jpnevulator --ascii --tty "/dev/ttyACM1" --read
  7B 22 30 22 3A 31 7D                            {"0":1}
  7B 22 30 22 3A 32 7D                            {"0":2}
  7B 22 30 22 3A 33 7D                            {"0":3}
  7B 22 30 22 3A 34 7D                            {"0":4}
  7B 22 30 22 3A 35 7D                            {"0":5}
  7B 22 30 22 3A 36 7D                            {"0":6}
  </code></pre>
  
  <p>However the used code results in scattered data read, and therefore shows the following results:</p>
  
  <pre><code>buffer= b'{"0'
  ascii= {"0
  buffer= b'":1}'
  ascii= ":1}
  buffer= b'{"'
  ascii= {"
  buffer= b'0":2'
  ascii= 0":2
  buffer= b'}'
  ascii= }
  </code></pre>
  
  <p>Also, when I use <code>read()</code> instead of <code>readline()</code>, I get the same behavior:</p>
  
  <pre><code>buffer= b'{'
  data_str= {
  buffer= b'"'
  data_str= "
  buffer= b'3'
  data_str= 3
  buffer= b'"'
  data_str= "
  buffer= b':'
  data_str= :
  buffer= b'1'
  data_str= 1
  buffer= b'}'
  data_str= }
  </code></pre>
  
  <p>I even tried using <a href="https://raw.githubusercontent.com/adamwtow/python-serial-monitor/master/python-serial-monitor.py" rel="nofollow noreferrer">another code</a> that uses the same library, but got the same issue.</p>
  
  <p>I'm not sure why I'm encountering such behavior.</p>