Author Topic: Chinese USB to serial adapters - No proper Parity support on Linux  (Read 4959 times)

0 Members and 1 Guest are viewing this topic.

Offline miceuzTopic starter

  • Frequent Contributor
  • **
  • Posts: 387
  • Country: lt
    • chirp - a soil moisture meter / plant watering alarm
Here goes a story of frustration...

I'm developing a Modbus over RS485 device. Modbus standard says that default configuration of the serial port must be 19200 baud, 8 bits, *Even parity*, 1 stop bit.

Fair enough, you usually never use this parity setting, but this time have to enable it. No problem, let's do it...

It just does not work. Why? Whatever setting I set, there is no parity bit generated. WTF? Ok, I finally get that my ust-to-rs485 dongle is CH340 based and Linux driver still does not support parity setting for it while there is a patch available for over a year. Well, typical Linux - takes time and effort.

Ok, i switch to another dongle - Silabs CP2102 based "6 in 1" off the ebay. Woo hoo - the parity bit gets generated! ...It's ...just ...wrong... If Even parity is set, it's always high, if Odd parity - always low, no matter the number of bits in the byte. Seems that it's implemented as if it's Space and Mark parity.  :scared:

Now I'm just rummaging through my stock of usb to serial dongles and trying everyone - Prolific PL2303 -- the same results, FTDI FT232 (most probably fake) -- the same results!

I grab a genuine Arduino with atmega8u as usb to serial converter - AH!, it works. I grab a chinese clone with the same atmega8u - it also works properly!

I wander if it's Linux drivers for 3 different chips that are broken or if there is the same broken state machine in all of those seemingly different chips?

I don't have a windows machine to test those and would appreciate if someone with access to one could grab a bunch of cheap dongles and run some tests to see if Parity bit is set properly when connected to Windows?

... it seems for now I will have to release a not quite Modbus compatible device, just because this shitty situation and because I want my users to have an easy access to it... doh! :palm: :palm: :palm: 

Offline meeder

  • Regular Contributor
  • *
  • Posts: 219
  • Country: nl
Re: Chinese USB to serial adapters - No proper Parity support on Linux
« Reply #1 on: May 25, 2017, 02:53:04 pm »
I have used this converter in Windows for a couple of years now to communicate with Modbus enabled proces instrumentation and it has never failed me in Windows.
I haven't tried this adapter with Linux though.
As you can see it is based on a FTDI chip.
 

Offline meeder

  • Regular Contributor
  • *
  • Posts: 219
  • Country: nl
Re: Chinese USB to serial adapters - No proper Parity support on Linux
« Reply #2 on: May 25, 2017, 03:00:08 pm »
The thing with Modbus is that the standard is a bit to flexible.
If you look what they write about parity they require even parity but you should support odd or none as well...

"Even parity is required, other modes ( odd parity, no parity ) may also be used.  In order to ensure a maximum compatibility with other products, it is recommended to support also No parity mode.  The default parity mode must be even parity. Remark : the use of no parity requires 2 stop bits.  "
 

Offline komet

  • Supporter
  • ****
  • Posts: 155
  • Country: ch
  • Shenzhen Retroencabulator Mfg. Co.
Re: Chinese USB to serial adapters - No proper Parity support on Linux
« Reply #3 on: May 25, 2017, 03:06:46 pm »
What Linux kernel version?

It's certainly quite plausible for multiple serial drivers to have the same bug because they were probably derived from each other.

Anyway, parity support for CH341 appears to have been implemented here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/usb/serial/ch341.c?h=v4.12-rc2&id=ba781bdf86621b71aa79a1ac0ad584f1e8aac307 so make sure that code is in your kernel.
 

Offline miceuzTopic starter

  • Frequent Contributor
  • **
  • Posts: 387
  • Country: lt
    • chirp - a soil moisture meter / plant watering alarm
Re: Chinese USB to serial adapters - No proper Parity support on Linux
« Reply #4 on: May 25, 2017, 03:12:28 pm »
That was what I was working on - use Even parity by default and allow soft configuration to change to different settings. But that will not be the case now, ecosystem steers us away from standards.

Besides I have seen some frustrated users that were raving about when "some smart ass" manufactures a proper Modbus device 19200/8-E-1 and they have to switch master to 19200/8-E-1 just to reconfigure it to use 9600/8-N-1 like all the rest cheap chinese devices on the bus...

On another hand, parity is redundant as crc16 takes care of data integrity.

Offline miceuzTopic starter

  • Frequent Contributor
  • **
  • Posts: 387
  • Country: lt
    • chirp - a soil moisture meter / plant watering alarm
Re: Chinese USB to serial adapters - No proper Parity support on Linux
« Reply #5 on: May 25, 2017, 03:16:23 pm »
What Linux kernel version?

It's certainly quite plausible for multiple serial drivers to have the same bug because they were probably derived from each other.

Anyway, parity support for CH341 appears to have been implemented here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/usb/serial/ch341.c?h=v4.12-rc2&id=ba781bdf86621b71aa79a1ac0ad584f1e8aac307 so make sure that code is in your kernel.

My kernel is 4.8.0-46-generic

Thanks @komet, I know about this patch. I could compile it no problem, been there done that, but that is besides the point - I'm thinking about customers who will be using my product - they will want an out of the box experience using cheap accessible stuff - usb-to-rs485 dongle on raspberry pi most probably.

I'm developing this soil moisture sensor btw: https://github.com/Miceuz/rs485-moist-sensor

Offline Monkeh

  • Super Contributor
  • ***
  • Posts: 7992
  • Country: gb
Re: Chinese USB to serial adapters - No proper Parity support on Linux
« Reply #6 on: May 25, 2017, 03:44:28 pm »
Advise your users not to use an old distro.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf