Jump to: navigation, search

Using Python

Revision as of 01:41, 17 September 2011 by Martin (Talk | contribs) (Reading all kwikwai has to say)

Python is a multi platform programing language. As all programing languages it has its up and downs but since we have some code to share already written we will start with this.

The first example allows you to log everything that is happening on the serial port of kwikwai.

What you need

  • A HDMI-CEC TV
  • A second HDMI-CEC device
  • A kwikwai
  • A mini USB cable or a RS232 cable
  • A computer capable of running Python

The code

Using the serial port

You also need:

  • A mini USB cable or a RS232 cable
  • A spare USB port or RS232 cable
  • The python serial toolbox pyserial

Reading all that kwikwai has to say

The following code enables you to write to a file all data written to the serial port by kwikwai.

 import serial
 
 PORT = "/dev/cu.usbserial-A80080OO"   # change this to your actual serial port
                                       # /dev/cu.usbserial-XXXXXXX on MacOS
                                       # /dev/ttyUSB0 on GNU/Linux
                                       # COM3 on Windows
                                       
 OUTPUT = "kwikwai.log"
 DEFAULT_TIMEOUT = 1
 
 kwikwai = serial.Serial(PORT, 38400, timeout = DEFAULT_TIMEOUT)
 
 logfile = open(OUTPUT,'w')
 
 kwikwai.write("\rcec:listen:set 1\r")
 try:
  while True:
    line = kwikwai.readline()
    logfile.write(line)
    logfile.flush()
 except KeyboardInterrupt:
  pass

Sending a command

This few lines are the only ones needed to tell your TV to shut down.

 import serial
 
 PORT = "/dev/cu.usbserial-A80080OO"   # change this to your actual serial port
                                       # /dev/cu.usbserial-XXXXXXX on MacOS
                                       # /dev/ttyUSB0 on GNU/Linux
                                       # COM3 on Windows
                                       
 DEFAULT_TIMEOUT = 1
 
 kwikwai = serial.Serial(PORT, 38400, timeout = DEFAULT_TIMEOUT)
 
 kwikwai.write("cec:send A FF:36\r")

Through TCP/IP

You also need

  • To connect your kwikwai to the Internet

Sending commands to kwikwai

 import socket
 
 HOST = 'kwikwai.local'
 PORT = 9090
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect((HOST, PORT))
 s.send('cec:send A FF:36\r')
 s.close()

Reading all kwikwai has to say

 import socket
 
 HOST = 'kwikwai.local'
 PORT = 9090
 
 OUTPUT = "kwikwai.log"
 
 logfile = open(OUTPUT,'w')
 
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect((HOST, PORT))
 
 s.send("\rcec:listen:set 1\r")
 try:
  while True:
    line = s.recv(1)
    logfile.write(line)
    logfile.flush()
 except KeyboardInterrupt:
  pass
 s.close()