Linux Python serial script for TV control
#1
I have a Samsung TV that I like to control by serial because I don't like the look of little IR transmitters.

Right now I have this:
Code:
import time, serial
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=1)
ser.write("\x08\x22\x00\x00\x00\x00\xd4")
ser.close()

And it works but often I have to run it 3 or 4 times before the TV responds.

I got this started:
Code:
import time, serial
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=1)


while True:
    data = ser.read(3)
    if len(data) > 0:
        print 'sucess'

    sleep(0.5)
    print 'Retrying'
    ser.write("\x08\x22\x00\x00\x00\x00\xd4")

ser.close()

The problem is this will just keep going over and over even if it does read some data. The TV responds with 3 bytes when theres a successful command. Ideally I would like this to keep trying until it gets those three bytes. (03 0c f1) can someone help me make this work the way I want?

Thanks!!!
Reply
#2
never used serial, but did you try the obvious:
Code:
while (data != "03 0c f1"):
  ser.write("\x08\x22\x00\x00\x00\x00\xd4")
  data = ser.read(3)
  sleep(0.5)
  
ser.close()

you'll have to check what "data" holds on a successful event and use that in the while clause.
Reply
#3
When I print the data variable I just get my whole terminal cleared and a odd little single character sized box in the top left corner. How can I get it to print the hex value?
Reply
#4
I found the solution to everything by accident. The reason I needed this to loop is the tv was not reliably responding to my command. I tried sending a command I thought would make the tv respond with its power status. However it actually more reliably turns the tv on and off.

For anyone looking for this solution I have used this simple code.

Code:
import time, serial
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=1)
ser.write("\x08\x22\x00\x00\x00\x00\xd6")
ser.close()
Reply

Logout Mark Read Team Forum Stats Members Help
Python serial script for TV control0