使用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)
if serial.isOpen() :
print("open success")
else :
print("open failed")
while True:
str1 = input("请输入要发送到串口的话:")
a=str1+"\n"
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文件
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)
str_1='/*19,501,36.2,35*/'
data = str_1.encode()
sock.send(data)
def one_servicer():
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
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
|