使用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

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L C H

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值