【赛昉科技昉·星光RISC-V单板计算机试用体验】Ubuntu Server 22.04 UART读取ZE08-CH2O 甲醛数据

描述

本文来源电子发烧友社区,作者:jf_25674040, 帖子地址:https://bbs.elecfans.com/jishu_2292606_1_1.html

一、安装ubuntu22.04 server系统

  1. 下载镜像ubuntu-server-22.04-riscv64-visionfive-29042022

  2. 在linux下使用etcher将镜像写入到SD卡;

    RISC-V

  3. 将SD卡插入到板卡SD卡卡槽中;

  4. ssh 账号: user 密码:star5

二、修改dtb文件,并使能uart1

  1. 获取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

     

  2. 修改/linux/arch/riscv/boot/dts/starfive/jh7100-common.dtsi中与uart有关的接口

    RISC-V

    RISC-V

    RISC-V

    
    		
     
    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映射

    RISC-V

三、硬件连接

RISC-V

四、软件代码

  1. 串口类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

 

 

  1. 工具类代码

    
    		
     
    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())

     

  2. 甲醛类代码:

    
    		
     
    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)

     

  3. main代码

    
    		
     
    if __name__=="__main__":

    my_statistics = ze08_ch20()

     

    五、执行代码获取甲醛值

    RISC-V

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分