I am currently working on serial communication between RPi and PIC. I am new to Raspberry Pi and python world.... okay following is my code:

from serial import *
from time import *
import RPi.GPIO as GPIO



ser = Serial("/dev/ttyS0",9600)
for x in range (0,20):
     print('input is '+q)
     print('OutPt is '+deta)

I am using GPIO pin as an enable pin for RS485 driver. I transmit 1 and should receive the same for 20 iterations but I get a bunch of garbage first then 1 and again garbage and again 1. In between 1's their are two or four garbage values as delay is varied. I have read different solutions like interrupt, threading, etc. but I can't wrap my head around any of them. Is their any simple solution?

  • Receiving "garbage" over USART is often a symptom of using the incorrect baud at one one end or the other of the line, you may also notice that you get more or less bytes out than you put in depending on whether the baud is too low or too high. Can you confirm the baud rate the PIC is listening for? Also worth trying a loop-back test on the Pi with a serial terminal emulator and your code. Feb 2, 2019 at 11:00
  • yes thanks for your comments both baud-rates of master and slave are 9600. problem was faulty driver IC now when i try to send '1' it gives me garbage and when i try to send 10 and above it gives me '\'. is it transmitting data in a different format or what? Feb 4, 2019 at 7:56
  • Did you try a loop-back test with your code: If you send '1' do you read it back correctly? I've not much experience with Python either I'm afraid but can you convert the data you receive into hex as that might give you a clue as to what the "garbage" is. Feb 4, 2019 at 8:59
  • Ok thank you roger Jones. we rectified the error, it was delay of 1 ms to be given after slave transmitter. now it is working fine Feb 13, 2019 at 10:59


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.