title: Data from serial port comes in a vertical format
body: <p>I've a python script that is reading from a serial port (TXRX) using a barcode scanner, everything works well, but my issue when text comes in from the serial port comes in a vertical format, something like this:</p>
  
  <p>if the barcode that I am reading has 123456, it comes in my python script as:</p>
  
  <pre><code>1
  2
  3
  4
  5
  6
  </code></pre>
  
  <p>I've tried changing the print() options, but seems not have any luck.</p>
  
  <pre><code>import sys
  import serial
  
  ser = serial.Serial("/dev/ttyAMA0",115200,timeout=0.8)
  
  print('serial test start ...')
  if ser != None:
    print('serial ready...')
  else:
    print('serial not ready')
    sys.exit()
  
  ser.timerout=0.8 #read time out
  ser.writeTimeout = 0.8 #write time out.
  
  try:
    x = ""
    while True:
      t = ser.read()
      if t != b'':
        ser.write(t)
        x = x + t
        print(str(x)) #&lt;--this one shows what it reads,line by line.
  
  except KeyboardInterrupt:
    #print(str(x)) #&lt;--this work fine when I I terminate the loop.
    if ser != None:
      ser.close()
  </code></pre>
  
  <p>I would like the text that I am capturing to look like:</p>
  
  <pre><code>123456
  </code></pre>
  
  <p>After updating my code, if I add the:</p>
  
  <pre><code>try:
    x = ""
    while True:
      t = ser.read()
      if t != b'':
        ser.write(t)
        x = x + t
        print(str(x))
  
  except KeyboardInterrupt:
    if ser != None:
      ser.close()
  print(str(x))
  </code></pre>
  
  <p>I get this result: (I am reading is X001PB45ZF from a barcode)</p>
  
  <pre><code>X
  X0
  X00
  X001
  X001P
  X001PB
  X001PB4
  X001PB45
  X001PB45Z
  X001PB45ZF
  </code></pre>
  
  <p>If I add it outside the loop:</p>
  
  <pre><code>try:
    x = ""
    while True:
      t = ser.read()
      if t != b'':
        ser.write(t)
        x = x + t
  
  except KeyboardInterrupt:
    print(str(x))
    if ser != None:
      ser.close()
  </code></pre>
  
  <p>I get this result, but only when I terminate the program.</p>
  
  <pre><code>X001PB45ZF
  </code></pre>
  
  <p>I added this to my code inside the loop:</p>
  
  <pre><code>try:
      while True:
           t = ser.read()
          if t != b'':
              ser.write(t)
              print(repr(t)) 
  </code></pre>
  
  <p>and the output now looks like this:</p>
  
  <pre><code>'X'
  '0'
  '0'
  '1'
  'P'
  'B'
  '4'
  '5'
  'Z'
  'F'
  '\r'
  </code></pre>
  
  <p>now that I see the <code>\r</code> at the end,  I can terminate my loop, right? and capture the text as needed? I am still trying to figure out how to terminate the loop when <code>\r</code> is giving by the scanner...</p>
  
  <p>It worked now!!</p>
  
  <pre><code>try:
      x = ""
      # look for \r\n. if this doesn't work
      # try \x00
      while '\r' not in x:
          t = ser.read()
          if t != b'':
              ser.write(t)
              x = x + t
      x = x.strip() # removes whitepace from beginning and end of string
                    # including \r \n
      print(str(x)) 
  
  except KeyboardInterrupt:
      #print(str(x)) #&lt;--this work fine when I I terminate the loop.
      if ser != None:
          ser.close()
  </code></pre>
  
  <p>Now that I can capture the input on a single line, how can I add it to an infinite loop?</p>
  
  <p>My goal is to read the barcode, store it in a txt or DB. The barcode is in motion mode, meaning, as soon as the camera detects movement, the barcode will activate and try to read.</p>