from machine import Pin, SoftSPI
import time
def ID(agreement):
#txbuf 需要将16进制转换为10进制
txbuf =bytearray([17])
rxbuf = bytearray(3) # create a buffer
spi = SoftSPI(baudrate=100000, polarity=1, phase=0, mosi=Pin(4), miso=Pin(16), sck=Pin(2))
spi.init(baudrate=125000) # set the baudrate
p16 = Pin(17, Pin.OUT) # create output pin on GPIO16
p16.value(1) # set pin to on/high
time.sleep(1)
p16.value(0) # set pin to on/high
#给芯片发送SDATAC
spi.write(txbuf) # write to MOSI and read from MISO into the buffer
txbuf =bytearray([32,0,0])
#给芯片发送读取ID的指令并且读取ID
spi.write_readinto(txbuf, rxbuf) # write to MOSI and read from MISO into the buffer
p16.value(1) # set pin to on/high
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')
# 连接demo板
ESP32 = serial.Serial('COM22', 115200, timeout=2)
# Pyserial 必须等待5S
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
#读取ID并做记录
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)
# 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)
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