我正在面臨的問題,而我試圖從COM端口讀取數據我不知道是什麼問題..這裏是我正在使用的代碼通過com端口發送和接收數據。com端口連接到STM32板,它將數據發送到COM端口。我在要顯示的字符串末尾追加'\ n'。所以這裏是代碼如何通過使用pyserial一次讀取一行
import serial
ser.port = "COM4"
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.isOpen()
print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
while 1:
input = raw_input()
if input == 'exit':
ser.close()
exit()
else:
ser.write(input.encode('ascii')+'\r')
out = ''
while ser.inWaiting() > 0:
out += ser.readline()
print out
這是預期的輸出
Different commands offered are as follows:
''dis'' Displays contact list
''crt name number'' To create contact list
''del name'' To delete contact details
''edt existingName newName number'' To edit contact details
''clog'' To display calllog details
''cin'' Displays only incoming call details
''cout'' Displays only outgoing call details
''cmis'' Displays only missed call details
''rvc number'' To receive a call
''mkc number'' To make a call
''mkc name'' To call from contact list
''clogc number'' To call from calllog list
''cdel'' To delete callLog details
但我發現了這個 http://s15.postimg.org/sg9pvr20r/Untitled.jpg 對不起,我不能粘貼總產量所以我包括我的輸出的屏幕截圖..
你在哪裏打開你的連接? – Finwood 2015-04-04 08:56:21
這是直接的...配置後,我們可以通過使用ser.write() – 2015-04-06 06:06:32
寫入端口,當然,我只是期待一些'ser = serial.Serial()':-) – Finwood 2015-04-06 06:48:05