Problem with pl2303 usb/485 adapter

93 views
Skip to first unread message

Marko Cebokli

unread,
Nov 4, 2017, 3:28:08 AM11/4/17
to libmodbus
Hello,

I am trying to run an Schneider altivar atv312 motor box from Opensuse 42.3 (linux 4.4.76-1-default #1 SMP), with libmodbus 3.1.4 compiled from source.

Dmesg reports the adapter as

[  913.493470] usb 1-6: new full-speed USB device number 8 using xhci_hcd
[  913.622119] usb 1-6: New USB device found, idVendor=067b, idProduct=2303
[  913.622122] usb 1-6: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[  913.622123] usb 1-6: Product: USB-Serial Controller
[  913.622124] usb 1-6: Manufacturer: Prolific Technology Inc.
[  913.622456] pl2303 1-6:1.0: pl2303 converter detected
[  913.623031] usb 1-6: pl2303 converter now attached to ttyUSB0

When I run my test program (pasted below) i get:

New OK
Opening /dev/ttyUSB0 at 19200 bauds (E, 8, 1)
Connect OK
set ser fail: -2
Set ser: Inappropriate ioctl for device
Ser mode = 0
Slave OK
Read regs 3201
[01][03][0C][81][00][01][D7][72]
Waiting for a confirmation...
ERROR Connection timed out: select
ERROR modbus_read_registers single (-1)
Address = 3201

I have modified the modbus_rtu_set_serial_mode() function in modbus-rtu.c to give different return values at different places and found that the error happens here:

        if (mode == MODBUS_RTU_RS485) {
            rs485conf.flags = SER_RS485_ENABLED;
            if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) {
                return -2;
            }

Any idea how I could fix this?

My test program is made from some examples I found and looks like this:

/*
 *testmodbus.c
 *
 *      MC okt 2017
 *
 * gcc testmodbus.c -o testmodbus `pkg-config --cflags --libs libmodbus`
 *
 */


#include <stdio.h>
#include <modbus.h>
#include <errno.h>

int main()
{
modbus_t *mb;
int er;

mb=modbus_new_rtu("/dev/ttyUSB0",19200,'E',8,1);
if (mb==NULL)
  {
  printf("new fail\n"); perror("New");
  }
else printf("New OK\n");

modbus_set_debug(mb,TRUE);


if (modbus_connect(mb) == -1)
  {
  fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  modbus_free(mb);
  return -1;
  }
else printf("Connect OK\n");


er=modbus_rtu_set_serial_mode(mb,MODBUS_RTU_RS485);
if (er<0)
  {
  printf("set ser fail: %d\n",er); perror("Set ser");
  }
else printf("Set ser OK\n");
printf("Ser mode = %d\n",modbus_rtu_get_serial_mode(mb));


er=modbus_set_slave(mb, 1);     //altivar default = 1  (COM menu, adr)
if (er<0)
  {
  printf("slave fail\n"); perror("Slave");
  }
else printf("Slave OK\n");

int rc,addr,nb_fail;
uint16_t tab_rq_registers[256];
uint16_t tab_rp_registers[256];

addr=3201;

tab_rq_registers[0]=0;
/*
printf("Write reg %d\n",addr);
rc = modbus_write_register(mb, addr, tab_rq_registers[0]);
//rc = modbus_write_registers(ctx, addr, nb, tab_rq_registers);
if (rc != 1)      //if (rc!=nb)
  {
  printf("ERROR modbus_write_register (%d)\n", rc);
  printf("Address = %d, value = %d (0x%X)\n", addr, tab_rq_registers[0], tab_rq_registers[0]);
  nb_fail++;
  }
else
*/
  {
  printf("Read regs %d\n",addr);
  rc = modbus_read_registers(mb, addr, 1, tab_rp_registers);
  if (rc != 1)
    {
    printf("ERROR modbus_read_registers single (%d)\n", rc);
    printf("Address = %d\n", addr);
    nb_fail++;
    }
  else
    {
    if (tab_rq_registers[0] != tab_rp_registers[0])
      {
      printf("ERROR modbus_read_registers single\n");
      printf("Address = %d, value = %d (0x%X) != %d (0x%X)\n", addr, tab_rq_registers[0], tab_rq_registers[0],tab_rp_registers[0], tab_rp_registers[0]);
      nb_fail++;
      }
    }
  }

modbus_free(mb);
printf("\n\n"); 
}



Torello Querci

unread,
Nov 4, 2017, 10:56:38 AM11/4/17
to libm...@googlegroups.com
Hi Marko,

are you tried to configure the device as normal RS232.
I'm using a lot of USBtoRS485 converter and the bus acquisition are made by the converter, not the Operating System.

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "libmodbus".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+unsubscribe@googlegroups.com.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

Marko Cebokli

unread,
Nov 4, 2017, 2:40:18 PM11/4/17
to libmodbus
Hello Torello,

I can do that, but how will then the TX/RX switching be done? RS232 is full duplex (separate TX and RX wires, like 422), but 485 has common wires, and the TX must be switched on and off.

Torello Querci

unread,
Nov 4, 2017, 2:56:27 PM11/4/17
to libm...@googlegroups.com
Hi Marko,

for all USB-RS485 converted that I have tested, bus is acquire only when I have something to transmit and when the host have nothing to transmit the line go to high impendence mode so other devices can transmit.
So, in my cases is the USB converter itself that manage the high impedence mode when the transmission buffer is empty.
In this scenario the device seems a simple RS232.

Best Regards

Marko Cebokli

unread,
Nov 5, 2017, 3:42:48 AM11/5/17
to libmodbus
Hello Torello,

I hava attached an oscilloscope and I see that the behavior is actually the same whether I set RS232 or RS485 (with the error) - see attached picture.

I haven't yet installed the polarization resistors, just a 180 ohm load.
In the picture, the two wires are top and bottom, and middle is the difference.

On the left, TX is off and difference is close to zero. Then, the start bit, the first byte (0x01), and the even parity bit (one) can be seen. But the stop bit (should be one) is missing, looks like the TX has been switched off prematurely (zero voltage difference).
Also, the second byte (0x03) is mangled, you can see the start bit and the first bit (one), but the second bit (should be one too) is mangled, like the TX is being switched off for one bit time (the following zeros are OK)?
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.
2017-11-05_09-19-15.jpg

Marko Cebokli

unread,
Nov 5, 2017, 4:46:36 AM11/5/17
to libmodbus
I found out than whenever there are two consecutive ones in the data, the TX goes off.
Weird!

Marko Cebokli

unread,
Nov 5, 2017, 2:29:27 PM11/5/17
to libmodbus
Now I tried just "cat somefile > /dev/ttyUSB0" and I get the same strange effect!
Looks the adapter itself is crap...  Chinese from Ebay....

Any suggestions where to buy a good one?

But please not in this price range:
http://at.rs-online.com/web/p/schnittstellenadapter/7456734/

:-)  :-)  :-)

Patrick Boettcher

unread,
Nov 5, 2017, 5:11:07 PM11/5/17
to Marko Cebokli, libm...@googlegroups.com
Hi Marko,

On Sun, 5 Nov 2017 11:29:27 -0800 (PST)
Marko Cebokli <mceb...@gmail.com> wrote:

> Now I tried just "cat somefile > /dev/ttyUSB0" and I get the same
> strange effect!
> Looks the adapter itself is crap... Chinese from Ebay....
>
> Any suggestions where to buy a good one?
>
> But please not in this price range:
> http://at.rs-online.com/web/p/schnittstellenadapter/7456734/
>
> :-) :-) :-)


I'm using this one for RS485.

https://www.reichelt.com/de/de/?ARTICLE=122187

regards,
--
Patrick.

Marko Cebokli

unread,
Nov 12, 2017, 4:13:48 AM11/12/17
to libmodbus
Hello,

bought a couple of USB/485 adaptors from Germany (a little more than double the Chinese price, but still quite cheap).  They are FTDI based, also can't set_serial to 485, but worked in 232 mode, I could read the status register from the motor box.

But after a couple of minutes, the motor box reported an modbus error, and the USB/485 adapter stopped working. I fried both of them before realizing that is is probably the response from the motor box that kills them, the diff drivers in there using a higher voltage? The ground is OK, and I soldered the polarizing resistors from the gnd and 5V provided by the USB/485, so I can't think of another reason.
On the scope, the response did not look higher, but the USB/485 was probably limiting the voltage. When I get some more adapters, I will try with some added resistors and zeners.

But that is not an libmodbus problem anymore, so I wont spam here with that.

Thanks to everybody who responded!
Reply all
Reply to author
Forward
0 new messages