前文:
petalinux_zynq7 C语言驱动DAC以及ADC模块之一:建立IPhttps://blog.csdn.net/qq_27158179/article/details/136234296petalinux_zynq7 C语言驱动DAC以及ADC模块之二:petalinuxhttps://blog.csdn.net/qq_27158179/article/details/136236138petalinux_zynq7 C语言驱动DAC以及ADC模块之三:实现C语言API并编译出库被python调用https://blog.csdn.net/qq_27158179/article/details/136238093本文用python + flask 在zynq中给出http api。用postman测试。
1. adda_api.py
from flask import Flask, jsonify, request
from adda_service import adda_service# Flask初始化参数尽量使用你的包名,这个初始化方式是官方推荐的,官方解释:http://flask.pocoo.org/docs/0.12/api/#flask.Flask
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False # 禁止中文转义@app.route("/adda/dac", methods=["POST"])
def adda_dac():httpInstance = adda_service()ret = httpInstance.adda_dac()return ret@app.route("/adda/adc", methods=["POST"])
def adda_adc():httpInstance = adda_service()ret = httpInstance.adda_adc()return ret@app.route('/HelloWorld')
def hello_world():return "Hello World!"if __name__ == "__main__":app.run(host="0.0.0.0")
2. adda_lib.py
import ctypes
import timell = ctypes.cdll.LoadLibrary
libadda = ll("./libadda.so")class adda_lib():def __init__(self):pass# 输出十六进制类型数组def print_hex(self, bytes):l = [hex(int(i)) for i in bytes]print(" ".join(l))# 字节列表以16进制格式打印数据def bytes_to_hexstring(self, data):lin = ['%02X' % i for i in data] # [ ]内是列表解析语法 ,'%02X'%是格式化语法。hex_str = " ".join(lin)return hex_str# initdef adda_open(self):libadda.adda_open.restype = ctypes.c_intret = libadda.adda_open()# closedef adda_close(self):libadda.adda_close.restype = ctypes.c_intret = libadda.adda_close()# dac 采样频率def adda_DacSetSampleFrequency(self, value):libadda.adda_DacSetSampleFrequency.argtype = ctypes.c_intlibadda.adda_DacSetSampleFrequency.restype = ctypes.c_intsample_frequency = ctypes.c_uint(value)ret = libadda.adda_DacSetSampleFrequency(sample_frequency)# dac 数组 - 正弦波def adda_DacGenDataSin(self, desire_length):# uint8_t dac_buf[1024]libadda.adda_DacGenDataSin.argtype = [ctypes.POINTER(ctypes.c_ubyte*1024), ctypes.c_int]libadda.adda_DacGenDataSin.restype = ctypes.c_intdac_buf = ctypes.create_string_buffer(desire_length)dac_length = ctypes.c_uint(desire_length)ret = libadda.adda_DacGenDataSin(dac_buf, dac_length)ba_raw = bytearray(dac_buf.raw)ba_out = bytearray(dac_length.value)for i in range(dac_length.value):ba_out[i] = ba_raw[i]# print("ba_out", ba_out)b_out = bytes(ba_out)return b_out# dac 数组 - 三角波def adda_DacGenDataTriangle(self, desire_length):# uint8_t dac_buf[1024]libadda.adda_DacGenDataTriangle.argtype = [ctypes.POINTER(ctypes.c_ubyte*1024), ctypes.c_int]libadda.adda_DacGenDataTriangle.restype = ctypes.c_intdac_buf = ctypes.create_string_buffer(desire_length)dac_length = ctypes.c_uint(desire_length)ret = libadda.adda_DacGenDataTriangle(dac_buf, dac_length)ba_raw = bytearray(dac_buf.raw)ba_out = bytearray(dac_length.value)for i in range(dac_length.value):ba_out[i] = ba_raw[i]# print("ba_out", ba_out)b_out = bytes(ba_out)return b_out# dac 数组 - 设置def adda_DacSetData(self, data_bytes):libadda.adda_DacSetData.argtype = [ctypes.POINTER(ctypes.c_ubyte*1024), ctypes.c_int]libadda.adda_DacSetData.restype = ctypes.c_intdac_buf = ctypes.create_string_buffer(data_bytes)dac_length = ctypes.c_uint(len(data_bytes))ret = libadda.adda_DacSetData(dac_buf, dac_length)# dac 设置输出def adda_DacSetOutput(self, enable):libadda.adda_DacSetOutput.argtype = ctypes.c_intlibadda.adda_DacSetOutput.restype = ctypes.c_intvalue = ctypes.c_int(enable)ret = libadda.adda_DacSetOutput(value)# dac demo 1def demo_dac_sin(self):#libadda.demo_dac_sin()# initself.adda_open()self.adda_DacSetSampleFrequency(128000)# dac 数组 - 正弦波dac_length = 128dac_buf = self.adda_DacGenDataSin(dac_length)print("dac_buf: ", self.bytes_to_hexstring(dac_buf))self.adda_DacSetData(dac_buf)# dac输出开启self.adda_DacSetOutput(1)# closeself.adda_close()# dac demo 2def demo_dac_triangle(self):# libadda.demo_dac_triangle()# initself.adda_open()self.adda_DacSetSampleFrequency(128000)# dac 数组 - 三角波dac_length = 128dac_buf = self.adda_DacGenDataTriangle(dac_length)print("dac_buf: ", self.bytes_to_hexstring(dac_buf))self.adda_DacSetData(dac_buf)# dac输出开启self.adda_DacSetOutput(1)# closeself.adda_close()# adc 采样频率def adda_AdcSetSampleFrequency(self, value):libadda.adda_AdcSetSampleFrequency.argtype = ctypes.c_intlibadda.adda_AdcSetSampleFrequency.restype = ctypes.c_intsample_frequency = ctypes.c_uint(value)ret = libadda.adda_AdcSetSampleFrequency(sample_frequency)# adc 获取采样数据def adda_AdcSampleData(self, desire_length):libadda.adda_AdcSampleData.argtype = [ctypes.POINTER(ctypes.c_ubyte*1024), ctypes.c_int]libadda.adda_AdcSampleData.restype = ctypes.c_intadc_buf = ctypes.create_string_buffer(desire_length)adc_length = ctypes.c_uint(desire_length)ret = libadda.adda_AdcSampleData(adc_buf, adc_length)ba_raw = bytearray(adc_buf.raw)ba_out = bytearray(adc_length.value)for i in range(adc_length.value):ba_out[i] = ba_raw[i]# print("ba_out", ba_out)b_out = bytes(ba_out)return b_out# adc demodef demo_adc(self):# initself.adda_open()# 设置采样频率self.adda_AdcSetSampleFrequency(100000)# 开始采样adc_length = 300adc_buff = self.adda_AdcSampleData(adc_length)# closeself.adda_close()# 打印结果print("adc_buff: ", self.bytes_to_hexstring(adc_buff))
3. adda_service.py
from flask import Flask, jsonify, request
from adda_lib import adda_libclass adda_service():def __init__(self):passdef adda_dac(self):"""设置DAC:return:"""data = request.get_json()sampleFrequency = int(data.get("sampleFrequency"))hexString = data.get("hexString")enable = int(data.get("enable"))addaLibInst = adda_lib()# ret = addaLibInst.demo_dac_sin()# ret = addaLibInst.demo_dac_triangle()ret = addaLibInst.adda_open()ret = addaLibInst.adda_DacSetSampleFrequency(sampleFrequency)ret = addaLibInst.adda_DacSetData(bytes.fromhex(hexString))ret = addaLibInst.adda_DacSetOutput(enable)ret = addaLibInst.adda_close()return jsonify({"code": 0,"msg": "OK"})def adda_adc(self):"""ADC读取:return:"""data = request.get_json()sampleFrequency = int(data.get("sampleFrequency"))adc_length = int(data.get("adc_length"))addaLibInst = adda_lib()ret = addaLibInst.adda_open()ret = addaLibInst.adda_AdcSetSampleFrequency(sampleFrequency)adc_buff = addaLibInst.adda_AdcSampleData(adc_length)adc_result = addaLibInst.bytes_to_hexstring(adc_buff)# print("adc_result: ", adc_result)ret = addaLibInst.adda_close()return jsonify({"code": 0,"msg": "OK","hexString":adc_result})
4. 运行
4.1 拷贝文件
把adda_api.py,adda_lib.py,adda_service,libadda.so,拷贝到zynq的linux系统内。
4.2 准备网络
把zynq板卡和电脑连接同一个路由器。
4.3 zynq运行 adda_api.py
5. postman调试http接口
5.1 测试dac输出正弦波
http方法:post,http://192.168.123.138:5000/adda/dac
Body,raw:
{
"sampleFrequency":"128000",
"hexString":"7F858B92989EA4AAB0B6BBC1C6CBD0D5D9DDE2E5E9ECEFF2F5F7F9FBFCFDFEFEFFFEFEFDFCFBF9F7F5F2EFECE9E5E2DDD9D5D0CBC6C1BBB6B0AAA49E98928B857F79736C66605A544E48433D38332E2925211C1915120F0C09070503020100000000000102030507090C0F1215191C2125292E33383D43484E545A60666C7379",
"enable":1
}
5.2 测试dac输出三角波
{
"sampleFrequency":"128000",
"hexString":"0001030507090B0D0F11131517191B1D1F21232527292B2D2F31333537393B3D3F41434547494B4D4F51535557595B5D5F61636567696B6D6F71737577797B7D7F81838587898B8D8F91939597999B9D9FA1A3A5A7A9ABADAFB1B3B5B7B9BBBDBFC1C3C5C7C9CBCDCFD1D3D5D7D9DBDDDFE1E3E5E7E9EBEDEFF1F3F5F7F9FBFD",
"enable":1
}
5.3 测试adc
post,http://192.168.123.138:5000/adda/adc
Body,raw:
{
"sampleFrequency":"100000",
"adc_length":100
}
下篇:
petalinux_zynq7 C语言驱动DAC以及ADC模块之五:nodejs+vue3实现web网页波形显示https://blog.csdn.net/qq_27158179/article/details/136240421