python虚拟串口_python 虚拟串口通信
准备着手写一个串口工具,当前编程环境,并没有办法接好下位机平台,需要模拟出一个串口,不断发送信息,方便调试串口工具。
1.win 下载虚拟串口
https://sourceforge.net/projects/com0com/files/latest/download
下载安装完成后,会在设备管理器中新增两个com。
这两个串口是互相接通的,一个com 可以当作上位机,一个com可以当作下位机
2.python 实现向一个串口内写数据,另一个串口内接受数据
#!/usr/bin/python
# -*-coding: utf-8 -*-
import serial
import serial.tools.list_ports
import threading
import time
from datetime import datetime
#列出所有当前的com口
port_list = list(serial.tools.list_ports.comports())
port_list_name = []
class SerialPort:
def __init__(self,port,buand):
self.port = serial.Serial(port,buand)
self.port.close()
if not self.port.isOpen():
self.port.open()
def port_open(self):
if not self.port.isOpen():
self.port.open()
def port_close(self):
self.port.close()
def send_data(self):
#date = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
while True:
date = datetime.now().strftime('%H:%M:%S.%f')[:-3]
print("send:",date)
self.port.write(date.encode())
time.sleep(1)
def read_data(self):
while True:
count = self.port.inWaiting()
if count > 0:
rec_str = self.port.read(count)
print("receive:",rec_str.decode())
def show_all_com():
if len(port_list) <= 0:
print("the serial port can't find!")
else:
for itms in port_list:
port_list_name.append(itms.device)
if __name__ == '__main__':
baunRate = 115200
print("1.list all com")
show_all_com()
print(port_list_name)
print("2.open write port ",port_list_name[0])
serialPort_w = port_list_name[0]
mSerial_w = SerialPort(serialPort_w,baunRate)
print("3.start write thread")
t1 = threading.Thread(target=mSerial_w.send_data)
t1.setDaemon(True)
t1.start()
print("4.open read port ",port_list_name[1])
serialPort_r = port_list_name[1]
mSerial_r = SerialPort(serialPort_r,baunRate)
print("5.start read thread")
t2 = threading.Thread(target=mSerial_r.read_data)
t2.setDaemon(True)
t2.start()
#do something else, make main thread alive there
while True:
time.sleep(10)
代码流程:
启动一个子线程,打开com3 ,将当前时间作为数据,写入到串口
启动另一个子线程,打开com4,读取串口中写入的数据
注意点:
1.串口中写入的数据为byte类型,str to byte 需要 encode(), 默认使用utf-8
2.从串口中读取的数据,也是byte类型,byte to str 需要decode(), 默认使用utf-8
3.虚拟串口是两个设备直接的通信 ,需要在com3 中写 ,在com4中读,笔者犯过一个低级错误,com3写,也在com3中读,不会出任何数据
4.子线程设置为守护线程,主线程退出,子线程随之退出,后续可以在主线程中处理读取到的数据
运行结果
原文链接:https://blog.csdn.net/cai472861/article/details/105888584
python虚拟串口_python 虚拟串口通信相关推荐
- 利用python程序、虚拟串口软件、串口通信助手,实现串口通信(两串口互相收发文字)
一.流程简述 程序可以参考: https://blog.csdn.net/colcloud/article/details/42454839 这是我参考后简化的版本: https://github.c ...
- 使用keil,proteus,虚拟串口,完成蓝牙通信
目的: 通过HC-06的蓝牙芯片,AT89C51的51单片机,借助keil,proteus,通过虚拟串口,在电脑上实现蓝牙串口通信,控制LED的开和关. 存在的问题: 单片机和蓝牙不能联动,暂未找出问 ...
- c# 蓝牙虚拟串口_C# 虚拟串口通信
将主端口COM8拆分成 COM1和COM2两个虚拟端口 COM8接收的消息会传递给COM1和COM2 SerialPort spSend;//spSend,spReceive用虚拟串口连接,它们之间可 ...
- 虚拟串口及其在串口转以太网中的应用
本文介绍虚拟串口的概念,以及如何在串口转以太网中利用该技术. 1.虚拟串口的概念 虚拟串口是用操作系统的虚拟驱动技术产生的串口(COM口),相对于计算机本身的硬件串口(COM1等)来说虚拟串口并不对应 ...
- Keil仿真基于虚拟串口VSPD的串口调试(Virtual Serial Port Driver)
1.添加虚拟串口对 2.Keil 启动仿真 3.打开command窗口 在命令行输入: MODE COM1 115200,0,8,1 ASSIGN COM1<S1IN>S1OUT //单片 ...
- linux 控制虚拟串口,linux虚拟串口控制器实现-适用于无开发板学习tty driver-好向圈...
在前面几章,我们介绍了tty子系统的框架.数据结构.tty驱动的注册与注销等内容,本章我们借助 之前学习的内容开发一个虚拟的串口控制器驱动,以便我们理解开发串口驱动的步骤及驱动开发实践. 本次实现的虚 ...
- 英伟达Jeston nano<3>使用Python实现三种方式串口通信
英伟达Jeston nano<3>使用Python实现三种方式串口通信 前言 通信配置过程 基本信息 配置过程 方案一:Python-40pinEXpansion Header 方案二:P ...
- python实现自动工作_python实现串口自动触发工作的示例
最近在一个python工具中需要实现串口自动触发工作的功能,之前只在winform上面实现,今天使用python试试.这里简单记一下: 首先用wxpython实现一个Button,点击事件绑定函数On ...
- python串口操作_Python Serial串口基本操作(收发数据)
1.需要模块以及测试工具 模块名:pyserial 使用命令下载:python -m pip install pyserial 串口调试工具:sscom5.13.1.exe 2.导入模块 import ...
- matlab制作以太网数据接收上位机_Python制作串口通讯上位机
串口通讯具有简单易用的特点广泛应用于测试设备的通讯和数据传递.单片机与计算机的通讯等,本案例基于Python语言制作一个用于接收燃油质量流量计的串口通讯上位机,实现数据的读取和保存. 1. 相关知识点 ...
最新文章
- Windows API获取系统配置文件的配置参数
- find = in a string
- 案例 | 用pdpipe搭建pandas数据分析流水线
- facebook_如何清除您的Facebook搜索记录
- c语言inline不起作用,C语言inline内联函数学习小结
- android 获取当前输入法,关于获取安卓手机系统输入法和设置输入法
- linux救援模式下卸载根目录,删除Linux的依赖库并进入救援模式恢复
- SilverLight跨域访问及其常用的几种解决方法
- 39.伪造重定向ICMP数据包
- 纯真数据库mysql_纯真数据库下载或自动更新实现
- 不想使用Windows自带远程桌面客户端的另外一种选择
- [转]密码算法揭秘,一文让你成为国际安全算法与国密算法专家
- 去了一趟少林寺 竟然发现...
- Contest2257 - 抗击疫情,从我做起--大中小学生联合训练赛第五十二场
- unicode转中文 中文转unicode的简单方式
- codeforces 645F Cowslip Collections
- WatchGuard Firebox配置动态口令(OTP)认证
- 瑞华吉瑞保重大疾病保险怎么样?好不好
- js设置弹出式独立窗口页面,和 window 的方法
- 到墨西哥的液体货物国际快递怎么邮寄
热门文章
- android电视APP开机自启动,安卓电视、机顶盒如何开机自动启动看电视直播-今日头条...
- 虚幻C++入门个人笔记(4)——UMG、网络
- 【数据分析与可视化】Python的input和output
- 钉钉机器人+Mac定时Launchctl
- html+css实现静态小米商城界面的学习感受与收获
- MATLAB 8.1 R2013a license.lic 问题
- 如何在CSS中解决长英文单词的页面显示问题?CSS3
- 模拟点击事件php,利用js脚本模拟鼠标点击事件
- Ubuntu20.4 ping baudu.com contect: 网络不可达
- golang学习(二)—— 变量