- from machine import Pin, SoftSPI
- import time
-
-
- def ID(agreement):
-
- txbuf =bytearray([17])
- rxbuf = bytearray(3)
- spi = SoftSPI(baudrate=100000, polarity=1, phase=0, mosi=Pin(4), miso=Pin(16), sck=Pin(2))
- spi.init(baudrate=125000)
- p16 = Pin(17, Pin.OUT)
- p16.value(1)
- time.sleep(1)
- p16.value(0)
-
- spi.write(txbuf)
- txbuf =bytearray([32,0,0])
-
- spi.write_readinto(txbuf, rxbuf)
- p16.value(1)
- return rxbuf
以上是MicroPython的代码,
以下是Python代码
- import time
- import pyvisa
- import serial
- import logging
-
-
- TestStep=100
-
-
- logging.basicConfig(level=logging.INFO, filename='coder.log', filemode='a',
- format='%(asctime)s- %(levelname)s: %(message)s')
-
- logLevel=logging.StreamHandler()
- logLevel.setLevel(level="INFO")
- logger = logging.getLogger('simpleExample')
-
-
- ESP32 = serial.Serial('COM22', 115200, timeout=2)
-
- time.sleep(3)
-
- ESP32.write(b'import ADX3228\r\n')
-
- rm = pyvisa.ResourceManager()
-
- print(rm.list_resources())
-
- E_Source=rm.open_resource(' ')
-
- print(E_Source.query("*IDN?"))
-
- E_Source.write("INST P6V")
-
-
- E_Source.write("VOLT 5.0")
- E_Source.write('CURR 0.2')
- E_Source.write('OUTPut OFF')
- time.sleep(1)
-
- E_Source.write('OUTPut ON')
-
- while TestStep>0:
- E_Source.write('OUTPut ON')
- time.sleep(0.3)
- TestStep=TestStep-1
-
- ESP32.write(b'ADX3228.ID(1)\r\n')
- Espmessage=ESP32.readlines()
- print(Espmessage)
- logger.info(Espmessage)
- E_Source_Current = E_Source.query("MEASure:CURRent? P6V")
- E_Source_Voltage = E_Source.query("MEASure:VOLTage? P6V")
- logger.info("Source Current:"+E_Source_Current)
- logger.info("Source Voltage:" + E_Source_Voltage)
- E_Source.write('OUTPut OFF')
- time.sleep(1)
-
-
-
-
-
- ESP32.close()
-
使用ESP32+MicroPython 配合Pyvisa等 完成自动化测试,最后收集数据即可,大大提升了工作效率
- 2023-12-28 11:52:24,378- INFO: [b'import ADX3228\r\n', b'>>> ADX3228.ID(1)\r\n', b"bytearray(b'\\x00\\x00\\x0e')\r\n", b'>>> ']
- 2023-12-28 11:52:24,623- INFO: Source Current:+1.09928000E-01
-
- 2023-12-28 11:52:24,623- INFO: Source Voltage:+4.99952200E+00
-
- 2023-12-28 11:52:30,956- INFO: [b'ADX3228.ID(1)\r\n', b"bytearray(b'\\x00\\x00\\x0e')\r\n", b'>>> ']
- 2023-12-28 11:52:31,203- INFO: Source Current:+1.09928000E-01
-
- 2023-12-28 11:52:31,203- INFO: Source Voltage:+4.99952200E+00
-
- 2023-12-28 11:52:37,541- INFO: [b'ADX3228.ID(1)\r\n', b"bytearray(b'\\x00\\x00\\x0e')\r\n", b'>>> ']
- 2023-12-28 11:52:37,785- INFO: Source Current:+1.09928000E-01
-
- 2023-12-28 11:52:37,785- INFO: Source Voltage:+4.99952200E+00
-
- 2023-12-28 11:52:57,079- INFO: [b'import ADX3228\r\n', b'>>> ADX3228.ID(1)\r\n', b"bytearray(b'\\x00\\x00\\x0e')\r\n", b'>>> ']
- 2023-12-28 11:52:57,330- INFO: Source Current:+1.09928000E-01
-
- 2023-12-28 11:52:57,331- INFO: Source Voltage:+4.99952200E+00
-
- 2023-12-28 11:53:03,660- INFO: [b'ADX3228.ID(1)\r\n', b"bytearray(b'\\x00\\x00\\x0e')\r\n", b'>>> ']
- 2023-12-28 11:53:03,913- INFO: Source Current:+1.09928000E-01
-
- 2023-12-28 11:53:03,913- INFO: Source Voltage:+4.99952200E+00
-
- 2023-12-28 11:53:10,231- INFO: [b'ADX3228.ID(1)\r\n', b"bytearray(b'\\x00\\x00\\x0e')\r\n", b'>>> ']
- 2023-12-28 11:53:10,493- INFO: Source Current:+1.10091000E-01
-
- 2023-12-28 11:53:10,494- INFO: Source Voltage:+4.99952200E+00
-
- 2023-12-28 11:53:16,834- INFO: [b'ADX3228.ID(1)\r\n', b"bytearray(b'\\x00\\x00\\x0e')\r\n", b'>>> ']
- 2023-12-28 11:53:17,075- INFO: Source Current:+1.09928000E-01
-
- 2023-12-28 11:53:17,075- INFO: Source Voltage:+4.99952200E+00
-