Psychopy 中的串口过载
Serial port overload in Psychopy
我正在使用通过串口连接的按钮盒进行 运行 Psychopy 实验。
from binascii import hexlify
import serial
import serial.tools.list_ports as port_list
ports = list(port_list.comports()) # search for the devices
#for p in ports: print (p)
ser = serial.Serial('/dev/ttyUSB0', 19200, bytesize=8, parity='N', stopbits=1, timeout=0)
if(ser.isOpen() == False): #open the serial port only if NOT open yet
ser.open()
ser.flush()
在实验中,我有4个套路。假设每个例程都包含一个书面文本,相应地 text_1
、text_2
、text_3
和 text_4
。例如,如果参与者在 text_1
和 he/she 中单击按钮,则实验移动到 text_2
。
但是,我面临以下问题。如果参与者在 text_1
中并快速按下按钮两次,实验将移动到 text_3
而不是 text_2
,就好像有东西在存储信息一样。相反,我想如果您在 text_1
中并且按下两次,则只考虑第一次按下。
每个text
代码都是这样
ser.flush()
for line in ser.read(1):
hex = hexlify(line)
num_1=int(hex, 16)
continueRoutine = False #this makes the experiment go to the next text
我可以添加什么来让它看起来像没有存储信息(如果这是真的发生的事情)?
串行端口通信是缓冲的,这意味着有消息的临时存储。如果参与者在试验中多次按下按钮框,则这些按下次数将存储在缓冲区中。当您从串口读取时,您将读取缓冲的消息。
为了解决您的问题,您可以在每次试用开始时清空串口缓冲区。使用 serial.Serial.reset_input_buffer()
方法来完成此操作。这是关于此方法的文档:
reset_input_buffer()
Flush input buffer, discarding all its contents.
有关缓冲通信的精彩类比,请参阅 :
Imagine that you're eating candy out of a bowl. You take one piece regularly. To prevent the bowl from running out, someone might refill the bowl before it gets empty, so that when you want to take another piece, there's candy in the bowl.
The bowl acts as a buffer between you and the candy bag.
我正在使用通过串口连接的按钮盒进行 运行 Psychopy 实验。
from binascii import hexlify
import serial
import serial.tools.list_ports as port_list
ports = list(port_list.comports()) # search for the devices
#for p in ports: print (p)
ser = serial.Serial('/dev/ttyUSB0', 19200, bytesize=8, parity='N', stopbits=1, timeout=0)
if(ser.isOpen() == False): #open the serial port only if NOT open yet
ser.open()
ser.flush()
在实验中,我有4个套路。假设每个例程都包含一个书面文本,相应地 text_1
、text_2
、text_3
和 text_4
。例如,如果参与者在 text_1
和 he/she 中单击按钮,则实验移动到 text_2
。
但是,我面临以下问题。如果参与者在 text_1
中并快速按下按钮两次,实验将移动到 text_3
而不是 text_2
,就好像有东西在存储信息一样。相反,我想如果您在 text_1
中并且按下两次,则只考虑第一次按下。
每个text
代码都是这样
ser.flush()
for line in ser.read(1):
hex = hexlify(line)
num_1=int(hex, 16)
continueRoutine = False #this makes the experiment go to the next text
我可以添加什么来让它看起来像没有存储信息(如果这是真的发生的事情)?
串行端口通信是缓冲的,这意味着有消息的临时存储。如果参与者在试验中多次按下按钮框,则这些按下次数将存储在缓冲区中。当您从串口读取时,您将读取缓冲的消息。
为了解决您的问题,您可以在每次试用开始时清空串口缓冲区。使用 serial.Serial.reset_input_buffer()
方法来完成此操作。这是关于此方法的文档:
reset_input_buffer()
Flush input buffer, discarding all its contents.
有关缓冲通信的精彩类比,请参阅 :
Imagine that you're eating candy out of a bowl. You take one piece regularly. To prevent the bowl from running out, someone might refill the bowl before it gets empty, so that when you want to take another piece, there's candy in the bowl.
The bowl acts as a buffer between you and the candy bag.