1

This is my first post to stackoverflow, so please let me know if I don't follow the correct etiquette.

I am also new to Python and would like to incorporate it into a project. I currently have a Emotiv EEG headset and would like to run a homebuilt 3-D Printer from brain signals. To do so, the EEG headset is read by EmoKey 2.0.0.20 (see attached photo), EmoKey will then send keystrokes to the Python Shell, Python interprets this and sends commands to my Arduino run printer through a COM PORT. This may seem to be an indirect way of doing things, but it works except for one hitch. Lets pretend I think left, this is sent to EmoKey which types 'L' and the keystroke Enter into the Python Shell. In theory this would then move the printer head left. However, when EmoKey sends Enter it only creates a new line in the Shell, it doesn't actually execute. I then have to press enter by hand, which defeats the whole point.

.getch() hasn't worked because I don't think there is an actual key stroke for it to read. The link below also seems like it would be useful, but It hasn't worked thus far.

How to run a shell script without having to press enter/confirm s.th. inbetween

My question is: How can I get Python to execute what is written in the shell when only something like L or R is written? I don't think I can have it wait for a keystroke, Python would have to wait and automatically execute when it sees a specific command.

I understand that this may seem like a duplicate of the link below. However, .getch hasn't worked with EmoKey so far (maybe it's just a mistake on my part). Also, I wan't to find a way for my Python script to read what is put into the shell. Although I've started this project with just the "L" and "R" commands for simplicity and prototyping, I will move into using G-code so I can communicate with other printers or CNC equipment. That is another reason why .getch won't work in my case since it only grabs a single character (A single G-code command will be a few characters long). Python read a single character from the user

I'm using Windows 10 and Python 2.7.11.

import serial

ser = 0

def init_serial():
    COMNUM = 3          #Enter Your COM Port Number Here.
    global ser          #Must be declared in Each Function
    ser = serial.Serial()
    ser.baudrate = 9600
    ser.port = COMNUM - 1   #COM Port Name Start from 0

    #ser.port = '/dev/ttyUSB0' 

    #Specify the TimeOut in seconds, so that SerialPort
    #Doesn't hangs
    ser.timeout = 10
    ser.open()          #Opens SerialPort

    # print port open or closed
    if ser.isOpen():
        print 'Open:'  + ser.portstr

init_serial()

while 1: 
    temp = raw_input('Send command + Enter:\r\n')
    ser.write(temp)         #Writes to the SerialPort

    #bytes = ser.readline()  #Read from Serial Port
    #print 'Response: ' + bytes      #Print What is Read from Port

EmoKey Interface

Community
  • 1
  • 1
cabenso
  • 11
  • 2
  • Thank you for bringing this to my attention, I've changed my question to represent how I think it is different. Let me know if it needs more clarification. – cabenso Apr 22 '16 at 03:54

2 Answers2

1

I think you are looking for something like readchar:

import readchar
ch = readchar.readkey()
Ben
  • 2,422
  • 2
  • 16
  • 23
0

This definition is kind of clunky and unsatisfying, but I think it solves your problem without having to install any new packages. If I create a module defined in this link (I put it in a file called "getch.py"), we can check if the input key matches anything in a list of strings.

from getch import getch

while True:
    if getch() in ['l', 'r', 'L', 'R']:
        print('This was the key I was looking for!')

Technically you only need the section of the class I linked to that is relevant to your OS, but this example getch() function that I linked to is nice in that it is cross platform.

  • Thanks for the input. Maybe I'm just not getting it. I put in the exact code from Daniel Prince but change the (from getch import getch) to (from msvcrt import getch). Whenever I run it, nothing happens. If I pres "L" or "l", it doesn't get through the if statement. In theory, I should be able to write just an "L" into the shell and it will then go to 'This was the key I was looking for!', right? – cabenso Apr 21 '16 at 19:36
  • Really? On my Windows 8.1 installation, I was able to switch out the `getch` module that I linked to for `from msvcrt import getch` as you mentioned, ad this code snippet still works fine for me. – Daniel Prince Apr 22 '16 at 03:45