Có thể ai đó vui lòng cho tôi xem mã mẫu python đầy đủ sử dụng pyserial, tôi có gói và tự hỏi cách gửi lệnh AT và đọc lại chúng!Ví dụ đầy đủ về việc sử dụng gói pySerial
Trả lời
import serial
ser = serial.Serial(0) # open first serial port
print ser.portstr # check which port was really used
ser.write("hello") # write a string
ser.close() # close port
sử dụng http://pyserial.wiki.sourceforge.net/pySerial để biết thêm ví dụ
tôi đã không sử dụng pyserial nhưng dựa trên các tài liệu API tại http://pyserial.wiki.sourceforge.net/pySerial nó có vẻ như một giao diện rất đẹp. Nó có thể là giá trị gấp đôi kiểm tra các đặc điểm kỹ thuật cho các lệnh AT của thiết bị/radio/bất cứ điều gì bạn đang xử lý.
Cụ thể, một số yêu cầu một khoảng thời gian im lặng trước và/hoặc sau lệnh AT để tham gia vào chế độ lệnh. Tôi đã gặp phải một số mà không thích đọc của phản ứng mà không có một số chậm trễ đầu tiên.
Blog bài 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
Tôi nhận được một lỗi 'serial.serialutil.SerialException: Cổng đã mở' khi chạy mã này. Tôi không chắc chắn về điều này nhưng tôi tin rằng cổng nối tiếp được tự động mở ra khi nó được xác định rõ ràng như bạn đã làm với 'ser'. Sau khi bình luận ra dòng 'ser.open()' nó đã hoạt động. – user3817250
Nhận xét này là vị cứu tinh. –
@ user3817250: Hoặc chỉ cần thực hiện một trường hợp nếu xung quanh 'ser.open()' –
http://www.roman10.net/serial-port-communication-in-python/comment-page-1/#comment-1877
#!/usr/bin/python
import serial, time
#initialization and open the port
#possible timeout values:
# 1. None: wait forever, block call
# 2. 0: non-blocking mode, return immediately
# 3. x, x is bigger than 0, float allowed, timeout block call
ser = serial.Serial()
#ser.port = "/dev/ttyUSB0"
ser.port = "/dev/ttyUSB7"
#ser.port = "/dev/ttyS2"
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
#ser.timeout = None #block read
ser.timeout = 1 #non-block read
#ser.timeout = 2 #timeout block read
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 2 #timeout for write
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput()#flush output buffer, aborting current output
#and discard all that is in buffer
#write data
ser.write("AT+CSQ")
print("write data: AT+CSQ")
time.sleep(0.5) #give the serial port sometime to receive the data
numOfLines = 0
while True:
response = ser.readline()
print("read data: " + response)
numOfLines = numOfLines + 1
if (numOfLines >= 5):
break
ser.close()
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
- 1. Scala Monad - Ví dụ làm việc đầy đủ
- 2. Ví dụ về việc sử dụng Generics.Collections.TObjectDictionary
- 3. đăng bài yêu thích và ví dụ đầy đủ
- 4. Ví dụ về việc sử dụng LibSVM trong java
- 5. ví dụ về việc sử dụng scala.collection.immutable.Set từ java
- 6. Ví dụ về việc sử dụng AdditionalMetadata trong MVC 3
- 7. Ví dụ về trình phân tích cú pháp đầy đủ với parsec?
- 8. Ví dụ về cách sử dụng SetupDiGetDeviceProperty
- 9. Ví dụ về sử dụng cây Judy
- 10. Ví dụ về AES sử dụng Crypto ++
- 11. Ví dụ tốt về sử dụng libpurple?
- 12. Ví dụ về AIDL sử dụng
- 13. Ví dụ về cách sử dụng TLoginCredentialService
- 14. Đơn giản, nhưng đầy đủ tính năng, ứng dụng ví dụ Backbone?
- 15. Ví dụ về Winsock?
- 16. Ví dụ về Google Dart Forms làm việc
- 17. Ví dụ về DDD .NET
- 18. Sử dụng URL đầy đủ với Restangular
- 19. Ví dụ về pysmb
- 20. Ví dụ đầy đủ bằng cách sử dụng MVC2, EF4 và Mô hình Kho lưu trữ với True SoC
- 21. không đầy đủ loại lỗi về việc sử dụng tăng :: enable_shared_from_this
- 22. Tôi có thể tải xuống dự án java ví dụ đầy đủ sử dụng stringtemplate ở đâu?
- 23. _CrtCheckMẫu sử dụng ví dụ
- 24. Ví dụ về ứng dụng .net sử dụng Amazon SQS
- 25. ví dụ về ứng dụng sử dụng vaadin
- 26. Ví dụ trong việc sử dụng RedisStore trong socket.io
- 27. Ví dụ về D.ccurl
- 28. Ví dụ hữu ích nhất về sử dụng dired (Emacs)
- 29. Ví dụ tìm kiếm văn bản đầy đủ của SQLite trên iOS
- 30. Ví dụ về JGraphT
liên kết đó giờ đây đang bị phá vỡ. Cái mới: http://pythonhosted.org/pyserial/ – MarredCheese