0

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

q='1'

GPIO.setmode(GPIO.BOARD)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.LOW)

ser = Serial("/dev/ttyS0",9600)
for x in range (0,20):
     GPIO.output(18,GPIO.HIGH)
     sleep(0.001)
     ser.write(q)
     print('input is '+q)
     GPIO.output(18,GPIO.LOW)
     sleep(1.1)
     deta=ser.read()
     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. – Roger Jones Feb 2 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? – andrexstack Feb 4 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. – Roger Jones Feb 4 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 – andrexstack Feb 13 at 10:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.