#lora #serial #tcp #mesh #usb-serial

meshtastic

一个用于与Meshtastic设备通信和配置的Rust库

7个版本

0.1.6 2024年3月20日
0.1.5 2023年12月22日
0.1.4 2023年9月7日

#4 in #lora

50 每月下载量

GPL-3.0 许可协议

145KB
2K SLoC

Meshtastic.rs

概述

Meshtastic.rs是一个crate,允许您在Rust中与Meshtastic设备交互。此crate旨在在桌面环境中使用,目前支持通过USB串行和TCP连接到收音机。

此crate设计用于在tokio异步运行时中使用。

Crates.io Documentation License

安装

您可以使用以下命令将此crate添加到项目中

cargo add meshtastic

用法

此crate在/examples目录中提供了基本的TCP和串行连接示例。您可以使用以下命令运行这些示例

cargo run --example basic_tcp
cargo run --example basic_serial

TCP示例

此示例需要一个具有暴露IP端口的Meshtastic或通过Meshtastic Docker实例模拟的收音机(见此处)。

/// This example connects to a TCP port on the radio, and prints out all received packets.
/// This can be used with a simulated radio via the Meshtastic Docker firmware image.
/// https://meshtastic.org/docs/software/linux-native#usage-with-docker

use std::io::{self, BufRead};

use meshtastic::api::StreamApi;
use meshtastic::utils;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let stream_api = StreamApi::new();

    println!("Enter the address of a TCP port to connect to, in the form \"IP:PORT\":");

    let stdin = io::stdin();
    let entered_address = stdin
        .lock()
        .lines()
        .next()
        .expect("Failed to find next line")
        .expect("Could not read next line");

    let tcp_stream = utils::stream::build_tcp_stream(entered_address).await?;
    let (mut decoded_listener, stream_api) = stream_api.connect(tcp_stream).await;

    let config_id = utils::generate_rand_id();
    let stream_api = stream_api.configure(config_id).await?;

    // This loop can be broken with ctrl+c, or by unpowering the radio.
    while let Some(decoded) = decoded_listener.recv().await {
        println!("Received: {:?}", decoded);
    }

    // Note that in this specific example, this will only be called when
    // the radio is disconnected, as the above loop will never exit.
    // Typically you would allow the user to manually kill the loop,
    // for example with tokio::select!.
    let _stream_api = stream_api.disconnect().await?;

    Ok(())
}

串行示例

此示例需要一个已供电并闪存的Meshtastic收音机,通过USB串行端口连接到主机计算机。

/// This example connects to a radio via serial, and prints out all received packets.
/// This example requires a powered and flashed Meshtastic radio.
/// https://meshtastic.org/docs/supported-hardware

use std::io::{self, BufRead};

use meshtastic::api::StreamApi;
use meshtastic::utils;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let stream_api = StreamApi::new();

    let available_ports = utils::stream::available_serial_ports()?;
    println!("Available ports: {:?}", available_ports);
    println!("Enter the name of a port to connect to:");

    let stdin = io::stdin();
    let entered_port = stdin
        .lock()
        .lines()
        .next()
        .expect("Failed to find next line")
        .expect("Could not read next line");

    let serial_stream = utils::stream::build_serial_stream(entered_port, None, None, None)?;
    let (mut decoded_listener, stream_api) = stream_api.connect(serial_stream).await;

    let config_id = utils::generate_rand_id();
    let stream_api = stream_api.configure(config_id).await?;

    // This loop can be broken with ctrl+c, or by disconnecting
    // the attached serial port.
    while let Some(decoded) = decoded_listener.recv().await {
        println!("Received: {:?}", decoded);
    }

    // Note that in this specific example, this will only be called when
    // the radio is disconnected, as the above loop will never exit.
    // Typically you would allow the user to manually kill the loop,
    // for example with tokio::select!.
    let _stream_api = stream_api.disconnect().await?;

    Ok(())
}

统计数据

Alt

贡献

欢迎贡献!如果您发现错误或想提议新功能,请打开问题或提交pull请求。

许可协议

本项目采用GPL-3.0许可协议。

依赖关系

~8–40MB
~596K SLoC