赞
踩
前文:
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测试。
- 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")
-
- import ctypes
- import time
-
-
- ll = 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
-
- # init
- def adda_open(self):
- libadda.adda_open.restype = ctypes.c_int
- ret = libadda.adda_open()
-
- # close
- def adda_close(self):
- libadda.adda_close.restype = ctypes.c_int
- ret = libadda.adda_close()
-
- # dac 采样频率
- def adda_DacSetSampleFrequency(self, value):
- libadda.adda_DacSetSampleFrequency.argtype = ctypes.c_int
- libadda.adda_DacSetSampleFrequency.restype = ctypes.c_int
- sample_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_int
- dac_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_int
- dac_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_int
- dac_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_int
- libadda.adda_DacSetOutput.restype = ctypes.c_int
- value = ctypes.c_int(enable)
- ret = libadda.adda_DacSetOutput(value)
-
- # dac demo 1
- def demo_dac_sin(self):
- #libadda.demo_dac_sin()
- # init
- self.adda_open()
- self.adda_DacSetSampleFrequency(128000)
- # dac 数组 - 正弦波
- dac_length = 128
- dac_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)
- # close
- self.adda_close()
-
- # dac demo 2
- def demo_dac_triangle(self):
- # libadda.demo_dac_triangle()
- # init
- self.adda_open()
- self.adda_DacSetSampleFrequency(128000)
- # dac 数组 - 三角波
- dac_length = 128
- dac_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)
- # close
- self.adda_close()
-
- # adc 采样频率
- def adda_AdcSetSampleFrequency(self, value):
- libadda.adda_AdcSetSampleFrequency.argtype = ctypes.c_int
- libadda.adda_AdcSetSampleFrequency.restype = ctypes.c_int
- sample_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_int
- adc_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 demo
- def demo_adc(self):
- # init
- self.adda_open()
- # 设置采样频率
- self.adda_AdcSetSampleFrequency(100000)
- # 开始采样
- adc_length = 300
- adc_buff = self.adda_AdcSampleData(adc_length)
- # close
- self.adda_close()
- # 打印结果
- print("adc_buff: ", self.bytes_to_hexstring(adc_buff))
- from flask import Flask, jsonify, request
- from adda_lib import adda_lib
-
- class adda_service():
- def __init__(self):
- pass
- def 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
- })
把adda_api.py,adda_lib.py,adda_service,libadda.so,拷贝到zynq的linux系统内。
把zynq板卡和电脑连接同一个路由器。
http方法:post,http://192.168.123.138:5000/adda/dac
Body,raw:
{
"sampleFrequency":"128000",
"hexString":"7F858B92989EA4AAB0B6BBC1C6CBD0D5D9DDE2E5E9ECEFF2F5F7F9FBFCFDFEFEFFFEFEFDFCFBF9F7F5F2EFECE9E5E2DDD9D5D0CBC6C1BBB6B0AAA49E98928B857F79736C66605A544E48433D38332E2925211C1915120F0C09070503020100000000000102030507090C0F1215191C2125292E33383D43484E545A60666C7379",
"enable":1
}
{
"sampleFrequency":"128000",
"hexString":"0001030507090B0D0F11131517191B1D1F21232527292B2D2F31333537393B3D3F41434547494B4D4F51535557595B5D5F61636567696B6D6F71737577797B7D7F81838587898B8D8F91939597999B9D9FA1A3A5A7A9ABADAFB1B3B5B7B9BBBDBFC1C3C5C7C9CBCDCFD1D3D5D7D9DBDDDFE1E3E5E7E9EBEDEFF1F3F5F7F9FBFD",
"enable":1
}
post,http://192.168.123.138:5000/adda/adc
Body,raw:
{
"sampleFrequency":"100000",
"adc_length":100
}
下篇:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。