当前位置:   article > 正文

petalinux_zynq7 驱动DAC以及ADC模块之四:python实现http_api

petalinux_zynq7 驱动DAC以及ADC模块之四:python实现http_api

前文:

petalinux_zynq7 C语言驱动DAC以及ADC模块之一:建立IPicon-default.png?t=N7T8https://blog.csdn.net/qq_27158179/article/details/136234296petalinux_zynq7 C语言驱动DAC以及ADC模块之二:petalinuxicon-default.png?t=N7T8https://blog.csdn.net/qq_27158179/article/details/136236138petalinux_zynq7 C语言驱动DAC以及ADC模块之三:实现C语言API并编译出库被python调用icon-default.png?t=N7T8https://blog.csdn.net/qq_27158179/article/details/136238093本文用python + flask 在zynq中给出http api。用postman测试。

1. adda_api.py

  1. from flask import Flask, jsonify, request
  2. from adda_service import adda_service
  3. # Flask初始化参数尽量使用你的包名,这个初始化方式是官方推荐的,官方解释:http://flask.pocoo.org/docs/0.12/api/#flask.Flask
  4. app = Flask(__name__)
  5. app.config['JSON_AS_ASCII'] = False # 禁止中文转义
  6. @app.route("/adda/dac", methods=["POST"])
  7. def adda_dac():
  8. httpInstance = adda_service()
  9. ret = httpInstance.adda_dac()
  10. return ret
  11. @app.route("/adda/adc", methods=["POST"])
  12. def adda_adc():
  13. httpInstance = adda_service()
  14. ret = httpInstance.adda_adc()
  15. return ret
  16. @app.route('/HelloWorld')
  17. def hello_world():
  18. return "Hello World!"
  19. if __name__ == "__main__":
  20. app.run(host="0.0.0.0")

2. adda_lib.py

  1. import ctypes
  2. import time
  3. ll = ctypes.cdll.LoadLibrary
  4. libadda = ll("./libadda.so")
  5. class adda_lib():
  6. def __init__(self):
  7. pass
  8. # 输出十六进制类型数组
  9. def print_hex(self, bytes):
  10. l = [hex(int(i)) for i in bytes]
  11. print(" ".join(l))
  12. # 字节列表以16进制格式打印数据
  13. def bytes_to_hexstring(self, data):
  14. lin = ['%02X' % i for i in data] # [ ]内是列表解析语法 ,'%02X'%是格式化语法。
  15. hex_str = " ".join(lin)
  16. return hex_str
  17. # init
  18. def adda_open(self):
  19. libadda.adda_open.restype = ctypes.c_int
  20. ret = libadda.adda_open()
  21. # close
  22. def adda_close(self):
  23. libadda.adda_close.restype = ctypes.c_int
  24. ret = libadda.adda_close()
  25. # dac 采样频率
  26. def adda_DacSetSampleFrequency(self, value):
  27. libadda.adda_DacSetSampleFrequency.argtype = ctypes.c_int
  28. libadda.adda_DacSetSampleFrequency.restype = ctypes.c_int
  29. sample_frequency = ctypes.c_uint(value)
  30. ret = libadda.adda_DacSetSampleFrequency(sample_frequency)
  31. # dac 数组 - 正弦波
  32. def adda_DacGenDataSin(self, desire_length):
  33. # uint8_t dac_buf[1024]
  34. libadda.adda_DacGenDataSin.argtype = [ctypes.POINTER(ctypes.c_ubyte*1024), ctypes.c_int]
  35. libadda.adda_DacGenDataSin.restype = ctypes.c_int
  36. dac_buf = ctypes.create_string_buffer(desire_length)
  37. dac_length = ctypes.c_uint(desire_length)
  38. ret = libadda.adda_DacGenDataSin(dac_buf, dac_length)
  39. ba_raw = bytearray(dac_buf.raw)
  40. ba_out = bytearray(dac_length.value)
  41. for i in range(dac_length.value):
  42. ba_out[i] = ba_raw[i]
  43. # print("ba_out", ba_out)
  44. b_out = bytes(ba_out)
  45. return b_out
  46. # dac 数组 - 三角波
  47. def adda_DacGenDataTriangle(self, desire_length):
  48. # uint8_t dac_buf[1024]
  49. libadda.adda_DacGenDataTriangle.argtype = [ctypes.POINTER(ctypes.c_ubyte*1024), ctypes.c_int]
  50. libadda.adda_DacGenDataTriangle.restype = ctypes.c_int
  51. dac_buf = ctypes.create_string_buffer(desire_length)
  52. dac_length = ctypes.c_uint(desire_length)
  53. ret = libadda.adda_DacGenDataTriangle(dac_buf, dac_length)
  54. ba_raw = bytearray(dac_buf.raw)
  55. ba_out = bytearray(dac_length.value)
  56. for i in range(dac_length.value):
  57. ba_out[i] = ba_raw[i]
  58. # print("ba_out", ba_out)
  59. b_out = bytes(ba_out)
  60. return b_out
  61. # dac 数组 - 设置
  62. def adda_DacSetData(self, data_bytes):
  63. libadda.adda_DacSetData.argtype = [ctypes.POINTER(ctypes.c_ubyte*1024), ctypes.c_int]
  64. libadda.adda_DacSetData.restype = ctypes.c_int
  65. dac_buf = ctypes.create_string_buffer(data_bytes)
  66. dac_length = ctypes.c_uint(len(data_bytes))
  67. ret = libadda.adda_DacSetData(dac_buf, dac_length)
  68. # dac 设置输出
  69. def adda_DacSetOutput(self, enable):
  70. libadda.adda_DacSetOutput.argtype = ctypes.c_int
  71. libadda.adda_DacSetOutput.restype = ctypes.c_int
  72. value = ctypes.c_int(enable)
  73. ret = libadda.adda_DacSetOutput(value)
  74. # dac demo 1
  75. def demo_dac_sin(self):
  76. #libadda.demo_dac_sin()
  77. # init
  78. self.adda_open()
  79. self.adda_DacSetSampleFrequency(128000)
  80. # dac 数组 - 正弦波
  81. dac_length = 128
  82. dac_buf = self.adda_DacGenDataSin(dac_length)
  83. print("dac_buf: ", self.bytes_to_hexstring(dac_buf))
  84. self.adda_DacSetData(dac_buf)
  85. # dac输出开启
  86. self.adda_DacSetOutput(1)
  87. # close
  88. self.adda_close()
  89. # dac demo 2
  90. def demo_dac_triangle(self):
  91. # libadda.demo_dac_triangle()
  92. # init
  93. self.adda_open()
  94. self.adda_DacSetSampleFrequency(128000)
  95. # dac 数组 - 三角波
  96. dac_length = 128
  97. dac_buf = self.adda_DacGenDataTriangle(dac_length)
  98. print("dac_buf: ", self.bytes_to_hexstring(dac_buf))
  99. self.adda_DacSetData(dac_buf)
  100. # dac输出开启
  101. self.adda_DacSetOutput(1)
  102. # close
  103. self.adda_close()
  104. # adc 采样频率
  105. def adda_AdcSetSampleFrequency(self, value):
  106. libadda.adda_AdcSetSampleFrequency.argtype = ctypes.c_int
  107. libadda.adda_AdcSetSampleFrequency.restype = ctypes.c_int
  108. sample_frequency = ctypes.c_uint(value)
  109. ret = libadda.adda_AdcSetSampleFrequency(sample_frequency)
  110. # adc 获取采样数据
  111. def adda_AdcSampleData(self, desire_length):
  112. libadda.adda_AdcSampleData.argtype = [ctypes.POINTER(ctypes.c_ubyte*1024), ctypes.c_int]
  113. libadda.adda_AdcSampleData.restype = ctypes.c_int
  114. adc_buf = ctypes.create_string_buffer(desire_length)
  115. adc_length = ctypes.c_uint(desire_length)
  116. ret = libadda.adda_AdcSampleData(adc_buf, adc_length)
  117. ba_raw = bytearray(adc_buf.raw)
  118. ba_out = bytearray(adc_length.value)
  119. for i in range(adc_length.value):
  120. ba_out[i] = ba_raw[i]
  121. # print("ba_out", ba_out)
  122. b_out = bytes(ba_out)
  123. return b_out
  124. # adc demo
  125. def demo_adc(self):
  126. # init
  127. self.adda_open()
  128. # 设置采样频率
  129. self.adda_AdcSetSampleFrequency(100000)
  130. # 开始采样
  131. adc_length = 300
  132. adc_buff = self.adda_AdcSampleData(adc_length)
  133. # close
  134. self.adda_close()
  135. # 打印结果
  136. print("adc_buff: ", self.bytes_to_hexstring(adc_buff))

3. adda_service.py

  1. from flask import Flask, jsonify, request
  2. from adda_lib import adda_lib
  3. class adda_service():
  4. def __init__(self):
  5. pass
  6. def adda_dac(self):
  7. """
  8. 设置DAC
  9. :return:
  10. """
  11. data = request.get_json()
  12. sampleFrequency = int(data.get("sampleFrequency"))
  13. hexString = data.get("hexString")
  14. enable = int(data.get("enable"))
  15. addaLibInst = adda_lib()
  16. # ret = addaLibInst.demo_dac_sin()
  17. # ret = addaLibInst.demo_dac_triangle()
  18. ret = addaLibInst.adda_open()
  19. ret = addaLibInst.adda_DacSetSampleFrequency(sampleFrequency)
  20. ret = addaLibInst.adda_DacSetData(bytes.fromhex(hexString))
  21. ret = addaLibInst.adda_DacSetOutput(enable)
  22. ret = addaLibInst.adda_close()
  23. return jsonify({
  24. "code": 0,
  25. "msg": "OK"
  26. })
  27. def adda_adc(self):
  28. """
  29. ADC读取
  30. :return:
  31. """
  32. data = request.get_json()
  33. sampleFrequency = int(data.get("sampleFrequency"))
  34. adc_length = int(data.get("adc_length"))
  35. addaLibInst = adda_lib()
  36. ret = addaLibInst.adda_open()
  37. ret = addaLibInst.adda_AdcSetSampleFrequency(sampleFrequency)
  38. adc_buff = addaLibInst.adda_AdcSampleData(adc_length)
  39. adc_result = addaLibInst.bytes_to_hexstring(adc_buff)
  40. # print("adc_result: ", adc_result)
  41. ret = addaLibInst.adda_close()
  42. return jsonify({
  43. "code": 0,
  44. "msg": "OK",
  45. "hexString":adc_result
  46. })

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网页波形显示icon-default.png?t=N7T8https://blog.csdn.net/qq_27158179/article/details/136240421

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/138096
推荐阅读
相关标签
  

闽ICP备14008679号