Python?Flask框架實現(xiàn)Proteus仿真Arduino與網(wǎng)頁數(shù)據(jù)交互
實驗原理
模擬電腦通過串口與Arduino開發(fā)板通信,并通過網(wǎng)頁實現(xiàn)簡單交互
開發(fā)環(huán)境
1、Windows10
2、Python3.10
3、Proteus8.6
4、com0com虛擬串口工具
Flask虛擬環(huán)境
先安裝virtualenv:
pip install virtualenv
建立項目文件夾(比如demo_4)
在demo_04文件夾內(nèi),文件-打開powershell
建立虛擬環(huán)境venv
PS D:\code\flask\demo_04> virtualenv venv
demo_04文件夾里面會出現(xiàn)venv文件夾,后面安裝的python庫都裝在這個文件夾里面
激活虛擬環(huán)境:
PS D:\code\flask\demo_04> .\venv\Scripts\activate
在虛擬環(huán)境中安裝Flask和pyserial(python的串口庫)
(venv) PS D:\code\flask\demo_04> pip install Flask pyserial
如果下載慢,建議修改pip源為清華大學源(請同學們自行百度)
至此開發(fā)環(huán)境配置完畢
Python Flask源碼
文件目錄結(jié)構(gòu):
demo_04
-html
index.html
-static
-images
pic_bulboff.gif
pic_bulbon.gif
app.py
index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>test</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <script> $(document).ready(function(){ $("#button_open").click(function(){ $.get("http://127.0.0.1:5000/open",function(data,status){ alert("數(shù)據(jù): " + data + "\n狀態(tài): " + status); }); }); $("#button_close").click(function(){ $.get("http://127.0.0.1:5000/close",function(data,status){ alert("數(shù)據(jù): " + data + "\n狀態(tài): " + status); }); }); setInterval(function() { $.get("http://127.0.0.1:5000/get",function(data,status){ if (data == 1){ document.getElementById("myimage").src="static/images/pic_bulbon.gif"; } else{ document.getElementById("myimage").src="static/images/pic_bulboff.gif"; } }); }, 1000); }); </script> </head> <body> <h1>我的第一個標題</h1> <p>我的第一個段落。</p> <button id="button_open">打開串口COM2</button> <button id="button_close">關閉串口COM2</button> <img id="myimage" src="static/images/pic_bulboff.gif" width="100" height="180"> </body> </html>
app.py
import os import serial from flask import Flask from flask import send_from_directory app = Flask(__name__) root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "html")#html是個文件夾 @app.route('/') def home(): return send_from_directory(root, "index.html")#homepage.html在html文件夾下 @app.route('/open') def open_port(): global ser port = 'COM2' baudrate = 9600 # 設置波特率 timeout = 1 ser = serial.Serial(port, baudrate, timeout=timeout) return 'Serial Port open' @app.route('/close') def close_port(): ser.close() return 'Serial Port close' @app.route('/get') def read_port(): ser.reset_input_buffer() line = ser.readline() return line if __name__ == '__main__': app.run(debug = True)
用Proteus仿真Arduino
原理圖:
Arduino源碼:
void setup() { // put your setup code here, to run once: pinMode(0,INPUT); Serial.begin(9600); Serial.println("hello my friend!"); pinMode(2,INPUT); } void loop() { // put your main code here, to run repeatedly: if(digitalRead(2)==0) { Serial.println("0"); }else{ Serial.println("1"); } delay(500); }
用com0com建立虛擬串口對
安裝完com0com后,開始菜單打開Setup Command Prompt,運行下圖的命令即可創(chuàng)建一個虛擬串口對COM1和COM2(往COM1寫數(shù)據(jù),可以從COM2讀出來,反之亦然)
創(chuàng)建完后,設備管理器可以看到新增了串口設備:
本實驗中,Proteus中仿真的Arduino向串口COM1中寫數(shù)據(jù)(在Proteus中雙擊COMPIM控件,設置Physical port為COM1),Python代碼app.py從COM2讀取數(shù)據(jù),從而實現(xiàn)網(wǎng)頁和Arduino的數(shù)據(jù)交互。
運行程序
1、Proteus中點擊三角按鈕開始仿真,COMPIM控件上可以看到TXD管腳閃爍,說明在發(fā)送數(shù)據(jù)
2、powershell中運行app.py
(venv) PS D:\code\flask\demo_04> python .\app.py
打開瀏覽器地址http://127.0.0.1:5000 看到網(wǎng)頁:
點擊打開串口按鈕,然后在Proteus中切換SW1開關狀態(tài),可以看到網(wǎng)頁中燈泡照片變亮變暗
到此這篇關于Python Flask框架實現(xiàn)Proteus仿真Arduino與網(wǎng)頁數(shù)據(jù)交互的文章就介紹到這了,更多相關Proteus仿真Arduino與網(wǎng)頁交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
ubuntu16.04升級Python3.5到Python3.7的方法步驟
這篇文章主要介紹了ubuntu16.04升級Python3.5到Python3.7的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08解決pytorch 損失函數(shù)中輸入輸出不匹配的問題
這篇文章主要介紹了解決pytorch 損失函數(shù)中輸入輸出不匹配的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Python+wxPython實現(xiàn)一個簡單的音樂播放器
這篇文章主要為大家詳細介紹了如何使用Python編程語言和wxPython模塊創(chuàng)建一個簡單的音樂播放器,文中的示例代碼講解詳細,感興趣的可以了解下2023-09-09python logging 日志的級別調(diào)整方式
今天小編就為大家分享一篇python logging 日志的級別調(diào)整方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02