Compare commits

...
15 Commits
7 changed files with 1069 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2015 Benjamin Larsson <benjamin@southpole.se>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
from .pd import Decoder
+322
View File
@@ -0,0 +1,322 @@
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2015 Benjamin Larsson <benjamin@southpole.se>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
import sigrokdecode as srd
class SamplerateError(Exception):
pass
class Decoder(srd.Decoder):
api_version = 3
id = 'SL2-100'
name = 'SL2-100'
longname = 'SL2-100'
desc = 'SL2-100振镜协议 by chenyue.'
license = 'gplv2+'
inputs = ['logic']
outputs = []
tags = ['IC', 'RFID']
# 必须要绑定的通道定义,将在界面上可见
# id:通道标识, 任意命名
# type:类型,根据需要设置一个值, -1:COMMON,0:SCLK,1:SDATA,2:ADATA
# name:标签名
# desc:该通道的说明
# 注意元组的最后的逗号不能少
channels = (
{'id': 'data', 'name': 'Data', 'desc': 'Data line'},
)
# 提供给用户通过界面设置的参数,根据业务需要来定义
options = (
# 一个数据周期10us
{'id': 'datatime', 'desc': '数据传输时间(ns)', 'default': 10000},
{'id': 'filename', 'desc': '解码输出文件名','default':'d:/sl2-100.csv'},
{'id': 'invert', 'desc': 'Invert Signal?', 'default': 'no','values': ('yes', 'no'), 'idn':'opt_invert'},
)
# 解析结果项定义
# annotations里的每一项可以有2到3个属性,当有3个属性时,第一个表示类型
# 类型对应0-16个颜色,当类型范围在200-299时,将绘制边沿箭头
annotations = (
('highlow', '电平'),
('bit', '数据位'),
('header', 'Header'),
('xpos', 'x坐标'),
('xpos_value', 'x坐标值'),
('xv', 'x坐标效验'),
('header2', 'header2'),
('ypos', 'y坐标'),
('ypos_value', 'y坐标值'),
('yv', 'y坐标效验'),
)
# 解析结果行定义
annotation_rows = (
# (0,)表示可输出第1个定义的annotations类型
('level', '电平', (0,)),
('bits', '数据位', (1,)),
# (2,3,4,5,6,7)表示可输出第2个到第7个定义的annotations类型
('fields', '字段', (2, 3, 5, 6, 7, 9)),
('xpos', 'x坐标值', (4,)),
('ypos', 'y坐标值', (8,)),
)
# 构造函数,自动被调用
def __init__(self):
self.reset()
# 重置函数,在这里做一些重置和定义类私有变量工作
def reset(self):
self.samplerate = None
self.bit_width = 0 #采样次数,400Mhz时计算出来应该是31.25
self.oldsamplenum = 0.0 #采样起始位置
self.ss_first = 0 #当前数据位起始位置
self.first_one = -1 #当前数据位的首电平
self.header_str = ""
self.head_cnt = 0 #hedaer累积的数据位个数
self.header_first = -1 #header起始位置
self.xpos_cnt = 0 #x坐标累积的数据位个数
self.xpos_first = 0 #x坐标起始位置
self.xv_str = ""
self.xv_cnt = 0 #x坐标校验累积的数据位个数
self.xv_first = 0 #x坐标校验起始位置
self.header2_str = ""
self.head2_cnt = 0 #hedaer2累积的数据位个数
self.header2_first = 0 #header2起始位置
self.ypos_cnt = 0 #y坐标累积的数据位个数
self.ypos_first = 0 #y坐标起始位置
self.yv_str = ""
self.yv_cnt = 0 #y坐标校验累积的数据位个数
self.yv_first = 0 #y坐标校验起始位置
self.state = 'HEADER' #当前处理的字段
self.state2 = 'FIND START' #当前处理的字段
self.data = 0 #存放坐标值数据
self.highpin = 0 #当前采样宽度(78.125ns)内高电平数量
self.lowpin = 0 #当前采样宽度(78.125ns)内低电平数量
self.filename = "d:/sl2-100.csv"
self.file = None
self.x_value = 0
def metadata(self, key, value):
if key == srd.SRD_CONF_SAMPLERATE:
self.samplerate = value
#每个电平采样次数,400Mhz时计算出来应该是31.25 = 0.4 * 78.125 = (采样次数/纳秒 * 持续时长/电平)
self.bit_width = (self.samplerate / (1000*1000*1000)) * (self.options['datatime'] / 128)
self.filename = self.options['filename']
# 开始执行解码任务时,由c底层代码自动调用一次
# 这里,完成一些解码结果项annotation类型的注册
# 类型有: OUTPUT_ANN,OUTPUT_PYTHON,OUTPUT_BINARY,OUTPUT_META
# self.register函数是c底层类提供的
def start(self):
self.out_ann = self.register(srd.OUTPUT_ANN)
#数据位处理函数 bit表示当前数据位1还是0 ss表示当前数据位起始位置 es表示当前数据位结束位置
def putbit(self, bit, ss, es):
#标记当前数据位
self.put(ss, es, self.out_ann,[1, [str(bit)]])
#下面处理每个字段
if self.state == 'HEADER':
#self.file.write(self.state+ '\n')
if(self.head_cnt == 0 and bit == 0):# 如果是第一个数据位且为0,则认为是y坐标数据
self.header2_first=self.header_first
self.header2_str += str(bit)
self.head2_cnt = self.head2_cnt+1
self.state = 'HEADER3'
else:
self.header_str += str(bit)
self.head_cnt = self.head_cnt+1
if self.head_cnt == 6:
self.put(self.header_first, es, self.out_ann,[2, ['HEADER_x:' + self.header_str]])
self.state = 'xpos'
self.xpos_first = es
self.xpos_cnt = 0 #当前坐标 位数归零,准备累积
self.data = 0 #当前坐标值归零,准备开始累积
elif self.state == 'xpos':
#self.file.write(self.state+ '\n')
if self.xpos_cnt == 19:
self.data = ((not bit) << self.xpos_cnt) | self.data
else:
self.data = (bit << self.xpos_cnt) | self.data
self.xpos_cnt = self.xpos_cnt+1
if self.xpos_cnt == 20:
self.put(self.xpos_first, es, self.out_ann,[3, ['X坐标:' + ': 0x%x' % self.data + ' = %d' % self.data]])
self.put(self.xpos_first, es, self.out_ann,[4, ['%d' % self.data]])
self.state = 'xpos_v'
self.xv_first = es
self.xv_cnt = 0
self.xv_str = ""
self.x_value=self.data
elif self.state == 'xpos_v':
self.xv_str += str(bit)
self.xv_cnt = self.xv_cnt+1
if self.xv_cnt == 4:
self.put(self.xv_first, es, self.out_ann,[5, ['X校验:'+ self.xv_str]])
self.state = 'HEADER2'
self.header2_first = es
self.head2_cnt = 0
self.header2_str = ""
elif self.state == 'HEADER2':
self.header2_str += str(bit)
self.head2_cnt = self.head2_cnt+1
if self.head2_cnt == 8:
self.put(self.header2_first, es, self.out_ann,[6, ['HEADER_y:' + self.header2_str]])
self.state = 'ypos'
self.ypos_first = es
self.ypos_cnt = 0 #当前坐标 位数归零,准备累积
self.data = 0 #当前坐标值归零,准备开始累积
self.head2_cnt = 0
elif self.state == 'HEADER3':
#self.file.write(self.state+ '\n')
self.header2_str += str(bit)
self.head2_cnt = self.head2_cnt+1
if self.head2_cnt == 6:
self.put(self.header2_first, es, self.out_ann,[6, ['HEADER_y:' + self.header2_str]])
self.state = 'ypos'
self.ypos_first = es
self.ypos_cnt = 0 #当前坐标 位数归零,准备累积
self.data = 0 #当前坐标值归零,准备开始累积
self.head2_cnt = 0
elif self.state == 'ypos':
if self.ypos_cnt == 19:
self.data = ((not bit) << self.ypos_cnt) | self.data
else:
self.data = (bit << self.ypos_cnt) | self.data
self.ypos_cnt = self.ypos_cnt+1
if self.ypos_cnt == 20:
self.put(self.ypos_first, es, self.out_ann,[7, ['y坐标:' + ': 0x%x' % self.data + ' = %d' % self.data]])
self.put(self.ypos_first, es, self.out_ann,[8, [ '%d' % self.data]])
self.state = 'ypos_v'
self.yv_first = es
self.yv_cnt = 0
self.yv_str = ""
self.file.write( '%d' % self.x_value + ',' + '%d' % self.data + '\n')
elif self.state == 'ypos_v':
self.yv_str += str(bit)
self.yv_cnt = self.yv_cnt+1
if self.yv_cnt == 4:
self.put(self.yv_first, es, self.out_ann,[9, ['y校验:' + self.yv_str]])
self.state = 'HEADER'
self.state2 = 'FIND START'
self.header_first = es
self.head_cnt = 0
self.header_str = ""
#差分曼彻斯特解码函数 pin表示当前电平是高(1)还是低(0)
def manchester_decode(self, ss,es,pin):
#标记电平 高/低
self.put(ss, es, self.out_ann, [0, ['高' if pin==1 else'低']])
#记录HEADER起始位置 第一次需要
if self.header_first == -1:
self.header_first = ss
#下面处理数据位
if self.first_one == -1: #处理数据位的首电平
self.first_one = pin
self.ss_first = ss #记录当前数据首电平起始位置
return
else: #处理数据位的第二电平
if self.first_one != pin: #有跳变 输出1
self.putbit(1, self.ss_first, es)
else: #无跳变 输出0
self.putbit(0, self.ss_first, es)
self.first_one = -1 #重置first_one标志,准备下一数据位的处理
# 解码函数,解码任务开始时由c底层代码调用
# 这里不断循环等待所有采样数据被处理完成
# 下面的示例代码是解析某一通道的数据,从向上边沿开始到向下边沿结束,输出它们的样品位置差值,
# 奇数次显示第二行,偶数次显示在第一行,我们只指定annotations里定义的序号
# 软件会自动根据annotation_rows的设置,决定显示在哪一行
def decode(self):
if not self.samplerate:
raise SamplerateError('Cannot decode without samplerate.')
if self.filename != "":
self.file = open(self.filename,'a')
self.file.write('x坐标,y坐标\n')
# Initialize internal state from the very first sample.
(pin,) = self.wait()
if self.oldsamplenum == 0:
self.oldsamplenum = self.samplenum
self.highpin = 0 #当前采样宽度(78.125ns)内高电平数量
self.lowpin = 0 #当前采样宽度(78.125ns)内低电平数量
#当前电平值是否取反
inv = self.options['invert'] == 'yes'
last_samplenum=0
while True:
if self.state2 == 'FIND START':
#self.file.write(self.state2+ '\n')
if inv:
(pin,) = self.wait({0:'h'})
else:
(pin,) = self.wait({0:'l'})
self.oldsamplenum = self.samplenum
#self.file.write('oldsamplenum= %.1f' % self.oldsamplenum+ '\n')
ss = self.samplenum
self.state2 = 'FIND START2'
if self.state2 == 'FIND START2':
#self.file.write(self.state2+ '\n')
(pin,) = self.wait({0:'e'})
start_width = self.samplenum - self.oldsamplenum
self.oldsamplenum = self.samplenum
#self.file.write('oldsamplenum= %.1f' % self.oldsamplenum+ '\n')
if (start_width >= self.bit_width*2.2) :
self.state2 = 'FIND START3'
else:
self.state2 = 'FIND START'
if self.state2 == 'FIND START3':
#self.file.write(self.state2+ '\n')
(pin,) = self.wait()
#当前已经累积的采样宽度
total_width = self.samplenum - self.oldsamplenum
if total_width>=self.bit_width-1 : #累积采样宽度接近设置的采样次数
self.state2 = 'FIND DATA'
es=self.samplenum
self.put(ss, es, self.out_ann, [0, ['header']])
self.oldsamplenum = self.oldsamplenum+self.bit_width-1
#self.file.write('oldsamplenum= %.1f' % self.oldsamplenum+ '\n')
last_samplenum = self.samplenum
self.highpin = pin #当前采样宽度(78.125ns)内高电平数量
self.lowpin = not pin #当前采样宽度(78.125ns)内低电平数量
if self.state2 == 'FIND DATA':
#self.file.write(self.state2+ '\n')
(pin,) = self.wait()
#当前已经累积的采样宽度
total_width = self.samplenum - self.oldsamplenum
#self.file.write('total_width= %.1f pin= %d' % (total_width, pin)+ '\n')
if pin==1:
self.highpin += 1 #当前采样宽度内高电平数量加1
else:
self.lowpin += 1 #当前采样宽度内低电平数量加1
if total_width>=self.bit_width : #累积采样宽度接近设置的采样次数
self.oldsamplenum = self.oldsamplenum+self.bit_width #当前采样结束作为下一采样的开始
#self.file.write('oldsamplenum= %.1f' % self.oldsamplenum+ '\n')
#当前电平起始位置
ss = last_samplenum
#当前电平结束位置
es = self.samplenum
last_samplenum= self.samplenum
self.manchester_decode(ss, es, 1 if self.highpin > 2 else 0)
self.highpin = pin #当前采样宽度(78.125ns)内高电平数量
self.lowpin = not pin #当前采样宽度(78.125ns)内低电平数量
# self.wait()可带参数,也可以不带参数,不带参数时将返回每个采样数据
# 参数{0:'r'}, 0表示匹配channels第1项绑定的通道,'r'表示查找向上边沿
# wait函数可传多个条件,与条件:{0:'f',1:'r'}, 或条件:[{0:'f'},{1:'r'}]
# h:高电平,l:低电平,r:向上边沿,f:向下边沿,e:向上沿或向下沿, n:要么0,要么1
# wait函数前的变量(a,b),对应的数量由定义的channels里的通道数决定,包括可选通道
# optional_channels 。例如:channels和optional_channels共定义了4个通道,
# 则变成(a,b,c,d) = self.wait(),共四个变量
# 底层模块提供的属性:
# 1. self.samplenum 当前wait()调用匹配结束的采样点位置
# 2. self.matched 本次调用wait()后所有通道的匹配结果信息,是一个uint64类型数值,
# 表示0到63个通道的匹配信息,通过位运算来获取具体�
+97
View File
@@ -0,0 +1,97 @@
##
## SL2-100 UDP Forwarder
## 监控 CSV 文件,将新行通过 UDP 发送到目标端口
##
## 用法: python udp_forwarder.py [csv文件路径] [目标IP] [目标端口]
## 示例: python udp_forwarder.py d:/sl2-100.csv 127.0.0.1 12345
## 默认: python udp_forwarder.py d:/sl2-100.csv 127.0.0.1 12345
##
import socket
import sys
import os
import time
def main():
# 默认参数
csv_file = "d:/sl2-100.csv"
target_host = "192.168.1.155"
target_port = 41234
# 命令行参数解析
if len(sys.argv) >= 2:
csv_file = sys.argv[1]
if len(sys.argv) >= 3:
target_host = sys.argv[2]
if len(sys.argv) >= 4:
target_port = int(sys.argv[3])
print(f"[UDP Forwarder] 启动")
print(f" CSV 文件: {csv_file}")
print(f" UDP 目标: {target_host}:{target_port}")
print(f" 等待 CSV 文件生成...")
# 创建 UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 等待 CSV 文件出现
while not os.path.exists(csv_file):
time.sleep(0.1)
print(f" CSV 文件已发现,开始监控...")
# 读取文件初始大小(跳过表头)
file_size = os.path.getsize(csv_file)
with open(csv_file, 'r', encoding='gbk') as f:
header = f.readline() # 跳过 "x坐标,y坐标" 表头
file_size = os.path.getsize(csv_file)
print(f" 已跳过表头: {header.strip()}")
# 轮询文件变化
while True:
try:
current_size = os.path.getsize(csv_file)
if current_size < file_size:
# 文件被截断了(重新开始),重新定位
print(f" 检测到文件被重置,重新开始跟踪")
file_size = current_size
with open(csv_file, 'r', encoding='gbk') as f:
f.readline() # 跳过表头
file_size = os.path.getsize(csv_file)
if current_size > file_size:
# 有新数据写入
with open(csv_file, 'r', encoding='gbk') as f:
f.seek(file_size)
new_lines = f.read()
file_size = current_size
# 逐行发送
for line in new_lines.strip().split('\n'):
line = line.strip()+',0'
if line:
data = (line + '\n').encode('utf-8')
sock.sendto(data, (target_host, target_port))
print(f" UDP -> {target_host}:{target_port}: {line}")
time.sleep(0.01) # 10ms 轮询间隔
except FileNotFoundError:
print(f" CSV 文件丢失,等待重新创建...")
while not os.path.exists(csv_file):
time.sleep(0.5)
file_size = 0
print(f" CSV 文件重新出现,继续监控...")
except KeyboardInterrupt:
print(f"\n[UDP Forwarder] 已停止")
break
except Exception as e:
print(f" 错误: {e}")
time.sleep(0.5)
sock.close()
if __name__ == '__main__':
main()
+28
View File
@@ -0,0 +1,28 @@
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2019 Uli Huber
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
'''
XY2-100 is a serial bus for connecting galvo systems to controllers
Details:
http://www.newson.be/doc.php?id=XY2-100
'''
from .pd import Decoder
+312
View File
@@ -0,0 +1,312 @@
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2019 Uli Huber
## Copyright (C) 2020 Soeren Apel
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
import sigrokdecode as srd
ann_bit, ann_bit2, ann_bit3, ann_bit4, ann_stat_bit, ann_type, ann_command, ann_parameter, ann_parity, ann_pos, ann_pos2, ann_pos3, ann_pos4, ann_status, ann_warning = range(15)
frame_type_none, frame_type_command, frame_type_16bit_pos, frame_type_18bit_pos = range(4)
class Decoder(srd.Decoder):
api_version = 3
id = 'xy2-100-E'
name = 'XY2-100-E'
longname = 'XY2-100-E(E) and XY-200(E) galvanometer protocol'
desc = 'Serial protocol for galvanometer positioning in laser systems by chenyue'
license = 'gplv2+'
inputs = ['logic']
outputs = []
tags = ['Embedded/industrial']
# 你要的 options
options = (
{'id': 'worksize', 'desc': '振镜区域大小', 'default': 60},
{'id': 'resolution', 'desc': '振镜分辨率', 'default': 65536},
{'id': 'filename', 'desc': '解码输出文件名','default':'d:/xy2-100.csv'},
)
channels = (
{'id': 'clk', 'name': 'CLK', 'desc': 'Clock'},
{'id': 'sync', 'name': 'SYNC', 'desc': 'Sync'},
{'id': 'data', 'name': 'DATA X', 'desc': 'X axis data'},
{'id': 'data2', 'name': 'DATA Y', 'desc': 'Y axis data'},
{'id': 'data3', 'name': 'DATA X return', 'desc': 'X axis data return'},
{'id': 'data4', 'name': 'DATA Y return', 'desc': 'Y axis data return'},
)
optional_channels = (
{'id': 'status', 'name': 'STAT', 'desc': 'X, Y or Z axis status'},
)
annotations = (
('bit', 'Data Bit X'),
('bit2', 'Data Bit Y'),
('bit3', 'Data Bit X Return'),
('bit4', 'Data Bit Y Return'),
('stat_bit', 'Status Bit'),
('type', 'Frame Type'),
('command', 'Command'),
('parameter', 'Parameter'),
('parity', 'Parity'),
('position', 'Position X'),
('position2', 'Position Y'),
('position3', 'Position X Return'),
('position4', 'Position Y Return'),
('status', 'Status'),
('warning', 'Human-readable warnings'),
)
annotation_rows = (
('bits', 'Data Bits X', (ann_bit,)),
('bits2', 'Data Bits Y', (ann_bit2,)),
('bits3', 'Data Bits X Return', (ann_bit3,)),
('bits4', 'Data Bits Y Return', (ann_bit4,)),
('stat_bits', 'Status Bits', (ann_stat_bit,)),
('data', 'Data', (ann_type, ann_command, ann_parameter, ann_parity)),
('positions', 'Positions X', (ann_pos,)),
('positions2', 'Positions Y', (ann_pos2,)),
('positions3', 'Positions X Return', (ann_pos3,)),
('positions4', 'Positions Y Return', (ann_pos4,)),
('statuses', 'Statuses', (ann_status,)),
('warnings', 'Warnings', (ann_warning,)),
)
def __init__(self):
self.samplerate = None
self.filename = ""
self.file = None
self.x=0
self.y=0
self.x1=0
self.y1=0
self.reset()
def reset(self):
self.bits = []
self.bits2 = []
self.bits3 = []
self.bits4 = []
self.stat_bits = []
self.stat_skip_bit = True
def metadata(self, key, value):
if key == srd.SRD_CONF_SAMPLERATE:
self.samplerate = value
self.filename = self.options['filename']
def start(self):
self.out_ann = self.register(srd.OUTPUT_ANN)
# 读取配置参数
self.worksize = self.options['worksize']
self.resolution = self.options['resolution']
self.half_res = self.resolution / 2
def put_ann(self, ss, es, ann_class, value):
self.put(ss, es, self.out_ann, [ann_class, value])
# 计算逻辑坐标
def calc_logic(self, raw_pos):
return (raw_pos - self.half_res) * self.worksize / self.resolution
def process_bit(self, sync, bit_ss, bit_es, bit_value, bit_value2):
# X 轴
self.put_ann(bit_ss, bit_es, ann_bit, ['%d' % bit_value])
self.bits.append((bit_ss, bit_es, bit_value))
# Y 轴
self.put_ann(bit_ss, bit_es, ann_bit2, ['%d' % bit_value2])
self.bits2.append((bit_ss, bit_es, bit_value2))
if sync == 0:
self.process_frame(self.bits, ann_pos, 0)
self.process_frame(self.bits2, ann_pos2, 1)
self.process_frame(self.bits3, ann_pos3, 2)
self.process_frame(self.bits4, ann_pos4, 3)
self.reset()
def process_bit2(self, sync, bit_ss, bit_es, bit_value3, bit_value4):
self.put_ann(bit_ss, bit_es, ann_bit3, ['%d' % bit_value3])
self.bits3.append((bit_ss, bit_es, bit_value3))
self.put_ann(bit_ss, bit_es, ann_bit4, ['%d' % bit_value4])
self.bits4.append((bit_ss, bit_es, bit_value4))
def process_frame(self, bits, pos_ann_id, ch):
if len(bits) < 20:
if bits:
self.put_ann(bits[0][0], bits[-1][1], ann_warning, ['Not enough data bits'])
return
parity = 0
for ss, es, value in bits[:-1]:
parity ^= value
par_ss, par_es, par_value = bits[19]
parity_even = (par_value == parity)
parity_odd = not parity_even
type_1_value = bits[0][2]
type_3_value = (bits[0][2] << 2) | (bits[1][2] << 1) | bits[2][2]
type = frame_type_none
parity_status = ['OK'] if parity_even else ['NOK']
type_ss = bits[0][0]
type_es = bits[2][1]
if (type_1_value == 1) and (parity_odd == 1):
type = frame_type_18bit_pos
type_es = bits[0][1]
elif (type_3_value == 1):
type = frame_type_16bit_pos
#if not parity_even:
# self.put_ann(bits[0][0], bits[-1][1], ann_warning, ['Parity error'])
elif (type_3_value == 7) and (parity_even == 1):
type = frame_type_command
else:
type = frame_type_16bit_pos
# self.put_ann(bits[0][0], bits[-1][1], ann_warning, ['Unknown frame'])
# return
if type == frame_type_16bit_pos:
self.put_ann(type_ss, type_es, ann_type, ['16bit Position'])
if type == frame_type_18bit_pos:
self.put_ann(type_ss, type_es, ann_type, ['18bit Position'])
if type == frame_type_command:
self.put_ann(type_ss, type_es, ann_type, ['Command'])
self.put_ann(par_ss, par_es, ann_parity, parity_status)
pos = 0
if type == frame_type_16bit_pos:
count = 15
for ss, es, value in bits[3:19]:
pos |= value << count
count -= 1
elif type == frame_type_18bit_pos:
count = 17
for ss, es, value in bits[3:19]:
pos |= value << count
count -= 1
pos = pos if pos < 131072 else pos - 262144
if ch == 0:
self.x=pos
elif ch == 1:
self.y=pos
elif ch == 2:
if pos>=32768:
pos=pos-32768
else :
pos=pos+32768
self.x1=pos
elif ch == 3:
if pos>=32768:
pos=pos-32768
else :
pos=pos+32768
self.y1=pos
if self.file is not None:
self.file.write('%d,%d,%d,%d\n' % (self.x, self.y, self.x1, self.y1))
# 显示原始位置
if type in (frame_type_16bit_pos, frame_type_18bit_pos):
self.put_ann(type_es, par_ss, pos_ann_id, ['%d' % pos])
if type == frame_type_command:
count = 7
cmd = 0
cmd_es = 0
for ss, es, value in bits[3:11]:
cmd |= value << count
count -= 1
cmd_es = es
self.put_ann(type_es, cmd_es, ann_command, ['Cmd 0x%X' % cmd])
count = 7
param = 0
for ss, es, value in bits[11:19]:
param |= value << count
count -= 1
self.put_ann(cmd_es, par_ss, ann_parameter, ['Param 0x%X' % param])
def process_stat_bit(self, sync, bit_ss, bit_es, bit_value):
if self.stat_skip_bit:
self.stat_skip_bit = False
return
self.put_ann(bit_ss, bit_es, ann_stat_bit, ['%d' % bit_value])
self.stat_bits.append((bit_ss, bit_es, bit_value))
if (sync == 0) and (len(self.stat_bits) == 19):
stat_ss = self.stat_bits[0][0]
stat_es = self.stat_bits[18][1]
status = 0
count = 18
for ss, es, value in self.stat_bits:
status |= value << count
count -= 1
self.put_ann(stat_ss, stat_es, ann_status, ['Status 0x%X' % status])
def decode(self):
try:
self.decode_impl()
finally:
if self.file is not None:
self.file.close()
self.file = None
def decode_impl(self):
if self.filename != "":
try:
self.file = open(self.filename, 'w')
self.file.write('x,y,x1,y1\n')
except OSError as e:
print('xy2-100-E: cannot open %s: %s' % (self.filename, e))
self.file = None
bit_ss = None
bit2_ss = None
bit_value = 0
bit_value2 = 0
stat_ss = None
stat_value = 0
sync_value = 0
has_stat = self.has_channel(6)
while True:
clk, sync, data, data2, data3, data4, stat = self.wait({0: 'e'})
if clk == 1:# rising edge
stat_value = stat
bit_es = self.samplenum
if bit_ss is not None:
self.process_bit(sync_value, bit_ss, bit_es, bit_value, bit_value2)
bit_ss = self.samplenum
else:# falling edge
bit_value = data
bit_value2 = data2
bit_value3 = data3
bit_value4 = data4
sync_value = sync
bit2_es = self.samplenum
if bit2_ss is not None:
self.process_bit2(sync_value, bit2_ss, bit2_es, bit_value3, bit_value4)
bit2_ss = self.samplenum
if stat_ss is not None and has_stat:
stat_es = self.samplenum
self.process_stat_bit(sync_value, stat_ss, stat_es, stat_value)
stat_ss = self.samplenum
+28
View File
@@ -0,0 +1,28 @@
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2019 Uli Huber
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
'''
XY2-100 is a serial bus for connecting galvo systems to controllers
Details:
http://www.newson.be/doc.php?id=XY2-100
'''
from .pd import Decoder
+262
View File
@@ -0,0 +1,262 @@
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2019 Uli Huber
## Copyright (C) 2020 Soeren Apel
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
import sigrokdecode as srd
ann_bit, ann_bit2, ann_stat_bit, ann_type, ann_command, ann_parameter, ann_parity, ann_pos, ann_pos2, ann_logic_x, ann_logic_y, ann_status, ann_warning = range(13)
frame_type_none, frame_type_command, frame_type_16bit_pos, frame_type_18bit_pos = range(4)
class Decoder(srd.Decoder):
api_version = 3
id = 'xy2-100-bsl'
name = 'XY2-100-bsl'
longname = 'XY2-100(E) and XY-200(E) galvanometer protocol'
desc = 'Serial protocol for galvanometer positioning in laser systems'
license = 'gplv2+'
inputs = ['logic']
outputs = []
tags = ['Embedded/industrial']
# 你要的 options
options = (
{'id': 'worksize', 'desc': '振镜区域大小', 'default': 60},
{'id': 'resolution', 'desc': '振镜分辨率', 'default': 65536},
{'id': 'filename', 'desc': '解码输出文件名','default':'d:/xy2-100.csv'},
)
channels = (
{'id': 'clk', 'name': 'CLK', 'desc': 'Clock'},
{'id': 'sync', 'name': 'SYNC', 'desc': 'Sync'},
{'id': 'data', 'name': 'DATA X', 'desc': 'X axis data'},
{'id': 'data2', 'name': 'DATA Y', 'desc': 'Y axis data'},
)
optional_channels = (
{'id': 'status', 'name': 'STAT', 'desc': 'X, Y or Z axis status'},
)
annotations = (
('bit', 'Data Bit X'),
('bit2', 'Data Bit Y'),
('stat_bit', 'Status Bit'),
('type', 'Frame Type'),
('command', 'Command'),
('parameter', 'Parameter'),
('parity', 'Parity'),
('position', 'Position X'),
('position2', 'Position Y'),
('logic_x', 'Logic X'),
('logic_y', 'Logic Y'),
('status', 'Status'),
('warning', 'Human-readable warnings'),
)
annotation_rows = (
('bits', 'Data Bits X', (ann_bit,)),
('bits2', 'Data Bits Y', (ann_bit2,)),
('stat_bits', 'Status Bits', (ann_stat_bit,)),
('data', 'Data', (ann_type, ann_command, ann_parameter, ann_parity)),
('positions', 'Positions X', (ann_pos,)),
('positions2', 'Positions Y', (ann_pos2,)),
('logic_x', 'Logic X', (ann_logic_x,)),
('logic_y', 'Logic Y', (ann_logic_y,)),
('statuses', 'Statuses', (ann_status,)),
('warnings', 'Warnings', (ann_warning,)),
)
def __init__(self):
self.samplerate = None
self.filename = ""
self.file = None
self.x=0
self.y=0
self.z=0
self.reset()
def reset(self):
self.bits = []
self.bits2 = []
self.stat_bits = []
self.stat_skip_bit = True
def metadata(self, key, value):
if key == srd.SRD_CONF_SAMPLERATE:
self.samplerate = value
self.filename = self.options['filename']
def start(self):
self.out_ann = self.register(srd.OUTPUT_ANN)
# 读取配置参数
self.worksize = self.options['worksize']
self.resolution = self.options['resolution']
self.half_res = self.resolution / 2
def put_ann(self, ss, es, ann_class, value):
self.put(ss, es, self.out_ann, [ann_class, value])
# 计算逻辑坐标
def calc_logic(self, raw_pos):
return (raw_pos - self.half_res) * self.worksize / self.resolution
def process_bit(self, sync, bit_ss, bit_es, bit_value, bit_value2):
# X 轴
self.put_ann(bit_ss, bit_es, ann_bit, ['%d' % bit_value])
self.bits.append((bit_ss, bit_es, bit_value))
# Y 轴
self.put_ann(bit_ss, bit_es, ann_bit2, ['%d' % bit_value2])
self.bits2.append((bit_ss, bit_es, bit_value2))
if sync == 0:
self.process_frame(self.bits, ann_pos, ann_logic_x, 0)
self.process_frame(self.bits2, ann_pos2, ann_logic_y, 1)
self.reset()
def process_frame(self, bits, pos_ann_id, logic_ann_id,is_y):
if len(bits) < 20:
self.put_ann(bits[0][0], bits[-1][1], ann_warning, ['Not enough data bits'])
return
parity = 0
for ss, es, value in bits[:-1]:
parity ^= value
par_ss, par_es, par_value = bits[19]
parity_even = (par_value == parity)
parity_odd = not parity_even
type_1_value = bits[0][2]
type_3_value = (bits[0][2] << 2) | (bits[1][2] << 1) | bits[2][2]
type = frame_type_none
parity_status = ['OK'] if parity_even else ['NOK']
type_ss = bits[0][0]
type_es = bits[2][1]
if (type_1_value == 1) and (parity_odd == 1):
type = frame_type_18bit_pos
type_es = bits[0][1]
elif (type_3_value == 1):
type = frame_type_16bit_pos
if not parity_even:
self.put_ann(bits[0][0], bits[-1][1], ann_warning, ['Parity error'])
elif (type_3_value == 7) and (parity_even == 1):
type = frame_type_command
else:
self.put_ann(bits[0][0], bits[-1][1], ann_warning, ['Unknown frame'])
return
if type == frame_type_16bit_pos:
self.put_ann(type_ss, type_es, ann_type, ['16bit Position'])
if type == frame_type_18bit_pos:
self.put_ann(type_ss, type_es, ann_type, ['18bit Position'])
if type == frame_type_command:
self.put_ann(type_ss, type_es, ann_type, ['Command'])
self.put_ann(par_ss, par_es, ann_parity, parity_status)
pos = 0
if type == frame_type_16bit_pos:
count = 15
for ss, es, value in bits[3:19]:
pos |= value << count
count -= 1
elif type == frame_type_18bit_pos:
count = 17
for ss, es, value in bits[3:19]:
pos |= value << count
count -= 1
pos = pos if pos < 131072 else pos - 262144
if(is_y==1):
self.y=pos
if self.filename != "":
self.file.write('%d,%d,%d\n' % (self.x, self.y, self.z))
else:
self.x=pos
# 显示原始位置
if type in (frame_type_16bit_pos, frame_type_18bit_pos):
self.put_ann(type_es, par_ss, pos_ann_id, ['%d' % pos])
# 显示逻辑坐标(3位小数)
logic_val = self.calc_logic(pos)
self.put_ann(type_es, par_ss, logic_ann_id, ['%.3f' % logic_val])
if type == frame_type_command:
count = 7
cmd = 0
cmd_es = 0
for ss, es, value in bits[3:11]:
cmd |= value << count
count -= 1
cmd_es = es
self.put_ann(type_es, cmd_es, ann_command, ['Cmd 0x%X' % cmd])
count = 7
param = 0
for ss, es, value in bits[11:19]:
param |= value << count
count -= 1
self.put_ann(cmd_es, par_ss, ann_parameter, ['Param 0x%X' % param])
def process_stat_bit(self, sync, bit_ss, bit_es, bit_value):
if self.stat_skip_bit:
self.stat_skip_bit = False
return
self.put_ann(bit_ss, bit_es, ann_stat_bit, ['%d' % bit_value])
self.stat_bits.append((bit_ss, bit_es, bit_value))
if (sync == 0) and (len(self.stat_bits) == 19):
stat_ss = self.stat_bits[0][0]
stat_es = self.stat_bits[18][1]
status = 0
count = 18
for ss, es, value in self.stat_bits:
status |= value << count
count -= 1
self.put_ann(stat_ss, stat_es, ann_status, ['Status 0x%X' % status])
def decode(self):
if self.filename != "":
self.file = open(self.filename,'w')
self.file.write('x,y,z\n')
bit_ss = None
bit_value = 0
bit_value2 = 0
stat_ss = None
stat_value = 0
sync_value = 0
has_stat = self.has_channel(4)
while True:
clk, sync, data, data2, stat = self.wait({0: 'e'})
if clk == 1:
stat_value = stat
bit_es = self.samplenum
if bit_ss is not None:
self.process_bit(sync_value, bit_ss, bit_es, bit_value, bit_value2)
bit_ss = self.samplenum
else:
bit_value = data
bit_value2 = data2
sync_value = sync
if stat_ss is not None and has_stat:
stat_es = self.samplenum
self.process_stat_bit(sync_value, stat_ss, stat_es, stat_value)
stat_ss = self.samplenum