0

I'm trying to send some data serially to my PC from an RPi 3, but I'm receiving gibberish most of the time. I have already performed the prerequisites: disabled Bluetooth and enabled GPIO 14 and 15 to use UART0/ttyAMA0 (more info here and here).

I am using a USB to TTL converter - connected to GPIO 14 (TXD0), 15 (RXD0), and Ground).

The Python test code I'm using to send data from the Pi is as follows:

import serial

port = serial.Serial("/dev/serial0", baudrate=9600, timeout=20000,bytesize=serial.EIGHTBITS, 
parity=serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE)

while True:
port.write('A')

And on the receiving end, I'm using a MATLAB script:

clear all;
clc;

   if ~isempty(instrfind)        //Check for any open serial port objects and close them
      fclose(instrfind);
      delete(instrfind);
   end

rpiserial=serial('COM4','BaudRate',9600, 'DataBits', 8, 'StopBits', 1, 'Parity', 'none');

fopen(rpiserial);
pause(0.1)
y = fscanf(rpiserial, 'c')
fclose(rpiserial);

Most of the times after running the MATLAB script I get gibberish on the receiving PC end. How do I fix this?

  • 1
    You do not need to disable Bluetooth to use simple serial. Have you disabled Serial console? See How-do-i-make-serial-work-on-the-raspberry-pi3 – Milliways Feb 28 '18 at 22:55
  • Yes, I have disabled serial console. – P_Lash Feb 28 '18 at 23:04
  • 1
    use a terminal program to monitor the data ... do not use MATLAB ... that should narrow down the area of failure ... it is possible that the python script on the RPi is swamping the serial port – jsotola Feb 28 '18 at 23:31
  • Would parity bits help with error correction? – P_Lash Mar 2 '18 at 7:33
0

For enabling and testing serial comm in raspi, please follow these steps.

1. Login to raspi via ssh (use putty)
2. Enable serial via raspi-config
2.a Enter the command "sudo raspi-config"
2.b Select "Interfacing Options"
2.c Select "Serial  Enable/Disable shell and kernel m..."
2.d Select "No" for "Would you like a login shell to be accessible..."
2.e Select "Yes" for "Would you like the serial prot hardware..."
2.f Select "Ok" and then "Finish" to exit from raspi-config
3. Make sure that uart is enabled by running this command "cat /boot/config.txt | grep uart"
3.a If you got enable_uart=0, edit the file /boot/config.txt and change "enable_uart=1"
4 Reboot the Raspberry Pi

The above steps will ensure that serial port is enabled and system logs wont get redirected to serial port.

The next step is to create a loopback connection and test with a simple python script. You can use the below test script. Use /dev/serial0 as it will point to the default uart interface of the Pi.

import serial
ser = serial.Serial('/dev/serial0', 9600, timeout=1)

ser.write("loopback test")
try:
    response = ser.readline()
    print response
except KeyboardInterrupt:
    ser.close()

Once all these are done, you can try connecting to MATLAB. There are additional tools and support from MATLAB/Simulink to program Raspberry Pi. You can try those if interested. Link: Raspberry Pi support from MATLAB

| improve this answer | |
0

You specifically mentioned a USB to TTL converter. The Raspberry Pi is a 3v3 device and TTL is a 5V specification.

Or did you mean a USB to 3v3 serial converter?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.