本文来源电子发烧友社区,作者:jf_25674040, 帖子地址:https://bbs.elecfans.com/jishu_2292606_1_1.html
一、安装ubuntu22.04 server系统
-
-
将SD卡插入到板卡SD卡卡槽中;
-
ssh 账号: user 密码:star5
二、修改dtb文件,并使能uart1
-
获取linux源代码,并编译源代码linux源代码
sudo apt update
sudo apt upgrade sudo apt install gcc-riscv64-linux-gnu git clone https://github.com/starfive-tech/linux.git cd linux make CROSS_COMPILE=riscv64-linux-gnu- ARCH=riscv visionfive_defconfig make CROSS_COMPILE=riscv64-linux-gnu- ARCH=riscv
-
修改
/linux/arch/riscv/boot/dts/starfive/jh7100-common.dtsi
中与uart有关的接口sudo cp linux/arch/riscv/boot/dts/starfive/jh7100-starfive-visionfive-v1.dtb /boot/dtb-5.17.5-visionfive-foobar-29042022
sudo sudo update-grub
UART和DEV映射
三、硬件连接
四、软件代码
- 串口类python代码
import serial
import serial.tools.list_ports class MySerial(object): def init(self): self.ser = None
def get_port_list(self): port_list = list(serial.tools.list_ports.comports()) return port_list def open_serial_port(self, port, baud): try: self.ser = serial.Serial(port, baud, timeout=0.3, interCharTimeout=0.05) except Exception as e: self.ser = None raise Exception(e) def close_serial_port(self): if self.ser == None: raise Exception("no selected serial") try: self.ser.close() self.ser = None except Exception as e: raise Exception(e
-
工具类代码
class MyTools(object):
def init(self): pass
def bytes_to_strs(self, bytes_value): arry = ''.join(['%02x '% b for b in bytes_value]) return (arry.upper())
-
甲醛类代码:
class ze08_ch20(object):
def init(self) -> None: self.tools = MyTools() self.serial = MySerial() self.serial.open_serial_port('/dev/ttyS2', 9600) ''' self.recv_thread_ = threading.Thread(target=self.recv_thread) self.recv_thread_.start() ''' self.recv_thread()
def calc_checksum(self, bytes_value): checksum = 0 for byte_ in bytes_value: checksum += byte_ checksum = checksum % (0xff + 1) if checksum == 0xff: return True return False
def recv_thread(self): recv_buffer = bytes() #print("hello world") while True: try: recv_buffer += self.serial.read() except Exception as e: print(str(e)) if len(recv_buffer) < 9: time.sleep(0.01) #print(len(recv_buffer)) continue else: if recv_buffer[0] != 0xff: recv_buffer = recv_buffer[1:] else: if len(recv_buffer) >= 9: #print(len(recv_buffer)) phase_buffer = recv_buffer[:9] recv_buffer = recv_buffer[9:] print(self.tools.bytes_to_strs(phase_buffer)) if self.calc_checksum(phase_buffer) == True: int_formaldehyde = int.from_bytes(phase_buffer[4:6], byteorder='big') #print(int_formaldehyde) float_formaldehyde = int_formaldehyde /1000.0 *1.25 print(float_formaldehyde) time.sleep(0.001)
-
main代码
if __name__=="__main__":
my_statistics = ze08_ch20()
五、执行代码获取甲醛值
-
RISC-V
+关注
关注
44文章
2218浏览量
45978 -
赛昉科技
+关注
关注
3文章
145浏览量
14247
发布评论请先 登录
相关推荐
评论