简体   繁体   English

python pyserial

[英]Python pyserial

I am triggering a device called olfactometer from python 2.7 anaconda spyder 64 bit. 我从python 2.7 anaconda spyder 64位触发了一个名为olfactometer的设备。 There are eight solenoid TTL valves in that olfactometer. 该嗅觉计中有八个电磁TTL阀。 If I want to switch the status of that valve I just have to write below-mentioned code. 如果要切换该阀的状态,只需编写以下代码。

import serial
import time

port = serial.Serial("COM3", 19200, timeout=0.5)
#turn on port 2, sleep 2 seconds, turn off port 2
port.write(b"\nF2\r")  # if opened then close port 2
time.sleep(2.0)
port.write(b"\nF2\r")  # if closed then open port 2


#close the port
port.close()

I would like to know, is it possible to give port 2 specific values from 0 or 1? 我想知道,是否可以为端口2提供0或1的特定值?

for example 例如

if 'e' in keypress: 
    # it must open the port 2
if 'i' in keypress:
    # it must close the port 2

What should I do to perform the test in an above-mentioned way? 我应该怎么做才能以上述方式进行测试? Thank you in advance! 先感谢您! - Ravi -拉维

One way you can capture input is with the raw_input built-in function ( input in Python3). 捕获输入的一种方法是使用raw_input内置函数(Python3中的input )。 For example 例如

port = serial.Serial("COM3", 19200, timeout=0.5)

prompt = 'Press "e" to open, "i" to close, and "q" to quit: '
keypress = raw_input(prompt)
while keypress != 'q':
    if keypress == 'e':
        port.write(b"\nF2\r")  # if closed then open port 2
        print('Opened')
    elif keypress == 'i':
        port.write(b"\nF2\r")  # if opened then close port 2
        print('Closed')
    # prompt again    
    keypress = raw_input(prompt)

port.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM