promod
Posts: 39
Joined: Tue Nov 13, 2012 6:47 pm

CH340

Tue Jan 03, 2017 2:08 pm

Hi friends. I have read a lot of articles about not working driver for chipset CH340/ CH 341 in raspbian. It is used in USB to serial convetrors. In millions of convertors. I try two of them and they realy not works. Isn't it possible to give good driver to raspbian?

User avatar
scruss
Posts: 2752
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: CH340

Tue Jan 03, 2017 4:55 pm

Please give more details of what "not working" means to you. I've been successfully using CH340-based equipment with Raspberry Pi for months.

Doing serial communications on Linux is much harder than it should be, so there's a chance you're missing a detail to make your project work.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

promod
Posts: 39
Joined: Tue Nov 13, 2012 6:47 pm

Re: CH340

Wed Jan 04, 2017 9:36 am

Hi - Thank you for your interest.
I want to send something over RS232 ( RS485 too ) but If I use USB to serial converter with chipset profilic logic pl2302 or FTDI FT232RL communication works properly. But If I use convereter with CH341 ( or CH340 ) detected by operating system raspbian everything changes. I send some bytes and I receive something other. I found out on some forums, that in some settings of serial parametters converters doesn't work properly. So and my parameters 4800 Boud, 7 Bit, Even partty and one stop bit are probably the wrong settings - I can't change settings on second side. There are some advices on forums, but nothing works for me. Convertors are working - I tryed them on Windows applications.

User avatar
scruss
Posts: 2752
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: CH340

Wed Jan 04, 2017 4:15 pm

Hmm, 7E1 is an unusual setting. Sure you're not needing 7E2? 4800 bps is also rarely used for anything other than GPS, and I haven't yet met a serial GPS that wasn't 8N1.

What do you get from

Code: Select all

stty -F /dev/ttyUSB0
(assuming that /dev/ttyUSB0 is your CH340 serial device)

Sometimes I find it useful to clear out all the settings on a serial port before use. I use this command:

Code: Select all

sudo stty -F /dev/ttyUSB0 0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
Are you a member of the dialout group? It makes serial access permissions a bit easier for users.

Forum advice about the Raspberry Pi — especially anything more than a few months old — will likely be out of date. Drivers change and become more stable. Advice that I needed to follow in 2013 for using a Prolific PL2303 is now obsolete and likely makes things less reliable. Are you running the latest release of Raspbian/PIXEL with all of the updates?

Can you connect and change parameters using the minicom terminal package? I find it helpful for debussing.

Serial connections can be frustrating. Personally, I'm having no problems with a CH340-based Arduino Mega clone driving my 3D printer, all managed by a Raspberry Pi 2 running OctoPrint.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

promod
Posts: 39
Joined: Tue Nov 13, 2012 6:47 pm

Re: CH340

Tue Jan 17, 2017 5:10 pm

Hi friend. I tried to test your advices - stty command was new for me. I use serial port from C language and settings for communication are the same for each type of converter - pl2302, FT232RL and of course CH340. But your idea - to check it with minicom - was very good for me. I connect raspberry with converter with minicom application with windows PC. Than I could see what is on wires. I press ''a'' and I get ''a'' but if I press ''f" I get "F". Everytime. And if I send chars less than 20 I get other chars. This eliminated my application (minicom) and I am sure now, that driver for CH340 is realy wrong. Your HW is probably other so you get success resuts. But I tested 3 or 4 devices with CH340 or CH341 and result is the same - driver in raspbian is wrong. Communication over this type converter is not possible even though kernel detects HW and everything looks like on other type of chipsets. If I have free time I try to test this on Debian. HW is good - windows applications work properly.

User avatar
scruss
Posts: 2752
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: CH340

Tue Jan 17, 2017 7:16 pm

This could be a hardware problem, but unlikely. Please show your stty -F output, though, and also what you get from uname -a and lsmod. All of these could critically affect your results. Otherwise, no-one can help you fix this, and I certainly won't spend any more time on this until you do. Facts are good, vague allegations bad.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

promod
Posts: 39
Joined: Tue Nov 13, 2012 6:47 pm

Re: CH340

Thu Jan 26, 2017 1:11 pm

More details - here you are.
After connecting converter syslog look like this:

usb 1-1.2.4: New USB device found, idVendor=1a86, idProduct=7523
usb 1-1.2.4: New USB device strings: Mfr=0, Product=2, SerialNumber=0
usb 1-1.2.4: Product: USB2.0-Serial
usbcore: registered new interface driver usbserial
usbcore: registered new interface driver usbserial_generic
usbserial: USB Serial support registered for generic
usbcore: registered new interface driver ch341
usbserial: USB Serial support registered for ch341-uart
ch341 1-1.2.4:1.0: ch341-uart converter detected
usb 1-1.2.4: ch341-uart converter now attached to ttyUSB0

uname -a shows this :
Linux vyvoj 4.1.19-v7+ #858 SMP Tue Mar 15 15:56:00 GMT 2016 armv7l GNU/Linux

stty -F /dev/ttyUSB0 shows this :
speed 9600 baud; line = 0;
-brkint -imaxbel

slmod shows this:
Module Size Used by
ch341 4732 0
usbserial 21779 1 ch341
snd_bcm2835 19802 0
snd_pcm 73474 1 snd_bcm2835
snd_seq 52634 0
snd_seq_device 3445 1 snd_seq
snd_timer 18848 2 snd_pcm,snd_seq
snd 50779 5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device
evdev 10278 6
w1_therm 3396 0
bcm2835_rng 1763 0
bcm2835_gpiomem 2860 0
spi_bcm2835 7074 0
w1_gpio 3401 0
wire 24703 2 w1_gpio,w1_therm
cn 4258 1 wire
uio_pdrv_genirq 2944 0
uio 7753 1 uio_pdrv_genirq

All look good - very similar to other device ( for example pl2303 converter ).

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24683
Joined: Sat Jul 30, 2011 7:41 pm

Re: CH340

Thu Jan 26, 2017 1:18 pm

Do you have a voltage mismatch somewhere? RS232 is not the same voltage as some UART's for example, and that can give weird results.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I own the world’s worst thesaurus. Not only is it awful, it’s awful."

User avatar
scruss
Posts: 2752
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: CH340

Thu Jan 26, 2017 1:29 pm

promod wrote:uname -a shows this :
Linux vyvoj 4.1.19-v7+ #858 SMP Tue Mar 15 15:56:00 GMT 2016 armv7l GNU/Linux
When did you last update the kernel? It should be 4.4.38 or so for armv7l
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

promod
Posts: 39
Joined: Tue Nov 13, 2012 6:47 pm

Re: CH340

Fri Jan 27, 2017 8:31 am

Hi.
You are right - this is my development device and I have wheezy distribution there. But I installed an actual version of raspberry image and kernel is 4.4.38 as you wrote. Result is the same - CH34x converters are not working.
But it is easier to see, more than a thousand words. May I let to send you one converter what I am speaking about?
I try this one : https://www.aliexpress.com/snapshot/0.h ... 2595624775.
This is RS232 version. If you prefer RS485 version I try this one for example:
https://www.aliexpress.com/item/M89-Fre ... 0.0.ubTgw4
I spent hours and hours by testing these devices and I am angry that it is not working. I buy beer for you if you find out why it is not working. Or I drink some for your health. :)

promod
Posts: 39
Joined: Tue Nov 13, 2012 6:47 pm

Re: CH340

Fri Jan 27, 2017 8:38 am

For jamesh.

Thanks for your interest. I try the same HW for communication from Raspberry and from common Windows PC. If I try it from RSP it is not working - from PC yes. If I use other converter ( with other chipset ) it is working too. Only CH341 or CH340 chipsets are not working. I use 2A power source and I thing it is good. I use USB port of raspberry so I don't know to affect powerring of USB converter.


User avatar
scruss
Posts: 2752
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: CH340

Fri Jan 27, 2017 6:25 pm

Hi promod — I know that it's very frustrating when hardware that should work, doesn't work.

I don't have exactly that AliExpress New USB 2.0 to RS232 Serial Converter 9 Pin Adapter for Win7/8 Wholesale converter to test like-for-like with you. If it's really as pictured, its resin-encapsulated chip could be anything. After the fake FTDI FT232RL issue, it might not even be a real CH340: people will fake anything they think that there's a market for. (Also, I don't see any room for charge pump capacitors on that AliExpress adapter, so its chances of putting out real RS-232 signals are slim.)

Ordering one of those adapters would take a very long time (it's New Year for AliExpress), so I'm not sure if I could exactly replicate your results. The CH340 devices I do have, though, are:
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

promod
Posts: 39
Joined: Tue Nov 13, 2012 6:47 pm

Re: CH340

Sat Jan 28, 2017 5:18 pm

I think that all mentioned converters are fake ( the price is too low ) - but each one is working on Windows environment. So I think that driver in raspbian can not operate each chipset ( fake ) clone. Any bytes are transmitting but I receive something else that I send. So I buy one piece for test and if it is working I use this type. Do you know any site with tested HW for raspberry? Maybe it is good idea to create one if not.

User avatar
scruss
Posts: 2752
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: CH340

Sun Jan 29, 2017 4:30 am

This used to have lots of tests: http://elinux.org/RPi_Hub

I don't know what to suggest: the CH340-based devices I have work as expected. If you need this to work, and you value your time, buy real FTDI cables.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

orcunbaslak
Posts: 12
Joined: Wed Mar 29, 2017 8:41 pm

Re: CH340

Wed Mar 29, 2017 9:02 pm

Hello,

I have the same issue as well. I'm trying to access a device via ModBus RTU.
I'm using QinHeng Electronics HL-340 USB-Serial adapter.

The adapter works fine under Windows.

Code: Select all

[288977.314445] usb 1-1.2: new full-speed USB device number 14 using dwc_otg
[288977.417589] usb 1-1.2: New USB device found, idVendor=1a86, idProduct=7523
[288977.417603] usb 1-1.2: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[288977.417610] usb 1-1.2: Product: USB2.0-Serial
[288977.418770] ch341 1-1.2:1.0: ch341-uart converter detected
[288977.422592] usb 1-1.2: ch341-uart converter now attached to ttyUSB0
[289033.671903] w1_master_driver w1_bus_master1: Family 0 for 00.72e800000000.47 is not registered.
[email protected]:~ $ lsusb
Bus 001 Device 004: ID 0bc2:3312 Seagate RSS LLC
Bus 001 Device 014: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
[email protected]:~ $
and my tty output is

Code: Select all

[email protected]:~ $ sudo stty -F /dev/ttyUSB0
speed 9600 baud; line = 0;
intr = <undef>; quit = <undef>; erase = <undef>; kill = <undef>; eof = <undef>; start = <undef>; stop = <undef>; susp = <undef>; rprnt = <undef>;
werase = <undef>; lnext = <undef>; flush = <undef>; min = 0; time = 0;
-brkint -icrnl -imaxbel
-opost -onlcr
-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke
My python code is as follows.

Code: Select all

import minimalmodbus

mb = minimalmodbus
mb.BAUDRATE = 9600
mb.PARITY = 'O'
mb.BYTESIZE = 8
mb.STOPBITS = 1
mb.TIMEOUT = 1.0

DirisA10 = mb.Instrument(port='/dev/ttyUSB0', slaveaddress=1, mode='rtu')
DirisA10.debug = True

Address = 50520
Sub_Address = 40000

print DirisA10.read_register(registeraddress=2822,numberOfDecimals=2, functioncode=3, signed=False)
Which in return gives me

Code: Select all

MinimalModbus debug mode. Writing to instrument (expecting 7 bytes back): '\x01\x03\x0b\x06\x00\x01f/' (01 03 0B 06 00 01 66 2F)
MinimalModbus debug mode. No sleep required before write. Time since previous read: 1490820559858.6 ms, minimum silent period: 4.01 ms.
MinimalModbus debug mode. Response from instrument: '' () (0 bytes), roundtrip time: 1001.2 ms. Timeout setting: 1000.0 ms.
However the same code under windows (I subs COM3 instead of /dev/ttyUSB0) gives

Code: Select all

C:\Python27>python.exe serial.txt

MinimalModbus debug mode. Writing to instrument (expecting 7 bytes back): '\x01\x03\x0b\x06\x00\x01f/' (01 03 0B 06 00 01 66 2F)
MinimalModbus debug mode. No sleep required before write. Time since previous read: 1490819882205.0 ms, minimum silent period: 4.01 ms.
MinimalModbus debug mode. Response from instrument: '\x01\x03\x02Q\xddE\x8d' (01 03 02 51 DD 45 8D) (7 bytes), roundtrip time: 46.0 ms. Timeout setting: 1000.0ms.

209.57
Any help about addressing the issue is appreciated. I'm unable to buy a different USB-RS485 converter in Turkey atm so I'm left with this.

Br,

orcunbaslak
Posts: 12
Joined: Wed Mar 29, 2017 8:41 pm

Re: CH340

Wed Mar 29, 2017 10:17 pm

Ok, I fixed the issue.

Basically the problem is the driver is incomplete. It can not work under different setup such as mine (9600baud, Odd parity).

Instead; I changed my baudrate to 19.200 and parity to None.

Here's the setup I'm using.

Code: Select all

mb.BAUDRATE = 19200
mb.PARITY = 'N'
mb.BYTESIZE = 8
mb.STOPBITS = 1
mb.TIMEOUT = 1.0

dirisa10 = mb.Instrument(port='/dev/ttyUSB0', slaveaddress=1, mode='rtu')
Now it works as it should. Try this setup with the CH340 chip.

Br,

Danilk
Posts: 108
Joined: Wed Feb 01, 2017 7:47 pm
Location: Russia, St-Peterburg

Re: CH340

Wed Jun 07, 2017 10:06 am

is there "happy end" with raspbian+ch340+modbus+rs485?

kreisler
Posts: 23
Joined: Fri Mar 10, 2017 4:08 pm
Location: Germany

Re: CH340

Mon Jun 26, 2017 11:42 am

I read everything but didn't understand everything, also confusing, not my bad. So which SKU (AX or DX or BG) works great with raspi, and which are the exact instructions for Jessie driver installation? Will there be SKU plug'n play under Stretch?

SteveSpencer
Posts: 351
Joined: Thu Mar 28, 2013 9:19 am
Location: Nottingham, UK

Re: CH340

Mon Jun 26, 2017 2:51 pm

I have a bunch of Wemos D1 mini and mini Pro boards (ESP8266), which identify as CH341 for the USB/serial side.
I found that when uploading code to them, I could not reliably get them to work at a speed higher than 9600, although on a couple, 19,200 worked. When you're uploading around 64KB, this is a bit of a pain. The same boards work at higher speeds (38,400 and above) on Windows 10, which is even more annoying...)

It may well be an issue in the driver. At some point, I will investigate more thoroughly what is in the ch340 code, but not right now.
Steve S
No, I can't think of anything funny that won't offend someone if they want it to...

kreisler
Posts: 23
Joined: Fri Mar 10, 2017 4:08 pm
Location: Germany

Re: CH340

Mon Jun 26, 2017 4:38 pm

CH340 might not be the recommended way to go after all. I have no idea what i am talking about, but after googling for 1 day (morning+afternoon) and finding lots of half-ar*ed posts on blogs and forums on this and related topics, and some/many of them seemingly outdated, referring to old knowledge old raspi firmware old raspi OS old raspi kernel etc, i don't feel encouraged at all to buy that blue cable product (based on CH340 or variants) —which of course works fine on Windows machines!— and eventually find out that i get stuck with making it work for my raspi application. I am a total beginner, and i cannot be the guinea pig! :P

I have 1 confirmation that USB-to-serial based on FDTI does work for my specific application (raspi logging a multimeter which requires a serial data cable connection). Even though i did not read the PDF nor some driver webpage, it seems that FDTI is the more promising way to go.

At this point i am also looking at RS232 Shields. Might be even more recommendable way to go.

So from now on, i'll check out FDTI solutions and the RS232 Shield solution and hopefully get my raspi application going. Then i won't comment back in this thread but move on.

User avatar
scruss
Posts: 2752
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: CH340

Tue Jun 27, 2017 5:05 pm

The situation seems to be slightly user-specific, but even reputable companies like Adafruit are shipping CH340-based hardware. I haven't had significant problems with mine (including Wemos D1s) on Raspberry Pi.

Try the FTDI cable if you want.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

kole5o
Posts: 1
Joined: Tue Aug 15, 2017 10:54 am

Re: CH340

Tue Aug 15, 2017 10:56 am

make ton of search
raspbian jessie 4.4.9 work ok
https://github.com/kole5o/raspberry-pi3

promod
Posts: 39
Joined: Tue Nov 13, 2012 6:47 pm

Re: CH340

Wed Jan 24, 2018 7:44 am

Hi friends.

I try all converters now with newest raspbian and all works for me. It was realy in raspbian - I do not any change in applications. Great thanks to raspbian programmers.

kwasmich
Posts: 7
Joined: Tue Sep 19, 2017 5:57 pm

Re: CH340

Sat Jan 27, 2018 6:01 pm

While this is true for the Raspberry Pi itself, it still does not work on the official DEBIAN STRETCH WITH RASPBERRY PI DESKTOP when run on an ordinary PC.

Return to “Raspbian”