Hey,
Recently had the need to decode some UART data and replay it over an arbitrary waveform generator. Wrote a quick python script to do the decode and show the resulting frames on the scope and on a bitmap on my laptop. Library supports most low speed serial protocols and USB 2.0 as well, so should be pretty flexible. Including in case anyone else finds it useful:
import ivi
import numpy as np
import vxi11
import re
from numpy import *
from matplotlib import pyplot as plt
import ripyl
import ripyl.streaming as stream
import ripyl.protocol.uart as uart
from collections import OrderedDict
try:
import matplotlib
matplotlib_exists = True
except ImportError:
matplotlib_exists = False
if matplotlib_exists:
import ripyl.util.plot as rplot
class AsciiWaveform:
def __init__(self, waveformString ):
waveform_list = re.split(';',waveformString)
self.CURVE(waveform_list[17])
def CURVE(self,value):
# Make this faster later
waveform = []
for val in re.split(',',value):
try:
waveform.append(float(val)/6400)
except:
pass
self.waveform = np.array(waveform)
tds = vxi11.Instrument("192.168.1.25")
awg = ivi.tektronix.tektronixAWG2021("ASRL::COM5,19200::INSTR")
tds.write("DATA:SOURCE CH1")
tds.write("DATA:ENCDG ASCII")
tds.write("DATa:BYT_NR 1")
record_length = int(tds.ask("HORizontal:RECOrdlength?"))
tds.write('DATa:STOP {}'.format(record_length))
tds.write("CURSor:VBArs SNAp")
waveform_data = tds.ask("WAVFrm?")
awf = AsciiWaveform( waveform_data )
# transfer to AWG2021
awg.outputs[0].arbitrary.create_waveform(awf.waveform)
awg.outputs[0].arbitrary.gain = max(awf.waveform)
awg.outputs[0].arbitrary.offset = float(tds.ask("WFMInpre:YOFf?"))
awg.arbitrary.sample_rate = tds.ask("HORizontal:MAIn:SAMPLERate?")
awg.outputs[0].enabled = True
sample_period = float(tds.ask("WFMOutpre:XINcr?"))
txd = stream.samples_to_sample_stream(awf.waveform, sample_period)
bits = 8
parity = 'even'
stop_bits = 1
polarity = uart.UARTConfig.IdleHigh
baud_rate = 115200
records = list(uart.uart_decode(txd, bits, parity, stop_bits, polarity, baud_rate=baud_rate))
data = [rec.data for rec in records]
lines = ''.join(chr(d) for d in data).split()
tds.write("CH1:LABEL:NAMe \"{}\"".format(lines))
tds.write("CH1:LABEL:XPOS 5")
tds.write("CH1:LABEL:YPOS 3.5")
txd = stream.samples_to_sample_stream(awf.waveform, sample_period)
channels = OrderedDict([('Volts', txd)])
title = 'UART Plot'
plotter = rplot.Plotter()
plotter.plot(channels, records, title, label_format=stream.AnnotationFormat.Text)
plotter.show()