CrazyCreator
Posts: 3
Joined: Thu Oct 11, 2012 3:01 pm

Python Serial Port Problem

Fri Nov 16, 2012 12:56 am

I have my computer connected to my Pi's serial port through a serial bluetooth module. I have a python script running that says when it receives a certain character, to turn on an LED. Not only is the LED not turning on, but if I send it "H", for example, it sends back and infinitely long string of "H"s. What could be wrong? Code is below.

Code: Select all

import serial
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)
ser = serial.Serial("/dev/ttyAMA0", 9600, timeout=1)
while 1:
      x = ser.read()
              if x == "H":
                      GPIO.output(24, GPIO.HIGH)
              if x == "L":
                      GPIO.output(24, GPIO.LOW)

SiriusHardware
Posts: 507
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Python Serial Port Problem

Mon Nov 19, 2012 7:37 pm

CrazyCreator wrote:I have my computer connected to my Pi's serial port through a serial bluetooth module. I have a python script running that says when it receives a certain character, to turn on an LED. Not only is the LED not turning on, but if I send it "H", for example, it sends back and infinitely long string of "H"s. What could be wrong? Code is below.

Code: Select all

import serial
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)
ser = serial.Serial("/dev/ttyAMA0", 9600, timeout=1)
while 1:
      x = ser.read()
              if x == "H":
                      GPIO.output(24, GPIO.HIGH)
              if x == "L":
                      GPIO.output(24, GPIO.LOW)
CC: By default, the Pi's system uses the onboard serial port as an alternative console - have you taken the steps to disable that, in order to gain sole control over the serial port? It may be that Pyserial on the Pi automatically does that for you, but if not, that would be one likely cause of seemingly irrational behaviour.

Return to “Python”