How do i get the serial data from an arduino ?

This is the place for queries that don't fit in any of the other categories.

How do i get the serial data from an arduino ?

Postby jpvain » Sun Jul 28, 2013 10:40 am

Hi, I'm trying to create a python script for controlling Spotify with serial commands from Arduino. I'm using Pytify https://code.google.com/p/pytify/ as a base, and have modified its code, but still it doesnt work. Here is a link to my current code: http://pastebin.com/daUVnNNq
Here is the code
Code: Select all
import serial, time
import sys
import win32gui

class Spotify(object):
   
       
        APPCOMMAND      = 0x0319
 
        # Command IDs
        CMD_PLAYPAUSE   = 917504

        # Instance vars
        _hwnd            = None
        def __init__(self):
                try:
                        self._hwnd = win32gui.FindWindow("SpotifyMainWindow", None)
                except:
                        raise self.SpotifyWindowNotFoundException()
        def playpause(self):
                self._sendCommand(self.CMD_PLAYPAUSE)

        def focus(self):
                win32gui.ShowWindow(self._hwnd, 1)
                win32gui.SetForegroundWindow(self._hwnd)
                win32gui.SetFocus(self._hwnd)

        def _sendCommand(self, id):
                win32gui.SendMessage(self._hwnd, self.APPCOMMAND, 0, id)

COMMANDLINE_COMMANDS = {
                'playpause': Spotify.playpause
}
if __name__ == "__main__":
        def fail():
                print ("Usage: pytify.py "+"|".join(COMMANDLINE_COMMANDS.keys()))
                sys.exit(0)
        ser =serial.Serial('COM5', 9600)
        cmd_name = ser.readline()
        if not(cmd_name) in COMMANDLINE_COMMANDS: fail()
 
        cmd = COMMANDLINE_COMMANDS[cmd_name]
        print('hello')
        try:
                spotify = Spotify()
               
               
        except:
                #print "Spotify is not running"
                sys.exit(0)
 
        res = cmd(spotify)
        if res : print(res)
Last edited by Yoriz on Sun Jul 28, 2013 11:58 am, edited 2 times in total.
Reason: Added the pastebin code in code tags & changed the title
jpvain
 
Posts: 3
Joined: Sun Jul 28, 2013 10:37 am

Re: Python+Spotify control+Arduino

Postby Yoriz » Sun Jul 28, 2013 11:16 am

Please read this New Users, Read This
I've put your code into the post inside code tags for you, please add a traceback of the error you get or go into more detail as 'it doesnt work' doesn't give much to go on.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 565
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Python+Spotify control+Arduino

Postby jpvain » Sun Jul 28, 2013 11:38 am

So the problem is, that the program seems to make no errors but it doesnt get the serial data from the arduino, which I have tested writes to the serial console.
jpvain
 
Posts: 3
Joined: Sun Jul 28, 2013 10:37 am

Re: How do i get the serial data from an arduino ?

Postby jpvain » Sun Jul 28, 2013 8:41 pm

Got some progress, now the script understands the command, but I get some kind of an error I don't understand:

"Usage: pytify.py playpause|focus
Traceback (most recent call last):
File "H:\hello.py", line 41, in <module>
if not(cmd_name) in COMMANDLINE_COMMANDS: fail()
File "H:\hello.py", line 38, in fail
sys.exit(0)
SystemExit: 0


here is the changed code:
Code: Select all
import serial, time
import sys
import win32gui

class Spotify(object):
   
       
        APPCOMMAND      = 0x0319
 
        # Command IDs
        CMD_PLAYPAUSE   = 917504

        # Instance vars
        _hwnd            = None
        def __init__(self):
                try:
                        self._hwnd = win32gui.FindWindow("SpotifyMainWindow", None)
                except:
                        raise self.SpotifyWindowNotFoundException()
        def playpause(self):
                self._sendCommand(self.CMD_PLAYPAUSE)

        def focus(self):
                win32gui.ShowWindow(self._hwnd, 1)
                win32gui.SetForegroundWindow(self._hwnd)
                win32gui.SetFocus(self._hwnd)

        def _sendCommand(self, id):
                win32gui.SendMessage(self._hwnd, self.APPCOMMAND, 0, id)

COMMANDLINE_COMMANDS = {
                'playpause': Spotify.playpause,
                'focus': Spotify.focus
}
if __name__ == "__main__":
        def fail():
                print ("Usage: pytify.py "+"|".join(COMMANDLINE_COMMANDS.keys()))
                sys.exit(0)
        ser =serial.Serial('COM5', 9600)
        cmd_name = ser.read()
        if not(cmd_name) in COMMANDLINE_COMMANDS: fail()
 
        cmd = COMMANDLINE_COMMANDS[cmd_name]
        try:
                spotify = Spotify()
               
               
        except:
                #print "Spotify is not running"
                sys.exit(0)
 
        res = cmd(spotify)
        if res : print(res)
jpvain
 
Posts: 3
Joined: Sun Jul 28, 2013 10:37 am

Re: How do i get the serial data from an arduino ?

Postby Yoriz » Sun Jul 28, 2013 11:01 pm

Did you write any of the code yourself, if you had im sure you would have an understanding of what it is doing,
Its being told to exit due to the command name given is not in your dictionary.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 565
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests