【教學】樹莓派Raspberry PI(RASPBIAN)測試Console 115200功能








※本篇內文資訊參考如下※



- -

※更新文章日期: 2016-12-26

登入SSH PI
 撰寫測試文件
nano 1.py
寫入以下程式碼
——————-
import serial
import time
def readlineCR(port):
rv = “”
while True:
ch = port.read()
rv += ch
if ch==’\r’ or ch==”:
return rv
port = serial.Serial(“/dev/ttyAMA0”, baudrate=115200, timeout=3.0)
while True:
port.write(“\r\n123456”)
rcv = readlineCR(port)
port.write(“\r\nYou sent:” + repr(rcv))
 ————————————–
或是第二種
—————————————
import datetime
import os
import time
import serial
ser = serial.Serial(‘/dev/ttyAMA0’, 115200, timeout=1)
date = datetime.datetime.now().strftime( “%d/%m/%Y %H:%M.%s” )
ser.close()
ser.open()
print “Hit ‘Control C’ to halt this program”
ser.write(“Starting \n\r”)
try:
while 1:
date = datetime.datetime.now().strftime( “%d/%m/%Y %H:%M.%S” )
response = ser.readline()
# you can check your serial receive by uncommenting the above line
#print response
ser.write(“Testing………………………” + date +”\n\r”)
except KeyboardInterrupt:
ser.close()
—————————————————
ctrl+o存檔 ctrl+z離開
執行python 1.py
看你的console有沒有收到訊號
測試結果
 1427251888484










 1427252347583
後來發現空格會影響python
請下載文字檔進行測試
下載點








-

-