IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 使用Serial Studio+Python实现串口通信以及TCP通信并实现数据可视化 -> 正文阅读

[网络协议]使用Serial Studio+Python实现串口通信以及TCP通信并实现数据可视化

使用Serial Studio+Python实现串口通信以及TCP通信并实现数据可视化

Serial Studio下载方法与地址

下载地址:https://github.com/Serial-Studio/Serial-Studio/releases/latest
在该网页中选择与电脑所安装系统相同的版本文件下载即可

1. 使用Serial Studio+Python实现串口间通信

下载Launch Virtual Serial Port Driver Pro用于构建串口间的虚拟连接用于测试

下载地址:http://www.downza.cn/soft/335981.html

文件解压密码在下方。

  • 打开软件:添加串口对
    在这里插入图片描述

  • 打开Serial Studio
    在这里插入图片描述

  • 加载显示文件.json文件
    在这里插入图片描述
    在这里插入图片描述

  • 1.json文件中的内容

    {
       "frameEnd": "*/",
       "frameStart": "/*",
       "groups": [
           {
               "datasets": [
                   {
                       "alarm": 200,
                       "fft": false,
                       "fftSamples": 1024,
                       "graph": true,
                       "led": true,
                       "log": true,
                       "max": 20,
                       "min": 0,
                       "title": "AD",
                       "units": "V",
                       "value": "%1",
                       "widget": "gauge"
                   },
                   {
                       "alarm": 529,
                       "fft": false,
                       "fftSamples": 1024,
                       "graph": true,
                       "led": true,
                       "log": true,
                       "max": 600,
                       "min": 0,
                       "title": "DA",
                       "units": "V",
                       "value": "%2",
                       "widget": "bar"
                   },
                   {
                       "alarm": 360,
                       "fft": false,
                       "fftSamples": 1024,
                       "graph": true,
                       "led": true,
                       "log": true,
                       "max": 360,
                       "min": 0,
                       "title": "ADS",
                       "units": "",
                       "value": "%3",
                       "widget": "compass"
                   },
                   {
                       "alarm": 60,
                       "fft": true,
                       "fftSamples": 1024,
                       "graph": true,
                       "led": true,
                       "log": true,
                       "max": 60,
                       "min": 0,
                       "title": "DAS",
                       "units": "V",
                       "value": "%4",
                       "widget": "gauge"
                   }
               ],
               "title": "1",
               "widget": ""
           }
       ],
       "separator": ",",
       "title": "ADSS"
    }
    
    • 在Serial Studio软件中选择【串行端口——>COM端口】,选择相应的端口并设置好相应的波特率
      在这里插入图片描述
  • 编写与串口通信相关的Python文件

    import serial
    from time import sleep
    
    def recv(serial):
        while True:
            data = serial.read_all()
            if data == '':
                continue
            else:
                break
            sleep(0.02)
        return data
      
    if __name__ == '__main__':
        serial = serial.Serial('COM1',9600, timeout=0.5)  #/dev/ttyUSB0   
        if serial.isOpen() :
            print("open success")
        else :
            print("open failed")
        while True:
            str1 = input("请输入要发送到串口的话:")
            a=str1+"\n"
            #print(len(a))
            serial.write((a).encode("gbk"))
            sleep(0.1)
            data =recv(serial)
            if data != b'' :
                print("receive : ",data.decode("gbk"))
    
  • 先在Serious Studio选择【串行端口】按钮,点击连接,在运行python代码,输入想要输入的数据即可。
    在这里插入图片描述

    • 自动开启仪表盘并显示数据。
      在这里插入图片描述
      在这里插入图片描述

2.使用Serial Studio+Python实现TCP通信

  • 在Serial Studio软件中选择【网络——>插座类型】,选择相应的插座类型,这里选择TCP并填写远程地址与端口号.
    在这里插入图片描述
  • 编写与串口通信相关的Python文件
# python 3
# 服务端
import numpy as np
import socket
import threading
import json
import logging
 
TCP_PORT = 23

def tcplink(sock,addr):
    print('Accept new connection from %s:%s...' % addr)
    #sock.send( ('connect success!').encode() )
    # 客户端返回信息进行确认开始工作
    str_1='/*19,501,36.2,35*/'
    
    data = str_1.encode()
    sock.send(data)
   
def one_servicer():
    #Create The Socket
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
    #Listen The Port
    s.bind(('127.0.0.1',TCP_PORT))
    s.listen(5)
    print('Waiting for connection...')
    # 一旦监听到立即进入连接
    while True:
        # 开始一个新连接
        sock,addr=s.accept()        
        # 创建一个线程来处理连接
        t=threading.Thread(target=tcplink(sock,addr))
 
if __name__ == "__main__":
        one_servicer()
  • 先运行pyton代码,后点击连接按钮,在Serial Studio显示数据并开启仪表盘
    在这里插入图片描述
    在这里插入图片描述

3.陀螺仪界面显示(测试程序)

将1.json文件中的内容修改为:

{
   "frameEnd": "*/",
   "frameStart": "/*",
   "groups": [
       {
           "datasets": [
                {
                    "t":"X",               
                    "v":"%1",  
                    "w":"yaw"              
                 },
                 {
                    "t":"Y",               
                    "v":"%2", 
                    "w":"roll"             
                 },
                 {
                    "t":"Z",                  
                    "v":"%3", 
                    "w":"pitch"           
                 }
           ],
           "title": "Gyroscope",
           "widget": "gyro"
       }
   ],
   "separator": ",",
   "title": "Gyroscope"
}

点击运行TCP的python程序,则可在Serial Studio可视化文件中显示波形以及陀螺仪界面:
在这里插入图片描述
在这里插入图片描述

其他界面框显示可参照:

https://github.com/Serial-Studio/Serial-Studio/wiki/Introduction-to-widgets

4. 参考资料

https://github.com/Serial-Studio/Serial-Studio
https://github.com/Serial-Studio/Serial-Studio/wiki/Communication-Protocol

5.其他相关重要文档可参照Serial Studio官方文档

https://github.com/Serial-Studio/Serial-Studio/wiki/Communication-Protocol

https://github.com/Serial-Studio/Serial-Studio/wiki/Basic-usage-of-Serial-Studio

https://github.com/Serial-Studio/Serial-Studio/wiki/Introduction-to-widgets

https://github.com/Serial-Studio/Serial-Studio/wiki/JSON-definition-file-example

https://www.youtube.com/watch?v=I5jasWrsxT0

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-05-21 19:17:01  更:2022-05-21 19:18:01 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 9:10:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码