How to emulate a specific usb-to-serial converter - Page 1
Forum section: electronics / general

author: cv007
author_rank: Frequent Contributor
timestamp: October 06, 2016, 04:54:18 pm
content: I have a little circuit consisting of a pic16f1822, a mosfet, a switch, a coin cell battery and a db9f connector which plugs into an LG tv serial port. Press the button, tv turns on, sets volume, disables remote control, etc. Press and hold button, tv turns off. Press button while tv on, enable remote for 5 minutes. (You can pretty much do anything you want).LG tv's had serial ports for a long time for control, some have db9m, some 1/8in jack (tx,rx,gnd), some usb port only (host). Now it looks like they are dropping all the serial control ports except the usb port. The manual says its looking for a specific vid/pid pair for a usb-serial-adapter which uses a prolific pl2303 chip (the vid/pid matches a ATEN UC232a it seems). I want to be able to use the usb port like I did with the previous circuit for a tv which has only the usb port control.So, I fire up a pic16f1454, figure out how to get usb cdc working, change the vid/pid to match what the tv is looking for, test the chip connected to a linux pc- works ok, connected to a Win10 pc- works ok, but when connected to a LG tv, it says usb device not recognized. So I download the Windows drivers so I can look at the inf files so I can match the description strings in addition to the vid/pid. That doesn't help (although I didn't look at all versions- could be the descriptor strings in the inf files don't match the actual hardware strings).I can't imagine the tv is looking at anything more than the vid/pid, but it doesn't like something about my usb setup. Any ideas on how to make the tv think I have the specified serial adapter?For some reason, LG is looking for only a 20 year old usb-serial adapter, and yet about 1/5 of every user manual they have describes the control commands so they must thinks its a somewhat important feature. Sometimes I wonder what goes on at these companies.These specific usb-serial adapters are not that abundant, but a few can be found on ebay for about $15 (most everywhere else that has this adapter sells its for about $30). I guess I may have to buy one to experiment with if I can't figure this out.

author: andersm
author_rank: Super Contributor
timestamp: October 06, 2016, 05:00:34 pm
content: IIRC, the PL2303 is not a class-compliant device, so you'll have to implement its protocol. I'm guessing the TV is either expecting an exact match for the descriptors, or sending a PL2303-specific request that your firmware doesn't respond to.

author: JTR
author_rank: Regular Contributor
timestamp: October 06, 2016, 05:16:26 pm
content: Yeah, I think that is right. It's a vendor defined class, not standard CDC.

author: cv007
author_rank: Frequent Contributor
timestamp: October 06, 2016, 05:32:04 pm
content: That makes sense.They (ATEN) seem to have a driver source for linux athttp://www.aten.com/global/en/products/mobility-&-usb/usb-converters/uc232a/so if I take a look at what they are doing on the pc side, could I then figure out what I need on the device side?If that can't be done, would the next step be to get a real UC232a, observe what goes on over the bus (via wireshark or something), and go from there? I'm not sure I can go farther down the Microchip usb stack than I am already, but I guess I could learn more if required.

author: andersm
author_rank: Super Contributor
timestamp: October 06, 2016, 05:39:41 pm
content: There's also a driver for the PL2303 right in the Linux kernel.

author: cv007
author_rank: Frequent Contributor
timestamp: October 06, 2016, 06:05:29 pm
content: I wasn't thinking. I see that now, thanks.It does look like the needed info is there, I will try to figure out whats going on.Thanks for pointing me in the right direction.

author: cv007
author_rank: Frequent Contributor
timestamp: October 11, 2016, 04:19:42 pm
content: Good news, bad news.Good news- my self-taught crash course in usb has succeeded (read, try, test, repeat- sprinkled with a few actual moments of understanding) - I can 'emulate' a pl2303 enough to make linux happy, and an lg tv happy, but not a Windows 10 pc (big surprise) which I don't care. I am sending control commands to an lg tv just like before (when using rs232 ports).Bad news- although the previous rs232 ports were able to power on the tv via command, the current tv's usb ports are not functional when the tv is off, so you can't turn on the tv via external control. I'm sure it makes sense (power wise) to not keep power to the usb host controller (I assume a big chip which does everything else also) when the tv is off, but having a control scheme where you can't power on the device seems odd to me. You have complete control over the tv... except you can't turn it on.On to plan C, or D, or scrap all plans and move on.

author: abyrvalg
author_rank: Frequent Contributor
timestamp: October 13, 2016, 10:37:28 pm
content: Not surprised. Older TVs with true hw serial port have a separate always-on MCU to handle serial remote. But USB-serial requires USB host, host stack software etc - all these reside in the main SoC which turns off. You can try attaching an IR LED to your PIC and blinking a power-on sequence towards TV's IR receiver.And here is an USB analyzer trace of real PL2303 connect/open/transfer/close under Win 8.1 if you ever decide to make Windows driver happy too:http://rgho.st/6kBk99c44Use this sw to view:http://ellisys.com/products/download/visualusb.msi

author: cv007
author_rank: Frequent Contributor
timestamp: October 14, 2016, 05:17:34 am
content: I currently 'debug' the usb by sending info out the serial side of the pic (which in this case is unused)- so I can see the control/setup packets. I'm not certain I'm doing everything correctly, but I am seeing the setup packets are matching what is expected from looking at the linux pl2303 source code. I'll take a look at the analyzer info, thanks.The IR is about the only thing that can be done to power the tv on (besides the actual power button- which on newer models is located where you can actually find it without a flashlight and a magnifying glass). The only problems are that 1) I now have to come up with a power source, even though the smallest of coin cells would work, 2) worse, one of the reasons I want this is for a group of tv's that are located next to each other- and one of the commands used is to disable the remote - so when used as planned the remote will be disabled, so no power on via IR. (a short press on the device button will toggle the remote enable/disable - so a single tv can be accessed by remote when needed without affecting all other tv's ).These tv's can also be controlled via LAN, but there is no wake on lan support so basically same boat- can't turn on tv.I guess for now I'll end up just using the tv power button to turn on.

author: ron8k8
author_rank: Newbie
timestamp: January 23, 2017, 09:12:43 am
content: Hi cv007!I also have the same issue as you do, the old LG TV could be controlled from a PC using a generic RS232 adapter, while the new one requires the (expensive) ATEN adapter. My idea was basically the same as yours, but instead of the PIC I would be using an STM32. Of course only rewriting the USB VID/PIDs didn't work so I started investigating the Linux PL2303 driver to find out how to emulate it on the STM32. And that's where I stopped at the end of the weekendSeeing your post, I was wondering if you have a repo where your code for the PIC-based emulation is available? That would make my work a lot easier, since then I only need to port the PIC code to the STM32, without reverse engineering the PL2303 USB part.

author: cv007
author_rank: Frequent Contributor
timestamp: January 23, 2017, 05:32:25 pm
content: I have no repo, and you should probably thank meHere is some relevant code I found in my project-https://gist.github.com/anonymous/f9a13bd0dcdbc23d82f439a3c4c5e94fIt always takes me a while to decipher Microchips usb code, but I think the above code is the only relevant parts that are needed.This is the abreviated process I used, as I remember-I have a Ubuntu 16.xx pc that was used to test/troubleshoot before trying on the tvdownloaded pl2303hx datasheet, got usb descriptors to match datasheet (with ATEN's VID/PID )plugged in pic usb, looked at messages with dmesgfound out linux not happy talking to pic (driver goes through vendor specific initial read/write sequence)added vendor specific requests code linked aboveplugged in pic usb, linux is happy, no errors, thinks its a pl2303 (ATEN)set pic usb to debug output to serial (in my case I was not using the serial side, just using the pic to send commands via button press)hooked up to tv, watched debug output- initial sequence matched linuxsent commands- worked okrealized usb does not work when tv off (unlike rs232, cannot turn on if tv is off)project comes to screeching haltplan B, built a remote control-http://www.mtcnet.net/~henryvm/lgir3.png( limited range/directional because its used to control a single tv in a group of tv's, 2 buttons- can turn on, turn off, ch up, ch dn, vol up, vol dn)If I wanted to actually control an LG tv (as intended with their ports), I would consider ignoring them and just use IR- there will always be ir control, and I wouldn't count on the usb control feature to be around much longer (they only recognize a 20 year old usb/serial adapter- so obviously not considered an important feature by LG)

author: ron8k8
author_rank: Newbie
timestamp: January 24, 2017, 10:36:21 am
content: Thanks for the code, this looks very promising. Now I just have to figure out how to handle vendor specific requests in the STM32 USB API.For my application (media player integration) I absolutely need the advanced functions available through the serial port. One example is the video output on/off command, which turns off the screen but leaves the TV and the speakers on. I use my main TV for music listening as well (small apartment, thin walls, problems with the main amplifier, etc.) and the screensaver turns on after 10 minutes but the backlight is still on and wasting power (and reducing its own lifetime). The video output can be turned off from the menu, but it requires 5 or 6 keypresses (menu -> down -> left -> left -> up -> ok if I remember correctly) and there is no feedback if the sequence was performed or a step was skipped. The serial port control is just simply better for these kinds of tasks.Unfortunately the TV cannot be turned on this way, but this task can be performed using an IR LED on one of the outputs of the uC. Also it is possible to turn on (but not off) the TV using HDMI-CEC. I tested it using a Raspberry Pi running LibreELEC, it turns on the TV if SimpLink is enabled. Unfortunately it is a slow process, requiring 2-3 seconds for this operation but it might be sped up by specifying the TV ID (cec-client does some scanning on the CEC bus which takes up some extra time).Bottom line: LG made some weird decisions regarding the serial port of their TVs and the power management (USB power is off when the TV is off, HDMI is on all the time), but now I think there is a chance to solve this problem using a relatively simple piece of hardware (a Chinese STM32 board + IR LED). Will post if there is progress.

author: cv007
author_rank: Frequent Contributor
timestamp: January 24, 2017, 04:09:18 pm
content: Another thought- depending on your tv model, I think some of these with network ports also have the ability to be controlled by IP (I suspect same commands as serial/usb)- search for 'lg tv network control' or 'lg tv IP control'.