RS232 (Serial) Control of LG LCD TV via MythTV

I got sick of having a remote control for everything, so I have started down the path of using RS232 (Serial) control where I can. Currently that is only my TV, but when the Decoder and DVD player get replaced I will be looking for models with remote control capability (I know I could use an IR blaster, but that can impede using the real remote and looks kind of ugly).

So I started by deciding the basic features I used on my TV all the time and how to remap them to the remote control I use for MythTV (I haven’t done every button, I figured if I need to use the remote once a month then so be it). The features required for me were:

  • On/Off
  • Aspect Ratio
  • Input (DTV, Comp, RCA, VGA)

I found the codes required to send to the TV on the LG website somewhere. If your TV supports RS232 there should be a document somewhere that describes the protocol used you will just need to find it.

I found the following code somewhere, it was originally used just to send and receive data via a serial port so I have modified it to send what I need to the TV and removed everything else.

Just drop the following in a file called tvcontrol in /usr/local/bin

#!/usr/bin/python

#allows you to capture the command switches
import sys

#get serial features for python
import serial

#this next line sets up the serial port to allow for communication
#and opens the serial port you may need to change
#ttyS0 to S1, S2, ect. The rest shouldn't need to change.
ser = serial.Serial('/dev/ttyS0', 9600, 8, serial.PARITY_NONE,
         serial.STOPBITS_ONE, xonxoff=0, rtscts=0, timeout=1)

#default COMMAND is bogus
COMMAND = "nope"
aspect169 = "kc 00 02n"
aspect43 = "kc 00 01n"
poweron = "ka 00 01n"
poweroff = "ka 00 00n"
inputdtv = "kb 00 00n"
inputav1 = "kb 00 02n"
inputcomp1 = "kb 00 04n"
inputpc = "kb 00 06n"

if sys.argv[1:] == ['--aspect169'] :
    COMMAND = aspect169

if sys.argv[1:] == ['--aspect43'] :
    COMMAND = aspect43

if sys.argv[1:] == ['--poweron'] :
    COMMAND = poweron

if sys.argv[1:] == ['--poweroff'] :
    COMMAND = poweroff

if sys.argv[1:] == ['--inputdtv'] :
    COMMAND = inputdtv

if sys.argv[1:] == ['--inputav1'] :
    COMMAND = inputav1

if sys.argv[1:] == ['--inputcomp1'] :
    COMMAND = inputcomp1

if sys.argv[1:] == ['--inputpc'] :
    COMMAND = inputpc

#This is when it actually writes the command to the serial port.
ser.write(COMMAND)

#all done now close the port.
ser.close()

The following is an example of using this script with LIRC, this is a small part of my lircrc file. This map the buttons that I use on my DVICO remote control.

begin
    remote = *
    button = live
    prog   = irexec
    config = tvcontrol --aspect169
end
begin
    remote = *
    button = folder
    prog   = irexec
    config = tvcontrol --aspect43
end
begin
    remote = *
    button = tv_onoff
    prog   = irexec
    config = tvcontrol --poweron
end
begin
    remote = *
    button = power_onoff
    prog   = irexec
    config = tvcontrol --poweroff
end
begin
    remote = *
    button = dtv
    prog   = irexec
    config = tvcontrol --inputdtv
end
begin
    remote = *
    button = mp3
    prog   = irexec
    config = tvcontrol --inputav1
end
begin
    remote = *
    button = dvd
    prog   = irexec
    config = tvcontrol --inputcomp
end
begin
    remote = *
    button = cpf
    prog   = irexec
    config = tvcontrol --inputpc
end

You mileage my vary, you will need to understand how your remote control functions and what the current button assignments are.

Another possiblity is to have the tvcontrol script run via a Cron entry or controlled via a webpage, use your mobile phone as a remote instead. The possiblities are endless. Have Fun.

10 thoughts on “RS232 (Serial) Control of LG LCD TV via MythTV

  1. I have a LG TV you and I have been trying to get the script to work. I noticed that the LG manual I have states that the data should be in hexadecimal, even the TV ID. Is that the case with your documentation?

  2. @Colin
    Is the cable you are using correct. For mine I had to make my own. I dont beleive the TV was using a standard cable. Also if you use a terminal connection rather then the script to connect to the TV do you get any response?

  3. Hiya

    Trying to run this script on my debian knoppmyth box, I get an error something like “python module serial not found”

    any idea how I can fix it ???

    regards

    Matt Shaw
    matt@shagshaw.com

  4. Hi Jon…

    Thanks for this nice script, helped me a lot on the way to control my lg 42LD4xx Display…

    However i had to modify a few things, some of the codes were different for my tv and although the command list is actually in the manual, its wrong or unusable,
    so here are my commands:

    aspect169 = “kc 00 02”
    aspect43 = “kc 00 01”
    poweron = “ka 00 01”
    poweroff = “ka 00 00”
    inputcable = “xb 00 10”
    inputav1 = “xb 00 20”
    inputav2 = “xb 00 21”
    inputhdmi = “xb 00 90”
    inputhdmi = “xb 00 91”

    i also added the sending of a carriage return at the end of each command:

    ser.write(COMMAND+’r’)

    instead of just (COMMAND)…

    works like a charm… love it!

    greetings
    hoshy

  5. Hi John,

    Thanks so much for this. I haven’t tried it with mythtv yet, but it works great just running tvcontrol –command. Below is my config for an NEC 42″ PX42VM5W

    #!/usr/bin/python

    # This was taken from http://www.thelazysysadmin.net/2009/05/rs232-control-lg-lcd-tv-mythtv/
    # The below commands control a NEC 42″ Plasma – PX42VM5W
    # The serial cable PC > Plasma needs to a ‘null’ modem cable
    # The NEC commands must be in HEX not ASCII, hence how the codes are formatted below
    # You will need the serial py package
    # Run “sudo apt-get install python-serial”
    # Follow the rest of the lazy admin’s instructions to make this work with LIRC
    # You will also have to set the tvcontrol script to be executable by
    # sudo chmod a+x /usr/bin/tvcontrol
    #
    # Scott Waller wallerscott72@gmail.com

    #allows you to capture the command switches
    import sys

    #get serial features for python
    import serial

    #this next line sets up the serial port to allow for communication
    #and opens the serial port you may need to change
    #ttyS0 to S1, S2, ect. The rest shouldn’t need to change.
    ser = serial.Serial(‘/dev/ttyUSB0’, 9600, 8, serial.PARITY_ODD,
    serial.STOPBITS_ONE, xonxoff=0, rtscts=0, timeout=1)

    #default COMMAND is bogus
    COMMAND = “nope”
    aspect169 = “xDFx80x60x51x01x05x16”
    aspect43 = “xDFx80x60x51x01x04x15”
    aspectzoom = “xDFx80x60x51x01x03x14”
    poweron = “x9Fx80x60x4Ex00xCD”
    poweroff = “x9Fx80x60x4Fx00xCE”
    # component RCA input
    component = “xDFx80x60x47x01x05x0C”

    dvi = “xDFx80x60x47x01x0Cx13”
    vga = “xDFx80x60x47x01x07x0E”
    svideo = “xDFx80x60x47x01x03x0A”
    # composite RCA input
    composite = “xDFx80x60x47x01x02x09”

    whiteon = “xDFx80x60x6Bx03x01x03x02x33”
    whiteoff = “xDFx80x60x6Bx03x01x02x02x32”

    if sys.argv[1:] == [‘–aspect169’] :
    COMMAND = aspect169

    if sys.argv[1:] == [‘–aspect43’] :
    COMMAND = aspect43

    if sys.argv[1:] == [‘–aspectzoom’] :
    COMMAND = aspectzoom

    if sys.argv[1:] == [‘–poweron’] :
    COMMAND = poweron

    if sys.argv[1:] == [‘–poweroff’] :
    COMMAND = poweroff

    if sys.argv[1:] == [‘–svideo’] :
    COMMAND = svideo

    if sys.argv[1:] == [‘–composite’] :
    COMMAND = composite

    if sys.argv[1:] == [‘–whiteon’] :
    COMMAND = whiteon

    if sys.argv[1:] == [‘–whiteoff’] :
    COMMAND = whiteoff

    if sys.argv[1:] == [‘–component’] :
    COMMAND = component

    if sys.argv[1:] == [‘–dvi’] :
    COMMAND = dvi

    if sys.argv[1:] == [‘–vga’] :
    COMMAND = vga

    #This is when it actually writes the command to the serial port.
    ser.write(COMMAND)

    #all done now close the port.
    ser.close()

  6. Hi Jon, I’ve expanded on your script and made a Python library out of it. I plan on sticking it up on Github, would you mind? (I’ll credit you and link to this blog post too, of course)

  7. Hi Suan,

    That would be great. The reason I put this stuff out there is for people to use and improve upon it.

    That is why I started this blog, Google and the Open Source community at large has always been very helpful (you can get a long way in this industry just by knowing how to search for something online, and do it quickly). So if I can give anything back then that is what I like to do.

    Cheers,

    Jon

  8. Well said! Here’s the library: https://github.com/suan/libLGTV_serial. It contains some features that you might useful, such as toggling between 2 commands with a single command (togglepower for example), and ignoring commands that get called many times in a row when a single button press is detected as many. Also could you kindly let me know if your TV model is not in the list of supported ones? Thanks!

  9. Greetings guys, I have a problem that is:
    I have a TV LG 47L330C at work and my boss wants from a RS-232 communication, I get in the lunch and dinner hours to tv connect to a television channel, outside of these hours you will need to open an application of omron in that this will be a kind of videos and slides.
    Appreciate a response to a future resolution of this problem.
    Thanks and a continuation of a good day.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.