網站首頁 編程語言 正文
實驗原理
模擬電腦通過串口與Arduino開發板通信,并通過網頁實現簡單交互
開發環境
1、Windows10
2、Python3.10
3、Proteus8.6
4、com0com虛擬串口工具
Flask虛擬環境
先安裝virtualenv:
pip install virtualenv
建立項目文件夾(比如demo_4)
在demo_04文件夾內,文件-打開powershell
建立虛擬環境venv
PS D:\code\flask\demo_04> virtualenv venv
demo_04文件夾里面會出現venv文件夾,后面安裝的python庫都裝在這個文件夾里面
激活虛擬環境:
PS D:\code\flask\demo_04> .\venv\Scripts\activate
在虛擬環境中安裝Flask和pyserial(python的串口庫)
(venv) PS D:\code\flask\demo_04> pip install Flask pyserial
如果下載慢,建議修改pip源為清華大學源(請同學們自行百度)
至此開發環境配置完畢
Python Flask源碼
文件目錄結構:
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("數據: " + data + "\n狀態: " + status);
});
});
$("#button_close").click(function(){
$.get("http://127.0.0.1:5000/close",function(data,status){
alert("數據: " + data + "\n狀態: " + 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,運行下圖的命令即可創建一個虛擬串口對COM1和COM2(往COM1寫數據,可以從COM2讀出來,反之亦然)
創建完后,設備管理器可以看到新增了串口設備:
本實驗中,Proteus中仿真的Arduino向串口COM1中寫數據(在Proteus中雙擊COMPIM控件,設置Physical port為COM1),Python代碼app.py從COM2讀取數據,從而實現網頁和Arduino的數據交互。
運行程序
1、Proteus中點擊三角按鈕開始仿真,COMPIM控件上可以看到TXD管腳閃爍,說明在發送數據
2、powershell中運行app.py
(venv) PS D:\code\flask\demo_04> python .\app.py
打開瀏覽器地址http://127.0.0.1:5000 看到網頁:
點擊打開串口按鈕,然后在Proteus中切換SW1開關狀態,可以看到網頁中燈泡照片變亮變暗
原文鏈接:https://blog.csdn.net/weixin_41573966/article/details/127940225
相關推薦
- 2022-07-06 如何利用python創建、讀取和修改CSV數據文件_python
- 2022-08-22 .Net彈性和瞬態故障處理庫Polly實現執行策略_實用技巧
- 2024-01-05 TCP、IP、TCP/IP、HTTP和HTTPS協議簡介
- 2022-11-22 Kotlin?List與Set和Map實例講解_Android
- 2022-08-26 Python?Opencv中基礎的知識點_python
- 2022-11-06 詳解Python中的null是什么_python
- 2022-06-24 windows服務器修改遠程登錄的端口以及防火墻配置_win服務器
- 2022-06-15 golang中net的tcp服務使用_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支