mafish
Posts: 3
Joined: Fri Nov 17, 2017 7:59 am

UART sending garbage after a succesfull message

Fri Nov 17, 2017 8:20 am

Hello,
I want Raspberry Pi 3 to send and receive data over rs485.
I've hooked it up via suitable transceivers, with two XMC4500MCUs and a USB logic analyzer on my PC.

The thing is RPI sends some garbage messages (as long as the program is running->until I stop it with Ctrl+C) just after successfully sending the first message. Then I try to send the second message it's being sent somewhere among the garbage messages.

I am sure that TxRx are connected correctly, and I'm sure RPI is the source of the problem ( checked that with logic analyzer on every TxRx pin in the network). Also, baud rates are correct, because the first message is both sent and received with no problems.

I've disabled Bluetooth module and serial console according to some tutorial I found on the net.
here is the code snippet for sending:

Code: Select all

	unsigned char msg[10]= {L.adress, 0xA1, 1, 1,1,1};
        int uart0_filestream = -1;
        uart0_filestream = open("/dev/serial0", O_RDWR | O_NOCTTY | O_NDELAY);
        if (uart0_filestream == -1)
        {
            printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");
        }
        write(uart0_filestream, msg, 6 );
        std::cout<<"..."; std::cin>>msg[5];		//used as trigger for second message
        msg[0] = P.adress;
        write(uart0_filestream, msg, 6);
Also there is a photo of the signal from oscilloscope
https://imgur.com/a/dMyEA

beta-tester
Posts: 1152
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: UART sending garbage after a succesfull message

Fri Nov 17, 2017 8:28 pm

mafish wrote:
Fri Nov 17, 2017 8:20 am
I've disabled Bluetooth module and serial console according to some tutorial I found on the net.
what exactly did you manually according which tutorial?
as far as i know is the raspi-config doing it normally for you, when you enable serial communication,
as long you have the most recent and actual version installed.
{ I only give negative feedback }
RPi Model B (rev1, 256MB) & B (rev2, 512MB) & B+, RPi2B (1GB), 64GB microSDXC1 class 10, HDMI 1920x1080, keyboard-mouse-combo (wireless), PiCamera, ethernet-cable, 5V/1.2A power supply, Wifi dongle (rt5370)

mafish
Posts: 3
Joined: Fri Nov 17, 2017 7:59 am

Re: UART sending garbage after a succesfull message

Mon Nov 20, 2017 8:37 am

I don't remember which tut did I follow but I definitely started from RPI site:
https://www.raspberrypi.org/documentati ... on/uart.md

I pretty sure that the problem lies in shared TX/RX buffer register in RPI and fact that UART has two separate lines for RX and TX, the standard RS485 is a single pair (two wire differential signal) that messages are going both ways on the same pair of cables.
I suspect RPI is sending the data, then "receiving" its own data at the same time, packing it back to TX/RX buffer, corrupting any existent data in the register, and sending it over and over via its UART ( as the buffer never empties).
Do you know any way to disable listening on RX pin via software?
I know I can theoretically use GPIO + NPN to enable/disable RX pin but I would prefer to avoid any hardware modification to the circuit.

User avatar
DougieLawson
Posts: 30430
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: UART sending garbage after a succesfull message

Mon Nov 20, 2017 3:54 pm

Have you disabled the 115200 baud serial console?
What happens if you connect at that baud rate? Do you still get garbage?
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

mafish
Posts: 3
Joined: Fri Nov 17, 2017 7:59 am

Re: UART sending garbage after a succesfull message

Thu Dec 07, 2017 12:51 pm

Sorry for this very long delay in posting.

Just as I thought , the problem was related to tx/rx buffers.
I am still not sure how do they work, but I've fixed my problem by adding the AND gate with GPIO and RX on the inputs and Raspberrys RX pin on the output side. Whenever i want to write to the bus I ahave got to set GPIO low , after i am done sending i set GPIO back high and the signals look just fine.
But I encountered anoter problem, my read() method seems to not work correctly , it just always sets my rxData variable to only 0s .

Read function:

Code: Select all

	digitalWrite(1, HIGH);
	while(digitalRead(1) == 0);
	read(fd, rx, 8);
	delay(2);
	digitalWrite(1,LOW);
Uart initialization

Code: Select all

int UART_Init()
{
	
	fd = open("/dev/serial0", O_RDWR | O_NOCTTY);// | O_NDELAY);
	struct termios options;
	tcgetattr(fd, &options);
	options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
	options.c_iflag = IGNPAR;
	options.c_oflag = 0;
	options.c_lflag = 0;
	tcflush(fd, TCIFLUSH);
	tcsetattr(fd, TCSANOW, &options);
	
    if (fd == -1)
    {
        cout << "UART FAIL" << endl;
		return 1;
    }
	else
	{
		cout << "UART OK" << endl;
		return -1;
		
	}
	
}
Again ive checked every hardware aspect with logic analyzer and alll signals are transmitted correctly, so the problem lies within Raspberry sofware.

Return to “Troubleshooting”

Who is online

Users browsing this forum: dazz100 and 49 guests