Raspberry-Pi-Pico到Raspberry-Pi的UART通信

2024-05-15 10:44:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

 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)

我的raspberry pi 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(),我将什么也不会收到


Tags: 代码importdatarawpisensortempraspberry