avrdudes / avrdude

AVRDUDE is a utility to program AVR microcontrollers
GNU General Public License v2.0
747 stars 138 forks source link

WCH CH340 serial port issue under Windows: can't set com-state for "\\.\COMx #1328

Closed mcuee closed 1 year ago

mcuee commented 1 year ago

I think this is not an avrdude issue but an WCH CH340 driver issue.

Still it is good to put some info here so that people can reference to it, since there are quite some similar issues posted online.

I was playing with CH341A yesterday and install the latest CH341SER driver for CH340/CH341. Then I was encountering the above issue. Reverting to an old version sorted out the issue for me -- I am using version 3.5 (3.5.2019.1) and it is working fine.

Problematic driver version: http://www.wch-ic.com/downloads/CH341SER_EXE.html (version 3.8, uploaded on 2023-03-16, size 642KB)

; CH341SER.INF
; Driver for CH340/CH341 (USB=>SERIAL chip) V3.8
; WDM&VXD for Windows 98/Me/2000/XP/Vista/7/8/8.1/10/11/SERVER 2003/2008/2012/2016/2019/2022
; Copyright (C) WCH 2001-2023
;

[Version]
Signature   = "$Chicago$"
Class       = Ports
ClassGuid   = {4D36E978-E325-11CE-BFC1-08002BE10318}
Provider    = %WinChipHead%
DriverVer   = 02/11/2023, 3.8.2023.02
CatalogFile =CH341SER.CAT

Working driver version:

; CH341SER.INF
; Driver for CH340/CH341 (USB=>SERIAL chip) V3.5
; WDM&VXD for Windows 98/Me/2000/XP/Vista/7/8/8.1/10/SERVER 2003/2008/2012/2016
; Copyright (C) W.ch 2001-2019
;

[Version]
Signature   = "$Chicago$"
Class       = Ports
ClassGuid   = {4D36E978-E325-11CE-BFC1-08002BE10318}
Provider    = %WinChipHead%
DriverVer   = 01/30/2019, 3.5.2019.1
CatalogFile = CH341SER.CAT

If you can not find the older driver version, here is one link for version 3.5. https://github.com/wemos/ch340_driver

Good and bad run log for reference:

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v1 -C .\avrdude_pr1215v1.conf -p m328pb -c urclock -P COM4 -xshowall -xbootsize=512

avrdude_pr1215v1: AVR device initialized and ready to accept instructions
0 0000-00-00 00.00  application 0 store 0 meta 0 boot 512 o8.0 -?s-?-r-- vector 0 (RESET) ATmega328PB
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v1 -C .\avrdude_pr1215v1.conf -p m328pb -c arduino -P COM4

avrdude_pr1215v1: AVR device initialized and ready to accept instructions
avrdude_pr1215v1: device signature = 0x1e9516 (probably m328pb)

avrdude_pr1215v1 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v1 -C .\avrdude_pr1215v1.conf -p m328pb -c arduino -P COM4
avrdude_pr1215v1 error: cannot set com-state for \\.\COM4
avrdude_pr1215v1 error: unable to open programmer arduino on port COM4

avrdude_pr1215v1 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v1 -C .\avrdude_pr1215v1.conf -p m328pb -c urclock -P COM4 -xshowall -xbootsize=512
avrdude_pr1215v1 error: cannot set com-state for \\.\COM4
avrdude_pr1215v1 error: unable to open programmer urclock on port COM4

avrdude_pr1215v1 done.  Thank you.
mcuee commented 1 year ago

Similar report: https://arduino.stackexchange.com/questions/55301/avrdude-ser-open-cant-set-com-state https://www.reddit.com/r/arduino/comments/jn51gf/really_weird_problem/

mcuee commented 1 year ago

The following is a different issue but with the same symptom. It is not using CH340 but the original ATmega16U2 USB to Serial FW. https://forum.arduino.cc/t/ser_open-can-t-set-com-state-for-com4-changes-uno-not-detected-solved/659323/8

mcuee commented 1 year ago

I found a solution for using the latest version 3.8 driver (CH341SER 3.8 version). I need to click the check box in the advanced settings --> Enabling the Serial Port Enumerator (SerEnum). There are no checkboxes for the version 3.5 driver.

Screenshot 2023-03-25 182254

Capture_serenum

Before enabling the option and after enabling the option. I have no idea why WCH does not make this option default.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v1 -C .\avrdude_pr1215v1.conf -p m328pb -c arduino -P COM4
avrdude_pr1215v1 error: cannot set com-state for \\.\COM4
avrdude_pr1215v1 error: unable to open programmer arduino on port COM4

avrdude_pr1215v1 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v1 -C .\avrdude_pr1215v1.conf -p m328pb -c arduino -P COM4

avrdude_pr1215v1: AVR device initialized and ready to accept instructions
avrdude_pr1215v1: device signature = 0x1e9516 (probably m328pb)

avrdude_pr1215v1 done.  Thank you.
mcuee commented 1 year ago

Somehow this is dependent on the PC. I do not need to enable serenum to get the thing working on my other laptop running Windows 10.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c urclock -P COM4 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
0 0000-00-00 00.00  application 0 store 0 meta 0 boot 512 o4.4 --s-h-r-- vector 0 (RESET) ATmega328P

Capture_Driver_No_SerEnum

mcuee commented 1 year ago

I have to use older version of the driver to work reliably in the end. The new version just does not work properly.

MCUdude commented 1 year ago

In case someone else are having the same issue, what driver version are you using (that works), and where did you download it from?

per1234 commented 1 year ago

Since I was not able to find a way to source an older version of the driver directly from the WCH website, I am recommending sourcing it from a SparkFun tutorial since I consider SparkFun to be a trustworthy source. The instructions are here:

https://forum.arduino.cc/t/arduino-nano-upload-error/1146296/14

mcuee commented 1 year ago

Yes, SparkFun version (WCH driver version 3.4) works fine.

mcuee commented 1 year ago

I will carry out a bit more tests to see which version does not work and which versions work. I believe all the older versions (3.7 and earlier) work fine except latest version 3.8 but I need to double check.

Version 3.8 does not work reliably (my test shows problem only in Windows 11 but not Windows 10). https://wch-ic.com/downloads/CH341SER_ZIP.html https://wch-ic.com/downloads/CH341SER_EXE.html

The scope of application | version | upload time | size -- | -- | -- | -- CH340G,CH340T,CH340C,CH340E,CH340B,CH341A,CH341T,CH341B,CH341C,CH341U | 3.8 | 2023-03-16 | 642KB

CH341SER_v38.ZIP

mcuee commented 1 year ago

Another reliable place to download is from Microsoft. They have version 3.8.2023.2 (problem version), version 3.5.2019.1 and version 3.4.2014.08 (both are working). There is no driver installer included but you can use Windows Device Manager to install the driver. https://www.catalog.update.microsoft.com/Search.aspx?q=CH340 wch340_v34.zip wch340_v35.zip wch340_v38.zip

mcuee commented 1 year ago

Interestingly I do not seem to have issues using serialupdi with latest driver.