Protocol Buffers在MCU上的nanopb介绍及使用详解

news2025/3/4 8:47:57

在嵌入式系统和资源受限的环境中,传统的Protocol Buffers 可能显得过于庞大。因此,nanopb 应运而生,它是一个轻量级的 Protocol Buffers 生成器,专为嵌入式系统设计c语言设计。本文将介绍如何安装和使用 nanopb,以及通过一个简单的例子来展示它的基本用法。

网上的大多数文章都是只讲如何使用。其实新手刚拿到后,很重要的一点是如何用起来?如何安装环境?protoc工具在哪里搞到?这里从环境介绍到详细使用做个总结,留作备忘。

什么是Protocol Buffers

Protocol Buffer是谷歌推出的,和开发语言无关、平台无关、可扩展的机制,用于序列化结构化数据——像XML,但它更小、更快、更简单。
关键特点:
1、跨平台。
2、可扩展。
3、序列化结构化数据。
4、使用简单。

官方网址:https://developers.google.com/protocol-buffers

支持最常使用的语言
1、C++
2、C#
3、Java
4、Python

什么是 nanopb

nanopb 是一个非常轻量级的 C 库,用于 Protocol Buffers 的序列化和反序列化。它专为嵌入式系统设计,可以运行在内存和存储空间有限的环境中。nanopb 支持 Protocol Buffers 2.3 和 3.0 版本的标准,因此可以用于大多数现有的 Protocol Buffers 定义文件。

仓库地址:https://gitcode.com/gh_mirrors/na/nanopb
github仓:https://github.com/nanopb/nanopb

安装 nanopb

这个很重要。很多人拿到仓库后,苦于找不到protoc工具,不知道如何生成pb.c文件。其实原因就出在这里,没有安装环境。而官方在这里的介绍简单了些。nanopb 的安装可以通过 pip3 和从源码编译两种方式进行。本文推荐使用 pip3 安装,因为它更加简便快捷。

首先,确保你的开发环境中已经安装了 Python3 和 pip3。如果还没有安装,可以通过以下命令安装:

sudo apt-get update
sudo apt-get install python3 python3-pip

接下来,使用 pip3 安装 nanopb 的相关工具。这里我们使用阿里云的镜像源来加速安装:

pip3 install protobuf grpcio-tools -i https://mirrors.aliyun.com/pypi/simple/

安装完成后,你需要确保 nanopb 的生成器工具 protocnanopb_generator 可用。上面安装完依赖后,这两个工具自然可用。可以将/root/test/c/nanopb/generator/加入搭排环境变量里,linux下方便使用protoc。 你可以通过以下命令来生成 .pb.c.pb.h 文件:

../../generator/protoc  --nanopb_out=. simple.proto
# 或者
nanopb_generator  simple.proto

这里 simple.proto 是你的 Protocol Buffers 定义文件。生成器会根据这个文件生成对应的 C 代码文件。

使用 nanopb

要使用 Nanopb 库,您需要执行以下两个步骤:

  1. 使用 protoc 编译您的 .proto 文件以生成适用于 Nanopb 的文件。
  2. 在项目中包含 pb_encode.c、pb_decode.c 和 pb_common.c。

开始学习的最佳方式是研究 “examples/simple” 目录中的示例项目。它包含了一个 Makefile,在大多数 Linux 系统上可以直接工作。然而,对于其他类型的构建系统,可以参考该目录下 README.txt 中的手动步骤。

下面,我们将通过一个简单的例子来展示如何使用 nanopb。
假设我们有一个简单的 Protocol Buffers 定义文件 simple.proto
内容如下:

syntax = "proto2";

message SimpleMessage {
    required int32 lucky_number = 1;
}

这个定义文件中只包含一个消息定义 SimpleMessage,它有一个 int32 类型的字段 lucky_number

接下来,我们将编写 C 代码来处理这个消息。示例代码如下:

#include <stdio.h>
#include <pb_encode.h>
#include <pb_decode.h>
#include "simple.pb.h"

int main()
{
    /* This is the buffer where we will store our message. */
    uint8_t buffer[128];

    size_t message_length;
    bool status;
    
    /* Encode our message */
    {
        /* Allocate space on the stack to store the message data.
         *
         * Nanopb generates simple struct definitions for all the messages.
         * - check out the contents of simple.pb.h!
         * It is a good idea to always initialize your structures
         * so that you do not have garbage data from RAM in there.
         */
        SimpleMessage message = SimpleMessage_init_zero;
        
        /* Create a stream that will write to our buffer. */
        pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
        
        /* Fill in the lucky number */
        message.lucky_number = 13;
        
        /* Now we are ready to encode the message! */
        status = pb_encode(&stream, SimpleMessage_fields, &message);
        message_length = stream.bytes_written;
        
        /* Then just check for any errors.. */
        if (!status)
        {
            printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
            return 1;
        }
    }
    
    /* Now we could transmit the message over network, store it in a file or
     * wrap it to a pigeon's leg.
     */

    /* But because we are lazy, we will just decode it immediately. */
    
    {
        /* Allocate space for the decoded message. */
        SimpleMessage message = SimpleMessage_init_zero;
        
        /* Create a stream that reads from the buffer. */
        pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
        
        /* Now we are ready to decode the message. */
        status = pb_decode(&stream, SimpleMessage_fields, &message);
        
        /* Check for errors... */
        if (!status)
        {
            printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
            return 1;
        }
        
        /* Print the data contained in the message. */
        printf("Your lucky number was %d!\n", (int)message.lucky_number);
    }
    
    return 0;
}

这段代码首先初始化了一个 SimpleMessage 结构体,然后使用 pb_encode 函数将这个结构体编码到一个字节缓冲区中。接着,它又将这个缓冲区中的数据解码回一个 SimpleMessage 结构体,并输出其中的 lucky_number 字段。

Makefile 文件

为了简化编译过程,我们可以使用 Makefile 文件来管理编译规则。以下是示例 Makefile 文件的内容:

# Include the nanopb provided Makefile rules
include ../../extra/nanopb.mk

# Compiler flags to enable all warnings & debug info
CFLAGS = -Wall -Werror -g -O0
CFLAGS += -I$(NANOPB_DIR)

# C source code files that are required
CSRC  = simple.c                   # The main program
CSRC += simple.pb.c                # The compiled protocol definition
CSRC += $(NANOPB_DIR)/pb_encode.c  # The nanopb encoder
CSRC += $(NANOPB_DIR)/pb_decode.c  # The nanopb decoder
CSRC += $(NANOPB_DIR)/pb_common.c  # The nanopb common parts

# Build rule for the main program
simple: $(CSRC)
	$(CC) $(CFLAGS) -osimple $(CSRC)

# Build rule for the protocol
simple.pb.c: simple.proto
	$(PROTOC) $(PROTOC_OPTS) --nanopb_out=. $<

这个 Makefile 文件首先包含了 nanopb 提供的 Makefile 规则 nanopb.mk。然后,它定义了一些编译器标志 CFLAGS,用于在编译过程中启用所有警告并包含调试信息。CSRC 变量列出了所有需要编译的 C 源代码文件,包括主程序文件 simple.c、编译后的 Protocol Buffers 定义文件 simple.pb.c 以及 nanopb 库的核心文件。

最后,Makefile 文件定义了生成最终可执行文件 simple 的规则,以及从 Protocol Buffers 定义文件 simple.proto 生成 C 源代码文件 simple.pb.c 的规则。

nanopb.mk 文件

nanopb.mk 文件包含了 nanopb 提供的 Makefile 规则,用于生成 .pb.c.pb.h 文件以及定义 nanopb 库的核心文件路径。以下是 nanopb.mk 文件的内容:

# This is an include file for Makefiles. It provides rules for building
# .pb.c and .pb.h files out of .proto, as well the path to nanopb core.

# Path to the nanopb root directory
NANOPB_DIR := $(patsubst %/,%,$(dir $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST)))))))

# Files for the nanopb core
NANOPB_CORE = $(NANOPB_DIR)/pb_encode.c $(NANOPB_DIR)/pb_decode.c $(NANOPB_DIR)/pb_common.c

# Check if we are running on Windows
ifdef windir
WINDOWS = 1
endif
ifdef WINDIR
WINDOWS = 1
endif

# Check whether to use binary version of nanopb_generator or the
# system-supplied python interpreter.
ifneq "$(wildcard $(NANOPB_DIR)/generator-bin)" ""
	# Binary package
	PROTOC = $(NANOPB_DIR)/generator-bin/protoc
	PROTOC_OPTS =
else
	# Source only or git checkout
	PROTOC_OPTS =
	ifdef WINDOWS
	    PROTOC = python $(NANOPB_DIR)/generator/protoc
	else
	    PROTOC = $(NANOPB_DIR)/generator/protoc
	endif
endif

# Rule for building .pb.c and .pb.h
%.pb.c %.pb.h: %.proto %.options
	$(PROTOC) $(PROTOC_OPTS) --nanopb_out=. $<

%.pb.c %.pb.h: %.proto
	$(PROTOC) $(PROTOC_OPTS) --nanopb_out=. $<

这个文件首先定义了 nanopb 的根目录路径 NANOPB_DIR,然后列出了 nanopb 库的核心文件路径 NANOPB_CORE。接着,它检查当前操作系统是否为 Windows,并根据操作系统的不同来设置 PROTOC 变量,以指向正确的 protoc 生成器工具。最后,它定义了生成 .pb.c.pb.h 文件的规则。

稍复杂的使用示例

接下来做一个稍复杂的使用,proto文件定义了两个message:KeyValue和DeviceConfig。其中,DeviceConfig包含一些基本类型的字段和一个重复的KeyValue字段。生成的nanopb头文件中对于字符串和重复字段,使用了pb_callback_t类型,这意味着这些字段需要通过回调函数来处理,或者在生成时可能没有设置相应的选项来优化为静态数组。

假如有以下proto文件定义:

syntax = "proto2";

package example;

message KeyValue {
    required string key = 1;
    required int32 value = 2;
}

message DeviceConfig {
    required int32 BrdNum = 1;
    required int32 address = 2;
    required string type = 3;
    required int32 priority = 4;
    required int32 accessTime = 5;
    required string brdLocation = 6;
    repeated KeyValue bitMean = 7;
}

编码(序列化)的过程大致是:初始化消息结构体,填充数据,然后调用pb_encode函数。对于回调处理的字段(如字符串和重复字段),需要设置回调函数或者在结构体中正确填充数据。例如,pb_callback_t类型的字段需要用户提供函数来处理数据的编码,或者在结构体中直接设置对应的数据指针和大小。

在提供的DeviceConfig结构体中,type、brdLocation和bitMean都是回调类型。对于字符串字段,通常可以使用pb_callback_t的简单方式,例如直接指定字符串的指针和长度,或者设置一个encode函数。而bitMean是repeated的KeyValue,需要处理为重复字段,可能使用多次回调或者预分配数组。但根据生成的代码,这里可能还是需要使用回调来处理每个条目。

因此在以下代码中,处理这些回调字段是关键。对于编码,需要为每个pb_callback_t字段设置对应的函数,或者直接填充数据。例如,对于字符串字段,可能可以使用pb_ostream_from_buffer来创建一个输出流,然后使用pb_encode函数来编码数据。

以下为编码和解码的使用:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pb_encode.h>
#include <pb_decode.h>
#include "simple.pb.h"

// 编码回调 ---------------------------------------------------
bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *arg) {
    const char *str = *(const char **)*arg;
    if (!pb_encode_tag_for_field(stream, field))
        return false;
    return pb_encode_string(stream, (uint8_t*)str, strlen(str));
}

bool encode_bitMean(pb_ostream_t *stream, const pb_field_t *field, void *const *arg) {
    const example_KeyValue *items = (const example_KeyValue *)*arg;
    for (size_t i = 0; i < 2; i++) {
        if (!pb_encode_tag_for_field(stream, field))
            return false;
        if (!pb_encode_submessage(stream, example_KeyValue_fields, &items[i]))
            return false;
    }
    return true;
}

// 解码回调 ---------------------------------------------------
typedef struct {
    char *key;
    int32_t value;
} KeyValueData;

bool decode_string(pb_istream_t *stream, const pb_field_t *field, void **arg) {
    char **strptr = (char **)*arg;
    size_t len = stream->bytes_left;
    *strptr = malloc(len + 1);
    if (!pb_read(stream, (uint8_t*)*strptr, len)) {
        free(*strptr);
        return false;
    }
    (*strptr)[len] = '\0';
    return true;
}

bool decode_bitMean(pb_istream_t *stream, const pb_field_t *field, void **arg) {
    KeyValueData **items = (KeyValueData **)*arg;
    size_t count = (*items == NULL) ? 0 : (*items)[0].value;
    
    // 扩展数组空间(使用[0]存储计数)
    KeyValueData *new_items = realloc(*items, (count + 1 + 1) * sizeof(KeyValueData));
    if (!new_items) return false;
    
    *items = new_items;
    (*items)[0].value = count + 1; // 更新计数
    
    KeyValueData *item = &(*items)[count + 1]; // 数据从[1]开始
    item->key = NULL;
    
    example_KeyValue kv = example_KeyValue_init_zero;
    kv.key.arg = &item->key;
    kv.key.funcs.decode = decode_string;
    
    if (!pb_decode(stream, example_KeyValue_fields, &kv)) {
        return false;
    }
    item->value = kv.value;
    return true;
}

int main() {
    /**************** 编码阶段 ****************/
    example_DeviceConfig encode_config = example_DeviceConfig_init_default;
    uint8_t buffer[256];
    
    // 配置编码参数
    encode_config.BrdNum = 123;
    encode_config.address = 456;
    encode_config.priority = 1;
    encode_config.accessTime = 999;

    const char *type_str = "temperature";
    encode_config.type.arg = &type_str;
    encode_config.type.funcs.encode = encode_string;

    const char *loc_str = "Room 101";
    encode_config.brdLocation.arg = &loc_str;
    encode_config.brdLocation.funcs.encode = encode_string;

    example_KeyValue bit_means[2] = {
        {.key.arg = (void*)&(const char*[]){"status"},   .key.funcs.encode = encode_string, .value = 100},
        {.key.arg = (void*)&(const char*[]){"error"},   .key.funcs.encode = encode_string, .value = 0}
    };
    encode_config.bitMean.arg = bit_means;
    encode_config.bitMean.funcs.encode = encode_bitMean;

    pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
    if (!pb_encode(&ostream, example_DeviceConfig_fields, &encode_config)) {
        fprintf(stderr, "Encode error: %s\n", PB_GET_ERROR(&ostream));
        return 1;
    }

    /**************** 解码阶段 ****************/
    example_DeviceConfig decode_config = example_DeviceConfig_init_zero;
    KeyValueData *bit_means_decoded = NULL;
    char *decoded_type = NULL;
    char *decoded_loc = NULL;

    // 配置解码回调
    decode_config.type.arg = &decoded_type;
    decode_config.type.funcs.decode = decode_string;
    
    decode_config.brdLocation.arg = &decoded_loc;
    decode_config.brdLocation.funcs.decode = decode_string;
    
    decode_config.bitMean.arg = &bit_means_decoded;
    decode_config.bitMean.funcs.decode = decode_bitMean;

    pb_istream_t istream = pb_istream_from_buffer(buffer, ostream.bytes_written); // 关键修正点
    if (!pb_decode(&istream, example_DeviceConfig_fields, &decode_config)) {
        fprintf(stderr, "Decode error: %s\n", PB_GET_ERROR(&istream));
        
        free(decoded_type);
        free(decoded_loc);
        if (bit_means_decoded) {
            for (size_t i = 1; i <= bit_means_decoded[0].value; i++)
                free(bit_means_decoded[i].key);
            free(bit_means_decoded);
        }
        return 1;
    }

    /**************** 输出结果 ****************/
    printf("Decoded config:\n"
           "Type: %s\n"
           "Location: %s\n"
           "BitMeans count: %d\n",
           decoded_type, decoded_loc, 
           bit_means_decoded ? bit_means_decoded[0].value : 0);

    if (bit_means_decoded) {
        for (int i = 1; i <= bit_means_decoded[0].value; i++) {
            printf("  [%d] %s = %d\n", 
                   i, bit_means_decoded[i].key, bit_means_decoded[i].value);
        }
    }

    /**************** 清理资源 ****************/
    free(decoded_type);
    free(decoded_loc);
    if (bit_means_decoded) {
        for (int i = 1; i <= bit_means_decoded[0].value; i++) {
            free(bit_means_decoded[i].key);
        }
        free(bit_means_decoded);
    }

    return 0;
}

优化使用示例

上述示例,使用了回调和动态内存分配,稍显复杂,改为以下方式则代码更简洁、内存管理更安全。
以下是通过添加nanopb选项优化proto定义后的使用示例:

1. 修改后的proto文件

syntax = "proto2";
import "nanopb.proto";  // 需要nanopb库中的选项定义

package example;

message KeyValue {
    required string key = 1 [(nanopb).max_size = 64];  // 限制key最大64字节
    required int32 value = 2;
}

message DeviceConfig {
    required int32 BrdNum = 1;
    required int32 address = 2;
    required string type = 3 [(nanopb).max_size = 64];    // 限制type长度
    required int32 priority = 4;
    required int32 accessTime = 5;
    required string brdLocation = 6 [(nanopb).max_size = 128]; // 限制位置字符串
    repeated KeyValue bitMean = 7 [(nanopb).max_count = 10];   // 最多10个元素
}

2. 生成新的头文件

使用nanopb生成器命令:

protoc --nanopb_out=. simple.proto

3. 优化后的使用示例

#include "simple.pb.h"
#include <pb_encode.h>
#include <pb_decode.h>
#include <stdio.h>
#include <string.h>

int main() {
    /**************** 编码示例 ****************/
    example_DeviceConfig config = example_DeviceConfig_init_default;
    
    // 直接赋值字符串(不再需要回调)
    strncpy(config.type, "temperature", sizeof(config.type));
    strncpy(config.brdLocation, "Room 101", sizeof(config.brdLocation));
    
    // 设置基本数值字段
    config.BrdNum = 123;
    config.address = 456;
    config.priority = 1;
    config.accessTime = 999;

    // 填充重复字段
    config.bitMean_count = 2;  // 自动生成的数组长度字段
    strncpy(config.bitMean[0].key, "status", sizeof(config.bitMean[0].key));
    config.bitMean[0].value = 100;
    strncpy(config.bitMean[1].key, "error", sizeof(config.bitMean[1].key));
    config.bitMean[1].value = 0;

    // 编码缓冲区
    uint8_t buffer[256];
    pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
    
    if (!pb_encode(&ostream, example_DeviceConfig_fields, &config)) {
        fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&ostream));
        return 1;
    }

    /**************** 解码示例 ****************/
    example_DeviceConfig decoded = example_DeviceConfig_init_default;
    pb_istream_t istream = pb_istream_from_buffer(buffer, ostream.bytes_written);
    
    if (!pb_decode(&istream, example_DeviceConfig_fields, &decoded)) {
        fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&istream));
        return 1;
    }

    /**************** 输出结果 ****************/
    printf("Decoded config:\n"
           "Type: %s\n"
           "Location: %s\n"
           "BitMeans count: %d\n",
           decoded.type,
           decoded.brdLocation,
           decoded.bitMean_count);

    for (int i = 0; i < decoded.bitMean_count; i++) {
        printf("  [%d] %s = %d\n", 
               i, decoded.bitMean[i].key, decoded.bitMean[i].value);
    }

    return 0;
}

优化后代码的主要变化

  1. 数据结构变化

    // 原始回调方式
    pb_callback_t type;
    
    // 优化后静态分配
    char type[64]; 
    size_t type_size;
    
  2. 重复字段处理

    // 原始方式
    pb_callback_t bitMean;
    
    // 优化后静态数组
    example_KeyValue bitMean[10];
    size_t bitMean_count;
    
  3. 字符串处理

    // 之前需要回调
    config.type.funcs.encode = encode_string;
    
    // 现在直接操作
    strncpy(config.type, "text", sizeof(config.type));
    

关键优势说明

  1. 内存管理简化

    • 所有字符串字段变为固定长度char数组
    • 重复字段变为固定大小的数组+count计数器
    • 不再需要手动malloc/free
  2. 性能提升

    • 消除回调函数开销
    • 数据内存连续,提高缓存命中率
  3. 代码可读性增强

    • 字段访问方式与常规结构体一致
    • 减少约60%的样板代码

注意事项

  1. 字段长度限制

    • 超出max_size的字符串会被截断
    • 超过max_count的数组元素会被丢弃
  2. 默认值初始化

    example_DeviceConfig_init_default  // 会清零所有字段
    example_DeviceConfig_init_zero     // 同上,两者等效
    
  3. 字符串处理建议

    // 使用strncpy防止溢出
    strncpy(config.type, src, sizeof(config.type));
    config.type[sizeof(config.type)-1] = '\0'; // 确保终止符
    
  4. 协议兼容性

    • 修改后的proto仍然与标准protobuf兼容
    • nanopb选项仅影响生成代码的实现方式

推荐使用场景

  • 嵌入式系统(内存受限环境)
  • 需要确定性内存分配的场景
  • 对性能要求较高的实时系统
  • 希望简化代码逻辑的项目

通过这种优化方式,代码复杂度显著降低,同时保持了协议的高效性。建议根据实际字段的预期最大长度来设置合理的max_sizemax_count值,在内存使用和灵活性之间取得平衡。
在这里插入图片描述

总结

通过本文,我们了解了什么是 nanopb,及其在嵌入式系统中的应用场景。我们还学习了如何安装和使用 nanopb,包括编写 Protocol Buffers 定义文件、C 代码文件以及 Makefile 文件。通过这些步骤,你可以轻松地在你的项目中集成 nanopb,以便更高效地进行消息的序列化和反序列化。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2309395.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

(十 二)趣学设计模式 之 享元模式!

目录 一、 啥是享元模式&#xff1f;二、 为什么要用享元模式&#xff1f;三、 享元模式的实现方式四、 享元模式的优缺点五、 享元模式的应用场景六、 总结 &#x1f31f;我的其他文章也讲解的比较有趣&#x1f601;&#xff0c;如果喜欢博主的讲解方式&#xff0c;可以多多支…

Trae:国内首款AI原生IDE,编程效率大提升

今年一月&#xff0c;在新闻上看到字节跳动面向海外市场推出了一款名为Trae的AI集成开发环境&#xff08;IDE&#xff09;。起初&#xff0c;我并未给予过多关注&#xff0c;因为市面上已有不少IDE集成了AI插件&#xff0c;功能也非常全面&#xff0c;而字节跳动自家的MarsCode…

RocketMQ定时/延时消息实现机制

RocketMQ 的延迟消息是其核心特性之一&#xff0c;允许消息在指定延迟时间后才被消费者消费。 定时消息生命周期 一、延迟消息的核心机制 RocketMQ&#xff08;5.0之前&#xff09; 不支持任意时间精度的延迟&#xff0c;而是通过预定义的 延迟级别&#xff08;Delay Level&a…

基于SpringBoot的校园二手交易平台(源码+论文+部署教程)

运行环境 校园二手交易平台运行环境如下&#xff1a; • 前端&#xff1a;Vue • 后端&#xff1a;Java • IDE工具&#xff1a;IntelliJ IDEA&#xff08;可自行更换&#xff09; • 技术栈&#xff1a;SpringBoot Vue MySQL 主要功能 校园二手交易平台主要包含前台和…

利用 LangChain 和一个大语言模型(LLM)构建一个链条,自动从用户输入的问题中提取相关的 SQL 表信息,再生成对应的 SQL 查询

示例代码&#xff1a; from langchain_core.runnables import RunnablePassthrough from langchain.chains import create_sql_query_chain from operator import itemgetter from langchain.chains.openai_tools import create_extraction_chain_pydantic# 系统消息&#xff…

力扣hot 100之矩阵四题解法总结

本期总结hot100 中二维矩阵的题&#xff0c;时空复杂度就不分析了 1.矩阵置零 原地标记&#xff0c;用第一行和第一列作为当前行列是否为0的标记&#xff0c;同时用两个标签分别记录0行、0列的标记空间中原本是否有0 class Solution:def setZeroes(self, matrix: List[List[…

在Linux上使用APT安装Sniffnet的详细步骤

一、引言 Sniffnet 是一款开源的网络流量监控工具&#xff0c;适用于多种Linux发行版。如果你的Linux系统使用APT&#xff08;Advanced Package Tool&#xff09;作为包管理器&#xff0c;以下是如何通过APT安装Sniffnet的详细步骤。 二、系统要求 在开始安装之前&#xff0…

zookeeper-docker版

Zookeeper-docker版 1 zookeeper概述 1.1 什么是zookeeper Zookeeper是一个分布式的、高性能的、开源的分布式系统的协调&#xff08;Coordination&#xff09;服务&#xff0c;它是一个为分布式应用提供一致性服务的软件。 1.2 zookeeper应用场景 zookeeper是一个经典的分…

StableDiffusion本地部署 3 整合包猜想

本地部署和整合包制作猜测 文章目录 本地部署和整合包制作猜测官方部署第一种第二种 StabilityMatrix下载整合包制作流程猜测 写了这么多python打包和本地部署的文章&#xff0c;目的是向做一个小整合包出来&#xff0c;不要求有图形界面&#xff0c;只是希望一键就能运行。 但…

数据结构(初阶)(七)----树和二叉树(前中后序遍历)

实现链式结构的二叉树 实现链式结构的二叉树遍历前序遍历中序遍历后序遍历 节点个数叶子节点个数⼆叉树第k层结点个数⼆叉树的深度/⾼度查找值为X的节点二叉树的销毁 层序遍历判断二叉树是否为完全二叉树 ⽤链表来表⽰⼀棵⼆叉树&#xff0c;即⽤链来指⽰元素的逻辑关系。 通常…

科技赋能筑未来 中建海龙MiC建筑技术打造保障房建设新标杆

近日&#xff0c;深圳梅林路6号保障房项目顺利封顶&#xff0c;标志着国内装配式建筑领域又一里程碑式突破。中建海龙科技有限公司&#xff08;以下简称“中建海龙”&#xff09;以模块化集成建筑&#xff08;MiC&#xff09;技术为核心&#xff0c;通过科技创新与工业化建造深…

json介绍、python数据和json数据的相互转换

目录 一 json介绍 json是什么&#xff1f; 用处 Json 和 XML 对比 各语言对Json的支持情况 Json规范详解 二 python数据和json数据的相互转换 dumps() : 转换成json loads(): 转换成python数据 总结 一 json介绍 json是什么&#xff1f; 实质上是一条字符串 是一种…

计算机毕设JAVA——某高校宿舍管理系统(基于SpringBoot+Vue前后端分离的项目)

文章目录 概要项目演示图片系统架构技术运行环境系统功能简介 概要 网络上许多计算机毕设项目开发前端界面设计复杂、不美观&#xff0c;而且功能结构十分单一&#xff0c;存在很多雷同的项目&#xff1a;不同的项目基本上就是套用固定模板&#xff0c;换个颜色、改个文字&…

Spring Boot 测试:单元、集成与契约测试全解析

一、Spring Boot 分层测试策略 Spring Boot 应用采用经典的分层架构&#xff0c;不同层级的功能模块对应不同的测试策略&#xff0c;以确保代码质量和系统稳定性。 Spring Boot 分层架构&#xff1a; Spring Boot分层架构 A[客户端] -->|HTTP 请求| B[Controller 层] …

Oracle 数据库基础入门(四):分组与联表查询的深度探索(上)

在 Oracle 数据库的学习进程中&#xff0c;分组查询与联表查询是进阶阶段的重要知识点&#xff0c;它们如同数据库操作的魔法棒&#xff0c;能够从复杂的数据中挖掘出有价值的信息。对于 Java 全栈开发者而言&#xff0c;掌握这些技能不仅有助于高效地处理数据库数据&#xff0…

机器学习的起点:线性回归Linear Regression

机器学习的起点&#xff1a;线性回归Linear Regression 作为机器学习的起点&#xff0c;线性回归是理解算法逻辑的绝佳入口。我们从定义、评估方法、应用场景到局限性&#xff0c;用生活化的案例和数学直觉为你构建知识框架。 回归算法 一、线性回归的定义与核心原理 定义&a…

17、什么是智能指针,C++有哪几种智能指针【高频】

智能指针其实不是指针&#xff0c;而是一个&#xff08;模板&#xff09;类&#xff0c;用来存储指向某块资源的指针&#xff0c;并自动释放这块资源&#xff0c;从而解决内存泄漏问题。主要有以下四种&#xff1a; auto_ptr 它的思想就是当当一个指针对象赋值给另一个指针对…

PyCharm接入本地部署DeepSeek 实现AI编程!【支持windows与linux】

今天尝试在pycharm上接入了本地部署的deepseek&#xff0c;实现了AI编程&#xff0c;体验还是很棒的。下面详细叙述整个安装过程。 本次搭建的框架组合是 DeepSeek-r1:1.5b/7b Pycharm专业版或者社区版 Proxy AI&#xff08;CodeGPT&#xff09; 首先了解不同版本的deepsee…

PyCharm怎么集成DeepSeek

PyCharm怎么集成DeepSeek 在PyCharm中集成DeepSeek等大语言模型(LLM)可以借助一些插件或通过代码调用API的方式实现,以下为你详细介绍两种方法: 方法一:使用JetBrains AI插件(若支持DeepSeek) JetBrains推出了AI插件来集成大语言模型,不过截至2024年7月,官方插件主要…

【定昌Linux系统】部署了java程序,设置开启启动

将代码上传到相应的目录&#xff0c;并且配置了一个.sh的启动脚本文件 文件内容&#xff1a; #!/bin/bash# 指定JAR文件的路径&#xff08;如果JAR文件在当前目录&#xff0c;可以直接使用文件名&#xff09; JAR_FILE"/usr/local/java/xs_luruan_client/lib/xs_luruan_…