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?

P_Lash
  • 31
  • 4

2 Answers2

1

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?

NomadMaker
  • 1,560
  • 9
  • 10
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