Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1251

Python • Re: [Errno 2] No such file or directory Error When Switching from Pi 4B to CM4

$
0
0

Code:

import RPi.GPIO as GPIOimport spidevimport tracebackclass TECController:    CS_PIN = 17  # 根据你的硬件配置    SPI_BUS = 1    SPI_DEVICE = 1    # 温度到DAC值的映射    temperature_to_dac_value = {        40: 5738,        41: 5595,        42: 5441,        43: 5297,        44: 5165,        45: 5022,        46: 4890,        47: 4758,        48: 4626,        49: 4493,        50: 4372,        51: 4251,        52: 4141,        53: 4020,        54: 3910,        55: 3800,        56: 3689,        57: 3590,        58: 3491,        59: 3392,        60: 3293,        61: 3194,        62: 3106,        63: 3018,        64: 2930,        65: 2852,        66: 2764,        67: 2687,        68: 2610,        69: 2533,        70: 2467,        71: 2390,        72: 2324,        73: 2258,        74: 2192,        75: 2126,        76: 2070,        77: 2004,        78: 1949,        79: 1894,        80: 1839,    }    def __init__(self):        # 初始化SPI和GPIO        self.spi = spidev.SpiDev(self.SPI_BUS, self.SPI_DEVICE)        self.spi.max_speed_hz = 500000        self.spi.mode = 0b00        # GPIO.setmode(GPIO.BOARD)        # GPIO.setup(self.CS_PIN, GPIO.OUT)        # GPIO.setup(7, GPIO.OUT)  # 假设7号引脚与你的MAX1978相关        # 设置MAX1978初始状态为ON        self.max1978_state = 'ON'        # GPIO.output(7, GPIO.HIGH)  # 打开MAX1978        self.dac = self.MAX5144(self.spi, self.CS_PIN)    class MAX5144:        def __init__(self, spi, cs_pin):            self.spi = spi            self.cs_pin = cs_pin            # GPIO.setup(self.cs_pin, GPIO.OUT)        def set_dac_output(self, value):            # print("MAX5144: Setting DAC output...")            # 断言:确保传入的值在0到16383之间(因为DAC的值范围是16位,但最高两位是控制位)            assert 0 <= value < 16384, "Invalid DAC value"            # 将值左移两位,为控制位腾出空间            data_word = value << 2            # 计算最高字节(MSB)和最低字节(LSB)            msb = (data_word >> 8) & 0xFF            lsb = data_word & 0xFF            # 将CS(片选)引脚设置为低电平,以开始与DAC的通信            GPIO.output(self.cs_pin, GPIO.LOW)            # 通过SPI发送数据:先发送MSB,然后发送LSB            self.spi.writebytes([msb, lsb])            # 将CS(片选)引脚设置为高电平,结束与DAC的通信            GPIO.output(self.cs_pin, GPIO.HIGH)            # 打印设置的DAC值,以十六进制格式显示MSB和LSB            print("DAC value set to: MSB = {0:#04X}, LSB = {1:#04X}".format(msb, lsb))    def set_temperature(self, temperature):        # print("TECController: Setting DAC value for temperature...")        self.current_temperature = temperature  # 保存当前温度设置        dac_value = self.temperature_to_dac_value.get(temperature)        if dac_value is not None:            self.dac.set_dac_output(dac_value)            print(f"Set temperature to {temperature}°C with DAC value {dac_value}")        else:            print("Temperature out of range")        # print(f"[TECController] Set temperature to {temperature}°C with DAC value {dac_value}")           def toggle_max1978(self):        # 打印调用栈        # print(''.join(traceback.format_stack()))        # 根据当前状态切换MAX1978的状态        if self.max1978_state == 'ON':            GPIO.output(7, GPIO.LOW)  # 关闭MAX1978            self.max1978_state = 'OFF'        else:            GPIO.output(7, GPIO.HIGH)  # 打开MAX1978            self.max1978_state = 'ON'        print(f"MAX1978 turned {self.max1978_state}")    def get_max1978_state(self):        return self.max1978_state  # 返回当前MAX1978状态            def cleanup(self):        # self.spi.close()        # GPIO.cleanup()        pass# 使用示例:if __name__ == "__main__":    try:        tec = TECController()        tec.set_temperature(50)  # 设置温度        print(f"Current MAX1978 state: {tec.get_max1978_state()}")    finally:        tec.cleanup()        
Update on my previous issue:

I'm facing a problem when executing TEC_test_v8.py on a Compute Module 4 (CM4), which was previously working on a Raspberry Pi 4B. Here's the error output:


Traceback (most recent call last):
File "/home/nero/TEC_test_v8.py", line 132, in <module>
tec = TECController()
File "/home/nero/TEC_test_v8.py", line 57, in __init__
self.spi = spidev.SpiDev(self.SPI_BUS, self.SPI_DEVICE)
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception:
Traceback (most recent call last):
File "/home/nero/TEC_test_v8.py", line 136, in <module>
tec.cleanup()
NameError: name 'tec' is not defined

This occurs despite SPI being enabled. I suspect differences in SPI handling between CM4 and standard Pi models might be causing this. Any insights or solutions are welcome. Thanks!

Statistics: Posted by dgy411852 — Wed Feb 28, 2024 10:20 pm



Viewing all articles
Browse latest Browse all 1251

Trending Articles