title: Pyserial read bytes return garbage values from serial port
body: <p>in my python programme , I am using PySerial library to read the data out as bytes from serial port. But most of the bytes are garbage values. I am giving the source as well as out come of the read bytes using this python module.</p>
  
  <p>So can some please let me know (correct configuration settings) to read the correct values from the serial port using PySerial library?  If not do we have any other serial library in Python where I can read the bytes without above issue.</p>
  
  <pre><code>import serial
  import sys
  import _thread
  import serial.tools.list_ports
  import threading
  
  
  class SerialPort:
      ReceiveCallback: None
  
      def __init__(self):
          self.comportName = ""
          self.baud = 0
          self.timeout = None
          self.ReceiveCallback = None
          self.isopen = False
          self.receivedMessage = None
          self.serialport = serial.Serial()
  
      def __del__(self):
          try:
              if self.serialport.is_open():
                  self.serialport.close()
          except:
              print("Destructor error closing COM port: ", sys.exc_info()[0])
  
      def RegisterReceiveCallback(self, aReceiveCallback):
          self.ReceiveCallback = aReceiveCallback
          try:
              ListnerThread = threading.Thread(target=self.SerialReadBytesThread)
              ListnerThread.start()
          except:
              print("Error starting Read thread: ", sys.exc_info()[0])
  
      def SerialReadlineThread(self):
          while True:
              try:
                  if self.isopen:
                      self.receivedMessage = self.serialport.readline()
                      if self.receivedMessage != "":
                          self.ReceiveCallback(self.receivedMessage)
              except:
                  print("Error reading COM port: ", sys.exc_info()[0])
  
      def SerialReadBytesThread(self):
          while True:
              try:
                  if self.isopen:
                      size = self.serialport.inWaiting()
                      if size &gt; 0:
                          readBytes = self.serialport.read(size)
                          self.receivedMessage = bytearray(readBytes)
                          if self.receivedMessage != b'':
                              self.ReceiveCallback(self.receivedMessage)
              except serial.SerialException as e:
                  print("Error reading com port:", e.strerror)
  
      def IsOpen(self):
          return self.isopen
  
      def Open(self, portname, baudrate, timeout=0):
          if not self.isopen:
              # serialPort = 'portname', baudrate, bytesize = 8, parity = 'N', stopbits = 1, timeout = None, xonxoff = 0, rtscts = 0)
              self.serialport.port = portname
              self.serialport.baudrate = baudrate
              self.serialport.timeout = timeout
              try:
                  self.serialport.open()
                  self.isopen = True
                  self.serialport.flushInput()
              except:
                  print("Error opening COM port: ", sys.exc_info()[0])
  
      def Close(self):
          if self.isopen:
              try:
                  self.serialport.close()
                  self.isopen = False
              except:
                  print("Close error closing COM port: ", sys.exc_info()[0])
  
      def Send(self, message):
          if self.isopen:
              try:
                  # Ensure that the end of the message has both \r and \n, not just one or the other
                  newmessage = message.strip()
                  newmessage += '\r\n'
                  self.serialport.write(newmessage.encode('utf-8'))
              except:
                  print("Error sending message: ", sys.exc_info()[0])
              else:
                  return True
          else:
              return False
  
      def GetComPort(vendorProductID):
          portName = " "
          ports = serial.tools.list_ports.comports()
          for port, des, hwID in ports:
              if vendorProductID in hwID:
                  portName = port
                  return portName
          return portName
  
  
  if __name__ == '__main__':
      portName = SerialPort.GetComPort("VID:PID=0DB5:0180")
      comport = SerialPort()
      comport.Open(portName, 115200)
      t=threading.Thread(target=comport.SerialReadBytesThread)
      t.start()
      t.join()
  </code></pre>
  
  <p><strong>Outcome read data from the Python module:</strong></p>
  
  <p>b'\x02\x02\x0e\x01\x0b\x010\x02A\x01\x17 \x19\t%\x11\x00\x11\xe3\xb0\xc4B\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99o\xb9$\'\xaeA\xe4d\x9b\x93L\xa4\x95\x99\x1bxR\xb8U\x01\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x03\xff\xff\xff\x01\x00\xb28\x04\x80\x08\xc0\x8a\x02\x00\x00\x00\x00\x00\x00\x00\x030\x00\x00\x00\x00\x00\x00\x01\x00\t%\x11\x00\x11&lt;\x00i\x11\x00\x11\t%\x05rQ1909251100114Vx\x90               \txR"\x01S\x00\x00\x00\x01\x01A\x82\x02 \x00\x95\x05\x00\x00\x00\x00\x00\x9c\x01\x00_*\x02\tx_4\x01\x00\x9f\x02\x06\x00\x00\x00\x00\x01\x00\x9f\x03\x06\x00\x00\x00\x00\x00\x00\x9f\x10\x07\x06\r\n\x03\xa0 \x00\x9f\x1a\x02\x08@\x9f&amp;\x08\x93?]\x8cL\xb1\x1b\xd4\x9f\'\x01\x80\x9f6\x02\x00\xc2\x9f7\x04\xc3\xe8\xee\xf9\x9f9\x01\x07\x9ff\x04\xa0\x00\xc0\x00\x9fn\x04 p\x00\x00\xcf\x01\x04W\x13I)Ew\x89\x17 \x06\xd2\x102\x01\x00\x13\x10\x00\x00\x00\x1f\x9a\x03\x19\t%\xdf\xdf#\x02\x08@\xdf\xdf\'\x01\x00\x00\x10\x92\xcf\xeb\x93\xb2\xe0\xfe\x00\x00,\x00$:\xd54\x14\x89\x96\x8b\t\xdb\xb9\xd4\xc9\xdb6}\x8e\x9eP\xc1?\x91a\xdd\x06~\xc8\xca\x85\xd8^\xa7\xda\xa9\x03\x03'</p>
  
  <p><strong>Expected value/Actual value:</strong> ( I did get this value from serial port snipping tool while running my python module)</p>
  
  <p>02 02 0E 01 0B 01 30 02 41 01 17 20 19 09 25 11 02 10 E3 B0 C4 42 98 FC 1C 14  9A FB F4 C8 99 6F B9 24 27 AE 41 E4 64 9B 93 4C A4 95 99 1B 78 52 B8 55 01 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF<br>
  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 03 FF FF FF 01 00 B2 38 04 80 08 C0 8A 02 00 00 00 00 00 00 00 03 30 00 00 00 00 00 00 01 00 09 25 11 02 10 3C 00 6A 11 02 10 09 25 05 72 51 31 39 30 39 32 35 31 31 30 32 31 30 34 56 78 90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 09 78 52 22 01 53 00 00 00 01 01 41 82  02 20 00 95 05 00 00 00 00 00 9C 01 00 5F 2A 02  09 78 5F 34 01 00 9F 02 06 00 00 00 00 01 00 9F 03 06 00 00 00 00 00 00 9F 10 07 06 0D 0A 03 A0 20 00 9F 1A 02 08 40 9F 26 08 EB 12 3F BB 4A 81  AB 37 9F 27 01 80 9F 36 02 00 C3 9F 37 04 0E D2  3B E0 9F 39 01 07 9F 66 04 A0 00 C0 00 9F 6E 04  20 70 00 00 CF 01 04 57 13 49 29 45 77 89 17 20  06 D2 10 32 01 00 13 10 00 00 00 1F 9A 03 19 09 25 DF DF 23 02 08 40 DF DF 27 01 00 00 10 92 CF<br>
  EB 93 B2 E0 FE 00 00 2D 00 24 B8 CD E0 22 A4 03  6E 2D D3 96 9E 0C 39 3A 6F 9C 01 45 88 F2 AC AF F2 C1 07 67  3F 07 52 16 FF 3D 26 35 03                         </p>