Python Forum
Basic TCP to Pioneer VSX-1021 Stereo Receiver
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic TCP to Pioneer VSX-1021 Stereo Receiver
#1
Trying to send a command to my stereo. I can open a standard telnet session from command prompt in windows and simply connect using the IP and port 23 and then type PO which turns the power on and this work perfectly. I would like to have a way to automate this so that I can have a home automation program run the Python script to do this. I have never used Python, but it looks like the following should work: but I must have something wrong? Please help Smile

import telnetlib
import sys

HOST = "192.168.1.140"
VSXCommand = raw_input("PO")

Telnet.open(HOST{[23])
Telnet.write(VSXCommand)
Telnet.close()

I saved this as testpython.py and ran it and a quick window(which looks like a command prompt window) pops up and then disappears, but the receiver does not turn on. Thanks in advance for any help!
Reply
#2
Well, you have to create an Telnet object or use it directly.

import telnetlib

telnet = telnetlib.Telnet()

HOST = "192.168.1.140"
VSXCommand = raw_input("PO")

telnet.open(host)
telnet.write(VSXCommand)
telnet.close()
Why Python 2?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Wavic, Thank you VERY much for the reply! Python 2? I don't know, this is what I found online...just trying to piece something simple together to work. I tried your example, but it did not work either. I can create a simple autohotkey script and do it, but then I have to use sendkeys and I figure that may not be good in case the user is pressing other buttons on the touchscreen on the wall that controls the audio etc.
Reply
#4
(Oct-22-2017, 12:05 PM)Bpet Wrote: VSXCommand = raw_input("PO")

The 'raw_input' is a dead give away that the code was written in Python 2.x. As a new programmer, you should be using the latest version, which is currently 3.6.3. In the code you supplied, you'd only need to change  VSXCommand = raw_input("PO") to  VSXCommand = input("PO") .

By the way, here is the link to the docs: telnetlib (for version 3)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
You should run the script in the terminal window. There are telnet responses which should be printed on the screen and errors messages if there some. "Doesn't work" does not help us to get a picture of what is happening. Are you sure for example that the audio system listens on port 23? Are you sure that you have to send a string instead of bites?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
sparkz_alot - I'm using current version of Python, I had just copied and pasted from old scripts found online to create the script...tried removing the raw, but same outcome.

Wavic - Thanks again for the input. I ran it in a terminal window. It does not appear to me like the Telnet commands are getting processed. It immediately starts typing the PO on the screen and then I have to press the enter key to get it to continue. Then it indicates:
Traceback <most recent call last>:
File "...path to python Scripts folder\aaa.py", line 9, in <module> telnet.write<VSX Command>
File "C:\Program Files\Python36-32\lib\telnetlib.py", line 287, in write if IAC in buffer:
TypeError: 'in <string>' requires string as left operand, not bytes

Here is the current aaa.py script:
import telnetlib

telnet = telnetlib.Telnet()

HOST = "192.168.1.140"
VSXCommand = input("PO")

telnet.open(HOST)
telnet.write(VSXCommand)
telnet.close()

I know its port 23 because I can get it to work using a telnet session from the command prompt...what is strange though, is that if I type telnet from the command prompt and try to do an interactive start, it doesn't work...I have to type telnet 192.168.1.140 to start it off and it works...then I just type PO and the Stereo Receiver turns on and there is some feedback as to the status of the receiver in the telnet session...I press the control key and the ] and it exits and then I type quit and it ends the telnet session, then I type exit to close command prompt.

Thank you again for all of your help! I have probably spent 10 hours trying to figure this out :)
Reply
#7
I was able to get rid of the error messages, but it still does not turn on the stereo receiver? Here is the latest code:
import time
import telnetlib

HOST = "192.168.1.140"
VSXCommand = "PO"
telnet = telnetlib.Telnet("192.168.1.140", 23)
time.sleep(3)
telnet.write((VSXCommand + "\n").encode('ascii'))
telnet.close()

I started a new session manually with telnet 192.168.1.140 and then typed PO and it worked fine, so I know that it works with Telnet...not sure what I have wrong in the code?

Also tried without telnetlib and does not work?
import sys
import socket
import time

hostname = "192.168.1.140"
VSXCommand = "PO"
connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connect.connect((hostname, 23))
time.sleep(3)
connect.send((VSXCommand).encode('ascii'))
connect.send(("\n").encode('ascii'))
connect.close()
Reply
#8
Thanks sparkz_alot, did not know how to do that...fixed it :) Any suggestions on how to fix the code?

I was able to get rid of the error messages, but it still does not turn on the stereo receiver? Here is the latest code:
HOST = "192.168.1.140"
VSXCommand = "PO"
telnet = telnetlib.Telnet("192.168.1.140", 23)
time.sleep(3)
telnet.write((VSXCommand + "\n").encode('ascii'))
telnet.close()
I started a new session manually with telnet 192.168.1.140 and then typed PO and it worked fine, so I know that it works with Telnet...not sure what I have wrong in the code?

Also tried without telnetlib and does not work?

import sys
import socket
import time

hostname = "192.168.1.140"
VSXCommand = "PO"
connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connect.connect((hostname, 23))
time.sleep(3)
connect.send((VSXCommand).encode('ascii'))
connect.send(("\n").encode('ascii'))
connect.close()
Reply
#9
I've found this and you may find it interesting I think this script should work. Try it.
The command must ends with a new line  - Windows new line ( '\r\n' ).

import telnetlib
import time

host = "192.168.1.140"
port = 23
telnet = telnetlib.Telnet(host, port)

while True:
    command = input('Send: ') # raw_input for Python 2; exit for leaving the script

    if command.lower() == 'exit':
        break
    else:
        telnet.write("{}{}".format(command.upper().encode('ascii'), "\r\n")
        time.sleep(0.5)

telnet.close()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
Wavic - I tried that and could not get it to work, but I was probably just doing something wrong? Your comment about windows wanting \r\n, got me thinking and I started playing with my other script again. BOOM!! It works. THANK YOU SOOOO MUCH for helping me get this working! :) Also modified the code so I can simply pass the Pioneer Receiver command to the script. So I can run: phython.exe ControlPioneer.py "PO" and it turns the power on.

Here is the code:

import time
import telnetlib
import sys
 
HOST = "192.168.1.140"

telnet = telnetlib.Telnet(HOST, 23)

telnet.write((sys.argv[1] + "\r\n").encode('ascii'))

telnet.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Frames above 2000 bytes not acknowledged by client/receiver lukasz139 0 2,158 Aug-24-2018, 06:34 PM
Last Post: lukasz139

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020