diff --git a/sl2-100/pd.py b/sl2-100/pd.py index 3b0f5a4..e483408 100644 --- a/sl2-100/pd.py +++ b/sl2-100/pd.py @@ -45,7 +45,7 @@ class Decoder(srd.Decoder): options = ( # 一个数据周期10us {'id': 'datatime', 'desc': '数据传输时间(ns)', 'default': 10000}, - {'id': 'filename', 'desc': '解码输出文件名','default':'d:/sl2-100.csv'}, + {'id': 'filename', 'desc': '解码输出文件名','default':'d:/sl2-100.csv'}, {'id': 'invert', 'desc': 'Invert Signal?', 'default': 'no','values': ('yes', 'no'), 'idn':'opt_invert'}, ) # 解析结果项定义 @@ -80,7 +80,7 @@ class Decoder(srd.Decoder): def reset(self): self.samplerate = None self.bit_width = 0 #采样次数,400Mhz时计算出来应该是31.25 - self.oldsamplenum = 0 #采样起始位置 + self.oldsamplenum = 0.0 #采样起始位置 self.ss_first = 0 #当前数据位起始位置 self.first_one = -1 #当前数据位的首电平 self.header_str = "" @@ -127,8 +127,10 @@ class Decoder(srd.Decoder): #标记当前数据位 self.put(ss, es, self.out_ann,[1, [str(bit)]]) #下面处理每个字段 - if self.state == 'HEADER': - if(self.head_cnt == 0 and bit == 0):# 如果是第一个数据位且为0,则认为是y坐标数据 + 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' @@ -141,7 +143,8 @@ class Decoder(srd.Decoder): self.xpos_first = es self.xpos_cnt = 0 #当前坐标 位数归零,准备累积 self.data = 0 #当前坐标值归零,准备开始累积 - elif self.state == 'xpos': + 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: @@ -172,8 +175,10 @@ class Decoder(srd.Decoder): self.state = 'ypos' self.ypos_first = es self.ypos_cnt = 0 #当前坐标 位数归零,准备累积 - self.data = 0 #当前坐标值归零,准备开始累积 - elif self.state == 'HEADER3': + 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: @@ -181,7 +186,8 @@ class Decoder(srd.Decoder): self.state = 'ypos' self.ypos_first = es self.ypos_cnt = 0 #当前坐标 位数归零,准备累积 - self.data = 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 @@ -207,11 +213,7 @@ class Decoder(srd.Decoder): self.head_cnt = 0 self.header_str = "" #差分曼彻斯特解码函数 pin表示当前电平是高(1)还是低(0) - def manchester_decode(self, pin): - #当前电平起始位置 - ss = int(self.samplenum-self.bit_width) - #当前电平结束位置 - es = int(self.samplenum) + def manchester_decode(self, ss,es,pin): #标记电平 高/低 self.put(ss, es, self.out_ann, [0, ['高' if pin==1 else'低']]) #记录HEADER起始位置 第一次需要 @@ -248,55 +250,64 @@ class Decoder(srd.Decoder): self.lowpin = 0 #当前采样宽度(78.125ns)内低电平数量 #当前电平值是否取反 inv = self.options['invert'] == 'yes' - + last_samplenum=0 while True: if self.state2 == 'FIND START': - #self.file.write('FIND START'+ '\n') + #self.file.write(self.state2+ '\n') if inv: - self.wait({0: 'h'}) + (pin,) = self.wait({0:'h'}) else: - self.wait({0: 'l'}) + (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('FIND START2'+ '\n') - if inv: - self.wait({0: 'e'}) - else: - self.wait({0: 'e'}) + #self.file.write(self.state2+ '\n') + (pin,) = self.wait({0:'e'}) start_width = self.samplenum - self.oldsamplenum - self.oldsamplenum = self.samplenum - if start_width >= self.bit_width*2.2: - self.state2 = 'FIND START3' + 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('FIND START3'+ '\n') - (pin,) = self.wait({0:'n'}) - if inv: - pin =not pin - + 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 : #累积采样宽度接近设置的采样次数 - self.state2 = 'FIND DATA' - self.oldsamplenum = self.samplenum - if self.state2 == 'FIND DATA': - #self.file.write('FIND DATA'+ '\n') - (pin,) = self.wait({0:'n'}) - if inv: - pin = not pin + 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 + 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.manchester_decode(1 if self.highpin > self.lowpin else 0) - self.highpin = 0 #当前采样宽度(78.125ns)内高电平数量 - self.lowpin = 0 #当前采样宽度(78.125ns)内低电平数量 + 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'}]