title: Connecting RS232 to Serial and establishing a connection to Nextion display from a python script
body: <p>I was trying to establish a serial communication between my python script and Nextion display.
  I want to create a custom firmware uploader, so I could use it instead of Nextion Editor. </p>
  
  <p>Unfortunately I can't establish a connection to the screen(RS232 to Serial is connected properly and detected, I use default 9600 baud rate).
  After sending</p>
  
  <pre><code>  DRAKJHSUYDGBNCJHGJKSHBDNÿÿÿ
    connectÿÿÿ
    ÿÿconnectÿÿÿ
  
  </code></pre>
  
  <p>It should respond properly...
  I found a document from Nextion that explains the Upload Protocol: <a href="https://nextion.tech/2017/12/08/nextion-hmi-upload-protocol-v1-1/" rel="nofollow noreferrer">https://nextion.tech/2017/12/08/nextion-hmi-upload-protocol-v1-1/</a></p>
  
  <p>And here is my script, that produces from time to time</p>
  
  <pre><code>b'$\xff\xff\xff'
  </code></pre>
  
  <pre><code>import serial
  from time import sleep
  ser = serial.Serial('/dev/tty.usbserial-A94RJPXT',9600,timeout=0)
  ser.write(str.encode("  DRAKJHSUYDGBNCJHGJKSHBDNÿÿÿ"))
  ser.write(str.encode("\n"))
  ser.write(str.encode("  connectÿÿÿ"))
  ser.write(str.encode("\n"))
  ser.write(str.encode("  ÿÿconnectÿÿÿ"))
  ser.write(str.encode("\n"))
  
  while True:
          print(ser.readline())
          sleep(0.1)
  </code></pre>
  
  <p>WORKING CODE:</p>
  
  <pre><code>import serial
  from time import sleep
  ser = serial.Serial('/dev/tty.usbserial-A94RJPXT',9600,timeout=0)
  
  ser.write("DRAKJHSUYDGBNCJHGJKSHBDNÿÿÿ".encode('iso-8859-1'))
  ser.write("connectÿÿÿ".encode('iso-8859-1'))  # could try some other encodings
  ser.write("ÿÿconnectÿÿÿ".encode('iso-8859-1'))
  
  while True:
          data=ser.readline().decode('iso-8859-1')
          if data !="":
                  print(data)
          sleep(0.1)
  
  </code></pre>