title: Android UsbSerial library problem in ch340
body: <p>I am trying to convert the open-source python project <a href="https://github.com/espressif/esptool" rel="nofollow noreferrer">esptool.py</a> to the Android(java) project.<br />
  In android, for serial communication, I am using <a href="https://github.com/felHR85/UsbSerial" rel="nofollow noreferrer">USB Serial</a> library.<br />
  I need only <a href="https://en.wikipedia.org/wiki/ESP8266" rel="nofollow noreferrer">esp8266</a> feature from <code>esptoo.py</code>, so I only converted the esp8266 function.</p>
  <p>From <a href="https://github.com/felHR85/UsbSerial" rel="nofollow noreferrer">UsbSerial</a> library, I am using <a href="https://github.com/felHR85/UsbSerial/wiki/5.-Synchronous-api" rel="nofollow noreferrer">synchronous api</a>, because the <a href="https://github.com/espressif/esptool" rel="nofollow noreferrer">esptool.py</a> use synchronous command for Serial <a href="https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.read" rel="nofollow noreferrer">read</a>/<a href="https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.write" rel="nofollow noreferrer">write</a>.<br />
  The current project is working well with esp8266/FTDI, esp8266/cp2102 same as <code>esptool.py</code>.<br />
  My problem is not working with <code>esp8266/ch340</code>.<br />
  Of course, the esptool.py is working well with <code>esp8266/ch340</code>.<br />
  But, my android project is not working with it.<br />
  In android, the main command which used in esptool.py is <code>setDTR</code>, <code>setRTS</code>, <code>serial Read</code> and <code>serial Write</code>.
  Each command working well although in <code>esp8266/ch340</code> in normal mode(not bootloader mode).</p>
  <p>Also, I confirmed the esp8266 can be bootloader mode correctly via a combination of setDTR, setRTS.<br />
  More correctly confirm bootloader mode, I can use <code>Flash button</code> and <code>RST button</code> of esp8266.<br />
  <a href="https://i.stack.imgur.com/7zt5L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7zt5L.png" alt="enter image description here" /></a>
  With esp8266/ch340, I cannot pass the first step with an android project.
  The first step is the sync operation.</p>
  <p>From android, call write command with <a href="https://github.com/espressif/esptool/wiki/Serial-Protocol#initial-synchronisation" rel="nofollow noreferrer">https://github.com/espressif/esptool/wiki/Serial-Protocol#initial-synchronisation</a>, then the esp8266 is a response as bytes.<br />
  The main problem is cannot read response command from esp8266.<br />
  From UsbSerial, there are <code>syncRead</code>, <code>syncWrite</code> commands, also there is an asynchronous command for serial operation.<br />
  The <code>esptool.py</code> is made with synchronous API, So, I am using synchronous API in android project.<br />
  Also, the current project is perfect with esp8266/FTDI, esp8266/cp2102.
  So, I think, the main algorithm is successfully converted from esptool.py</p>
  <p>But, esp8266/ch340, the current project is not working.<br />
  From debug, I confirmed that I cannot read any bytes from esp8266/ch340 in bootloader mode.<br />
  For debug, I created new project using <a href="https://github.com/felHR85/UsbSerial/wiki/4.-Asynchronous-api" rel="nofollow noreferrer">asynchronous api</a>.</p>
  <p>With using the button of esp8266, make <code>esp8266/ch340</code> bootloader mode, I am writing the sync command to esp8266.<br />
  <a href="https://i.stack.imgur.com/gjUXv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gjUXv.png" alt="flash button, rst button" /></a></p>
  <p>From <a href="https://github.com/espressif/esptool/wiki/Serial-Protocol" rel="nofollow noreferrer">https://github.com/espressif/esptool/wiki/Serial-Protocol</a>, the esp8266 is sending response command, but cannot read the response in <code>esp8266/ch340</code>.</p>
  <p><em>But, I found strange things.</em><br />
  After bootloader mode, write sync command and disconnect the USB from android, then, I can see the correct response command in <code>Disconnecting moment</code>.<br />
  If I write sync command 3 times, then the 3 response command arrives at disconnecting moment.<br />
  <strong>How I can solve this problem?</strong></p>
  <p>How I can debug esp8266 in bootloader mode?</p>
  <p>From debugging, <a href="https://stackoverflow.com/questions/12781807">this</a> is main problem in <code>esp8266/ch340</code>(<a href="https://github.com/espressif/esptool/wiki/ESP32-Boot-Mode-Selection" rel="nofollow noreferrer">bootloader mode</a>).</p>