我正试图与覆盆子皮野餐与我的覆盆子皮4通过uart。下面的代码确实传输数据,但我只接收打印语句中的数据。
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(),我将什么也得不到。
发布于 2022-11-02 11:25:56
中将print(str(temp))更改为print(str(temp), end="")。
uart.write(str(temp))正在做一些有用的事情。打印语句是将数据发送到raspberry pi的方法。
我向raspberry pi发送数据的一种方法是:
while True:
temp = temperature()
print(len(str(temp)))
utime.sleep(1)
print(str(temp))
utime.sleep(1)长度在1到9之间,因此raspberry pi将在raspberry pi代码中接收1个字符。
while True:
# read length of data
length = int(ser.read(1).encode("utf-8"))
# read the data
data = (ser.read(length))
#convert bytestring to unicode transformation format -8 bit
temperature = str(data).encode("utf-8")
print(temperature)https://stackoverflow.com/questions/69397785
复制相似问题