0

我正在尝试通过 uart 与我的树莓派 4 与树莓派 pico 通信。下面的代码确实传输数据,但我只从打印语句接收数据。

 import os
    import utime
    from machine import ADC
    
    temp_sensor = ADC(4) # Default connection of temperature sensor


def temperature():
    # get raw sensor data 
    raw_sensor_data = temp_sensor.read_u16()
    
    # convert raw value to equivalent voltage
    sensor_voltage = (raw_sensor_data / 65535)*3.3
    
    # convert voltage to temperature (celcius)
    temperature = 27. - (sensor_voltage - 0.706)/0.001721
    
    return temperature
    

#print setup information :
print("OS Name : ",os.uname())

uart = machine.UART(0, baudrate = 9600)
print("UART Info : ", uart)
utime.sleep(3)

while True:
    temp = temperature()
    print(str(temp))
    uart.write(str(temp))
           
    utime.sleep(1)

我的树莓派 4 上的代码是:

import serial
import time
import numpy as np
import matplotlib.pyplot as plt

#ser = serial.Serial('COM14',9600)
ser = serial.Serial('/dev/ttyACM0', 9600)

time.sleep(1)


while True:
    # read two bytes of data
    #data = (ser.read(8))
    data = (ser.readline())
    # convert bytestring  to unicode transformation format -8 bit
    temperature = str(data).encode("utf-8")
    #print("Pico's Core Temperature : " + temperature + " Degree Celcius")
    print(temperature)

我的 RPI 4 终端的输出是:

27.2332

26.443

26.443

26.564

之间有一条额外的新线。如果我从 pico 代码中删除 print(str(temp)),我什么也得不到。我可以在 uart.write(str(temp)) 中放入任何内容,但仍会收到 print 语句,但如果没有 uart.write(),我将一无所获。

4

0 回答 0