Saturday, 21 April 2012

Controlling an LG TV via serial from Linux.

I have an LG plasma TV. Like most electronic home entertainment stuff these days, it comes with a port on the back for controlling it. In this case a serial port. LG helpfully put all the commands for controlling it in the back of the manual. 

The commands are along the lines of:

ka 01 00 - Which turns the power off.


kf 01 ff - Which is query, "what is the power status?"

Which will return:

a 01 OK01x     - A string as to whether the TV is On or Off, in this case "ON".

This is useful as it provides a feedback mechanism, rather than shooting commands down the line and hoping that they'll have an effect (a la X10).

So if I want to increase the volume by 1, I can read the current volume and add 1 to it.

So this gives me the opportunity to automate tasks based on external events. Now a lot of my stuff I control from BASH Linux shell scripts and this is no different. 

To control it from BASH is quite easy, you can just "echo" to or "cat" from the serial device. But first I set up the serial connection:

stty time 3 min 0 < $serial
stty -F $serial -icrnl -ixon ignbrk isig -echoctl -echoke -isig -icanon -iexten 
-echo -echoe -echok -opost -onlcr -hupcl

That last line is all one line, i.e. it starts with "stty -F" and continues on to the end of "-hupcl".

The TV will communicate at the seemingly standard 9600 baud, 8N1 bits, parity and stop bits.

So if I want to turn the TV to the HDMI4 input, I can send:

echo "xb 01 93" > $serial

The 93 is derived from a binary representation from the chart on page 121 of my manual. The 9 being the input type (DTV, HDMI etc) and the 3 being the actual input of that type. In this case port 3 (as it counts up from 0).

If I want to read the current input:

echo "xb 01 FF" > $serial
sleep 1
rdserial=`cat $serial`

This would return something like:


Which in this case would mean:

HDMI, represented by the hex number "a" and port number represented by the number 3. The output doesn't seem to be in my manual, but goes as follows:

0 - DTV, 1 - Analogue TV, 3 -AV, 4 - Component, 5 - VGA PC Input and finally "a" - HDMI.

The manual list commands to be able to most of the functions on the TV by the serial port, everything from sending keys to simulate remote presses to changing the aspect ratio etc.

I've found the HDMI link function (SIMPLINK) on the TV not overly useful in my use with my PS3, so the serial control offers something a little more customisable.

No comments:

Post a Comment