<p>这是我第一次发到stackoverflow,如果我没有遵守正确的礼仪,请告诉我。你知道吗</p>
<p>我对Python也是一个新手,希望将它合并到一个项目中。我目前有一个Emotiv脑电耳机,并想运行一个自制的3-D打印机从大脑信号。为此,EmoKey 2.0.0.20读取EEG耳机(见附图),EmoKey将向Python Shell发送击键,Python对此进行解释,并通过COM端口向我的Arduino run打印机发送命令。
这似乎是一种间接的做事方式,但除了一个小问题外,它是有效的。让我们假设我认为左,这是发送到EmoKey的类型'L'和按键进入Python Shell。理论上,这将使打印头向左移动。但是,当EmoKey发送Enter时,它只在Shell中创建新行,实际上并不执行。然后我必须用手按回车键,这就破坏了整个要点。你知道吗</p>
<p>.getch()不起作用,因为我认为它没有实际的按键笔划。下面的链接看起来也很有用,但到目前为止还没有成功。你知道吗</p>
<p><a href="https://stackoverflow.com/questions/27146064/how-to-run-a-shell-script-without-having-to-press-enter-confirm-s-th-inbetween">How to run a shell script without having to press enter/confirm s.th. inbetween</a></p>
<p>我的问题是:当只写了像L或R这样的东西时,如何让Python执行shell中写的东西?我不认为我可以让它等待击键,Python必须等待并在看到特定命令时自动执行。你知道吗</p>
<p>我知道这看起来像是下面链接的复制品。然而,.getch到目前为止还没有和EmoKey一起工作(也许这只是我的一个错误)。另外,我不想找到一种方法让我的Python脚本读取放入shell的内容。虽然我开始这个项目时只使用了“L”和“R”命令来简化和原型化,但我将开始使用G代码,这样我就可以与其他打印机或CNC设备进行通信。这就是.getch在我的例子中不起作用的另一个原因,因为它只获取一个字符(一个G代码命令只有几个字符长)。
<a href="https://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user">Python read a single character from the user</a></p>
<p>我使用的是windows10和python2.7.11。你知道吗</p>
<pre><code>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
</code></pre>
<p><a href="https://i.stack.imgur.com/woNBn.jpg" rel="nofollow noreferrer">EmoKey Interface</a></p>