0

Here is my code so far. It's intended to read an output from projector's power state.

Problem I have with it is that it gives no output besides ">>"

Already tried modifing the response variable with:

  1. response = ser.readline()
  2. response = ser.read()
  3. response = ser.inWaiting()

What's intresting is that when I run cat /dev/ttyS5 after finished Python script. It gives me output and exits. Normally when I am using a cat /dev/ttyS5 it goes forever and needs to be terminated.

#!/usr/bin/python
import serial
import sys
import os
import time

pin_export = 'echo 2 > /sys/class/gpio/export'
pin_out = 'sleep 0.1 && echo out > /sys/class/gpio/gpio2/direction'
pin_high = 'sleep 0.1 && echo 1 > /sys/class/gpio/gpio2/value'
pin_low = 'sleep 0.1 && echo 0 > /sys/class/gpio/gpio2/value'
pin_unexport = 'echo 2 > /sys/class/gpio/unexport'
response = ''

os.system(pin_export)
os.system(pin_out)
os.system(pin_high)

ser = serial.Serial()
ser.port = '/dev/ttyS5'
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False

ser.open()
ser.write(b'\r*pow=?#\r')
y = "1"
ser.timeout=0.5

while ser.inWaiting() > 0:
    response = ser.readline()

print(">>" + response)

time.sleep(1)
os.system(pin_low)
os.system(pin_unexport)

On the other hand if any of you guys have a better idea how to read an input from /dev/ttyS5 that I later can save to text file I'm open for suggestions.

1 Answer 1

1

Okay, so I figured it out myself with some testing. Here's my answer if someone comes here with the same problem or related.

The problem was that the first line of the rs respond was containing some uneeded values and it was blocking the port from other answers.

#!/usr/bin/python

import serial
import sys
import os
import time

# bash commands for gpio
pin_export = 'sleep 0.05 && echo 2 > /sys/class/gpio/export'
pin_out = 'sleep 0.06 && echo out > /sys/class/gpio/gpio2/direction'
pin_high = 'sleep 0.05 && echo 1 > /sys/class/gpio/gpio2/value'
pin_low = 'sleep 0.05 && echo 0 > /sys/class/gpio/gpio2/value'
pin_unexport = 'sleep 0.05 && echo 2 > /sys/class/gpio/unexport'

# turning on pin 2
os.system(pin_export)
os.system(pin_out)
os.system(pin_high)

# serial settings
ser = serial.Serial()
ser.port = '/dev/ttyS5'
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False

# port open
ser.open()

while True:
    # buffor reset
    ser.reset_input_buffer()
    ser.reset_output_buffer()
    # sending question for projector
    ser.write('\r*pow=?#\r')
    time.sleep(0.6)

    # trashing first line
    trash = ser.readline(30)
    # the needed answer from rs
    pwrinfo = ser.readline(9)

    if pwrinfo == "*POW=OFF#":
        print "Projector OFF"
    elif pwrinfo == "*POW=ON#\r":
        print "Projector ON"
        break
    else:
        print "Heating or Cooling..."
                
    # buffor clear
    ser.flush()

# port close
ser.close()

# turning off pin 2
os.system(pin_low)
os.system(pin_unexport)

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