indee
Posts: 10
Joined: Fri Oct 14, 2016 8:48 am

Problem with RS-232

Fri Oct 14, 2016 9:10 am

I'm using different USB to RS232 converters to establish serial communication with PC.
In Raspberry Pi I write string "0123456789\r" to "/dev/ttyUSB0".
Serial cable connected to PC where I'm reading this data from Raspberry Pi.
In most cases on the PC I'm receiving string "0123456789\r" correctly:

number char int
0 0 48
1 1 49
2 2 50
3 3 51
4 4 52
5 5 53
6 6 54
7 7 55
8 8 56
9 9 57
10 13

But quite often I'm receiving a wrong data:

number char int
0 ᆰ 86
1 10
2 ᄁ 94
3 j 106
4 ハ 118
5 ᄄ 88
6 8
7 * 42
8 ᅠ 96
9 ( 40
10 8

This wrong data will appear until I restart the board or unplug the USB adapter then I'm receiving another wrong data.

Code:

Code: Select all

struct termios PortSettings;


bool OpenPort(){
          
    Port = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
    
    if(Port == -1){
        printf("%s", "Error of opening port /dev/ttyUSB0");
        printf("error %d opening %s: %s", errno, "", strerror (errno));
        return false;
    }
    else{
        printf("%s", "\n  ttyUSB0 Opened Successfully\n");        
    }    
    
    
    tcgetattr(Port, &PortSettings);
    
    cfsetispeed(&PortSettings,B9600);
    cfsetospeed(&PortSettings,B9600);
    
    PortSettings.c_cflag |=  PARENB;
    PortSettings.c_cflag &= ~CSTOPB;
    
    PortSettings.c_cflag &= ~CSIZE;
    PortSettings.c_cflag |=  CS8;    
    
    PortSettings.c_cflag |= CREAD | CLOCAL;
    
    PortSettings.c_cc[VMIN]  = 0; 
    PortSettings.c_cc[VTIME] = 20;
    
    PortSettings.c_iflag = 0;
    
    
    PortSettings.c_oflag = 0;
    PortSettings.c_lflag = 0;
    
    tcflush(Port, TCIFLUSH);
    
    tcsetattr(Port,TCSANOW,&PortSettings);    
    
    return true;
}


void WriteToPort(){
            
    tcflush(Port, TCIOFLUSH);
    
    write(Port, "0123456789\r", 11);
        
}
The same code I'm using in another embedded ARM-based boards and It works properly and stable.
Serial port parameters: 9600, 8, evenparity, onestopbit

How this problem can be solved and why data is corrupted?

Thank you!

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Problem with RS-232

Fri Oct 14, 2016 10:42 am

If you want to test the hardware to exclude your code as the source of the error then you can use this shell script.

Code: Select all

#!/bin/bash

stty -F /dev/ttyUSB0 9600
stty -a -F /dev/ttyUSB0

while true; do
  echo -en "0123456789\r" > /dev/ttyUSB0
  sleep 5
done;


6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8918
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Problem with RS-232

Fri Oct 14, 2016 3:25 pm

You appear to be asking for even parity. One of the USB to serial chips mishandled parity with the standard Linux kernel driver. Searching appears to say that it is the CH34x chips.
What chip is in your device? "lsusb -v" should tell you.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

indee
Posts: 10
Joined: Fri Oct 14, 2016 8:48 am

Re: Problem with RS-232

Mon Oct 17, 2016 7:34 am

Raspberry Pi 2

lsusb -v

1)
USB to Serial device which does not work properly:
idVendor: 0x0403 Future Technology Devices Internation, Ltd
idProduct: 0x6001 FT232 USB-Serial (UART) IC

2)
USB to Serial device which works properly:
idVendor: 0x067b Profilic Technology, Inc.
idProduct: 0x2303 PL2303 Serial Port.

I also have some other problems with Raspberry Pi 3 and USB_to_Serial devices but I have to investigate it first

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Problem with RS-232

Mon Oct 17, 2016 9:04 am

Those are the 2 most common usb2serial chipsets you can find, and should behave quite nicely unless you have fake ones.

( I think FTDI may have some windows programs you can use to detect fakes of their chips, but I'm not sure. )

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8918
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Problem with RS-232

Mon Oct 17, 2016 9:21 am

topguy wrote:( I think FTDI may have some windows programs you can use to detect fakes of their chips, but I'm not sure. )
You mean the driver update that bricked fake devices?!
http://www.theregister.co.uk/2014/10/23 ... to_bricks/ although subsequently pulled according to http://www.theregister.co.uk/2014/10/24 ... _response/

Agreed though that both FTDI and PL2303 chips normally work just fine.
Have you tested with a straight loopback and confirmed you receive what you think you sent?
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

indee
Posts: 10
Joined: Fri Oct 14, 2016 8:48 am

Re: Problem with RS-232

Mon Oct 17, 2016 9:23 am

I'm using FTDI for years without problems but with the Raspberry Pi ...((((
The first time I discovered problem of FTDI it was rxtx Java library.
I spent many days to find out why software works correctly on other embedded boards but not on PBpi

indee
Posts: 10
Joined: Fri Oct 14, 2016 8:48 am

Re: Problem with RS-232

Mon Oct 17, 2016 9:25 am

Have you tested with a straight loopback and confirmed you receive what you think you sent?
I tested with RP3 and it works ok, but unfortunately I do not have RP2 anymore to compare the results

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8918
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Problem with RS-232

Mon Oct 17, 2016 10:08 am

indee wrote:I tested with RP3 and it works ok, but unfortunately I do not have RP2 anymore to compare the results
Does the Pi3 have issues talking to the PC?

There's nothing I can think of (except power - are you using a decent power supply?)) that could cause that sort of corruption.
The Pi doesn't supply any form of clock to the USB to serial chip that could change the transmission as a baud rate issue. Checking the kernel driver logs, there haven't been any changes to the FTDI driver except adding some extra USB VID:PID combinations for a fair while. What kernel version were you using on your other boards, just so that a kernel driver change can be excluded?
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

indee
Posts: 10
Joined: Fri Oct 14, 2016 8:48 am

Re: Problem with RS-232

Tue Oct 18, 2016 6:29 am

topguy, thank you for the shell script, could you please add also a reading here (cat /dev/tty.. or tail -f /dev/tty..) that I'll be able to test read/write.

indee
Posts: 10
Joined: Fri Oct 14, 2016 8:48 am

Re: Problem with RS-232

Wed Oct 19, 2016 11:43 am

I have a lot of errors talking to the PC with Pi3 with USB->Serial device "idVendor: 0x067b Profilic Technology, Inc.".
This device has 4 serial ports.
When I open more than two ports and send data through ttyUSB0 I'm receiving a wrong data on PC. when only one port is opened it works OK.
quite often instead of "123456789\r" I get a garbage such as:
"1223\r"
"8901\r23"
"01\r"
"\r\r"
etc.

With FTDI "idVendor: 0x0403 Future Technology Devices Internation, Ltd" a have less errors approx 1 error per 20000 messages.
Type of error as I described at the beginning of this post.

Pi3 has original power supply STONTRONICS T5875DV Official 2.5A 5.1V
OS: Rasbian GNU/Linux 8.0(jessie) 4.4.21-v7+

Each USB device has own 5V power supply.

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5618
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Problem with RS-232

Wed Oct 19, 2016 5:20 pm

Can you clarify/confirm some things please?
1) Which direction are you connected:
USB at the Pi, serial at the PC or vice versa? The latter is the way I've usually connected a PC, running GtkTerm on Ubuntu/Debian, to a Pi ie. USB on the PC, serial (3.3V TTL) at the Pi.
2) If you're using USB at the Pi end how do you know for sure the issue isn't at the PC end (eg. do you get similar "corrupted" data if you run a loopback test?)**
Trev.
** As done in the examples (for various USB-to-SerialTTL adapters):
http://www.cpmspectrepi.uk/raspberry_pi ... pters.html
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

indee
Posts: 10
Joined: Fri Oct 14, 2016 8:48 am

Re: Problem with RS-232

Thu Oct 20, 2016 7:29 am

In this post I describe only 50% of problem, the next half is errors or reading at Pi side.

-- 1) Which direction are you connected: USB at the Pi, serial at the PC or vice versa?
Yes, but it is not necessary a PC at can be another device such as embedded board, PLC or Pi. I found this problem using a controller which uses RS232.

-- 2) If you're using USB at the Pi end how do you know for sure the issue isn't at the PC end

I run GTKTerm to listen ttyUSB1, run shell script in ttyUSB0, connected both serial serial ports with a cable (ttyUSB2 and ttyUSB3 opened as well) ... and result is https://www.dropbox.com/s/zwqo4t1c4c5ux ... 081234.mp4

Thank you for the link, seems I do not have problem with such device but it would be great if you provide a link where Pi works correctly with multi port devices (see uploaded pic)
Attachments
Untitled.jpg
Untitled.jpg (39.72 KiB) Viewed 2372 times

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5618
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Problem with RS-232

Thu Oct 20, 2016 5:10 pm

indee wrote:In this post I describe only 50% of problem, the next half is errors or reading at Pi side.
-- 1) Which direction are you connected: USB at the Pi, serial at the PC or vice versa?
Yes, but it is not necessary a PC at can be another device such as embedded board, PLC or Pi. I found this problem using a controller which uses RS232.
-- 2) If you're using USB at the Pi end how do you know for sure the issue isn't at the PC end
I run GTKTerm to listen ttyUSB1, run shell script in ttyUSB0, connected both serial serial ports with a cable (ttyUSB2 and ttyUSB3 opened as well) ... and result is https://www.dropbox.com/s/zwqo4t1c4c5ux ... 081234.mp4
Thank you for the link, seems I do not have problem with such device but it would be great if you provide a link where Pi works correctly with multi port devices ...
Thank you for the clarifications. Given that you're (only) using USB at the Pi end then the issue is not really a specific "serial device"** one but rather one of competition between multiple USB devices (which can include the wired network which is a USB-to-ethernet device). W.r.t. to "multi-port" USB devices, including serial port(s) the only thing I've (briefly) tried out (on an older Pi) was this:
http://www.cpmspectrepi.uk/raspberry_pi ... rtRep.html
and I probably only used it to test a PS/2 keyboard, PS/2 mouse and serial loopback at the most (ie. 3 of the four available ports and none running significantly "in parallel") so that's probably far less complex than your set up.
Trev.
** such as the serial port capable pins of the GPIO's which the Pi controls the clocks, baud rate, parity etc. directly, or a single model/type of USB-to-serial chip and associated driver.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5618
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Problem with RS-232

Tue Oct 25, 2016 5:17 pm

I've just come across this in another thread:
viewtopic.php?p=1058151#p1058151
Maybe that's the root cause of your issues?
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

indee
Posts: 10
Joined: Fri Oct 14, 2016 8:48 am

Re: Problem with RS-232

Wed Oct 26, 2016 5:41 am

Thank you Trevor, it is an interesting post.

Return to “C/C++”