Python?Flask框架實(shí)現(xiàn)Proteus仿真Arduino與網(wǎng)頁(yè)數(shù)據(jù)交互
實(shí)驗(yàn)原理
模擬電腦通過(guò)串口與Arduino開發(fā)板通信,并通過(guò)網(wǎng)頁(yè)實(shí)現(xiàn)簡(jiǎn)單交互
開發(fā)環(huán)境
1、Windows10
2、Python3.10
3、Proteus8.6
4、com0com虛擬串口工具
Flask虛擬環(huán)境
先安裝virtualenv:
pip install virtualenv
建立項(xiàng)目文件夾(比如demo_4)
在demo_04文件夾內(nèi),文件-打開powershell
建立虛擬環(huán)境venv
PS D:\code\flask\demo_04> virtualenv venv
demo_04文件夾里面會(huì)出現(xiàn)venv文件夾,后面安裝的python庫(kù)都裝在這個(gè)文件夾里面
激活虛擬環(huán)境:
PS D:\code\flask\demo_04> .\venv\Scripts\activate
在虛擬環(huán)境中安裝Flask和pyserial(python的串口庫(kù))
(venv) PS D:\code\flask\demo_04> pip install Flask pyserial
如果下載慢,建議修改pip源為清華大學(xué)源(請(qǐng)同學(xué)們自行百度)
至此開發(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>我的第一個(gè)標(biāo)題</h1> <p>我的第一個(gè)段落。</p> <button id="button_open">打開串口COM2</button> <button id="button_close">關(guān)閉串口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是個(gè)文件夾 @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 # 設(shè)置波特率 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建立虛擬串口對(duì)
安裝完com0com后,開始菜單打開Setup Command Prompt,運(yùn)行下圖的命令即可創(chuàng)建一個(gè)虛擬串口對(duì)COM1和COM2(往COM1寫數(shù)據(jù),可以從COM2讀出來(lái),反之亦然)
創(chuàng)建完后,設(shè)備管理器可以看到新增了串口設(shè)備:
本實(shí)驗(yàn)中,Proteus中仿真的Arduino向串口COM1中寫數(shù)據(jù)(在Proteus中雙擊COMPIM控件,設(shè)置Physical port為COM1),Python代碼app.py從COM2讀取數(shù)據(jù),從而實(shí)現(xiàn)網(wǎng)頁(yè)和Arduino的數(shù)據(jù)交互。
運(yùn)行程序
1、Proteus中點(diǎn)擊三角按鈕開始仿真,COMPIM控件上可以看到TXD管腳閃爍,說(shuō)明在發(fā)送數(shù)據(jù)
2、powershell中運(yùn)行app.py
(venv) PS D:\code\flask\demo_04> python .\app.py
打開瀏覽器地址http://127.0.0.1:5000 看到網(wǎng)頁(yè):
點(diǎn)擊打開串口按鈕,然后在Proteus中切換SW1開關(guān)狀態(tài),可以看到網(wǎng)頁(yè)中燈泡照片變亮變暗
到此這篇關(guān)于Python Flask框架實(shí)現(xiàn)Proteus仿真Arduino與網(wǎng)頁(yè)數(shù)據(jù)交互的文章就介紹到這了,更多相關(guān)Proteus仿真Arduino與網(wǎng)頁(yè)交互內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用python驗(yàn)證代理ip是否可用的實(shí)現(xiàn)方法
驗(yàn)證代理IP是否可用。原理是使用代理IP訪問(wèn)指定網(wǎng)站,如果返回狀態(tài)為200,表示這個(gè)代理是可以使用的。這篇文章重點(diǎn)給大家介紹使用python驗(yàn)證代理ip是否可用的實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2018-07-07Python使用zip合并相鄰列表項(xiàng)的方法示例
這篇文章主要介紹了Python使用zip合并相鄰列表項(xiàng)的方法,涉及zip、iter函數(shù)合并相鄰列表項(xiàng)、切片等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03ubuntu16.04升級(jí)Python3.5到Python3.7的方法步驟
這篇文章主要介紹了ubuntu16.04升級(jí)Python3.5到Python3.7的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08解決pytorch 損失函數(shù)中輸入輸出不匹配的問(wèn)題
這篇文章主要介紹了解決pytorch 損失函數(shù)中輸入輸出不匹配的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Python+wxPython實(shí)現(xiàn)一個(gè)簡(jiǎn)單的音樂(lè)播放器
這篇文章主要為大家詳細(xì)介紹了如何使用Python編程語(yǔ)言和wxPython模塊創(chuàng)建一個(gè)簡(jiǎn)單的音樂(lè)播放器,文中的示例代碼講解詳細(xì),感興趣的可以了解下2023-09-09Django權(quán)限機(jī)制實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了Django權(quán)限機(jī)制實(shí)現(xiàn)代碼詳解,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02python logging 日志的級(jí)別調(diào)整方式
今天小編就為大家分享一篇python logging 日志的級(jí)別調(diào)整方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02python字符串驗(yàn)證的幾種實(shí)現(xiàn)方法
字符串的驗(yàn)證是確保數(shù)據(jù)符合特定要求的關(guān)鍵步驟之一,本文主要介紹了python字符串驗(yàn)證的幾種實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07