gpt4 book ai didi

python - 使用 pySerial 包的完整示例

转载 作者:IT老高 更新时间:2023-10-28 21:09:14 27 4
gpt4 key购买 nike

谁能给我看一个使用 pyserial 的完整 python 示例代码,我有这个包,我想知道如何发送 AT 命令并读回它们!

最佳答案

博文Serial RS232 connections in Python

import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='/dev/ttyUSB1',
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS
)

ser.isOpen()

print 'Enter your commands below.\r\nInsert "exit" to leave the application.'

input=1
while 1 :
# get keyboard input
input = raw_input(">> ")
# Python 3 users
# input = input(">> ")
if input == 'exit':
ser.close()
exit()
else:
# send the character to the device
# (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)
ser.write(input + '\r\n')
out = ''
# let's wait one second before reading output (let's give device time to answer)
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(1)

if out != '':
print ">>" + out

关于python - 使用 pySerial 包的完整示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/676172/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com