首页 > 其他分享 >ESP32+RS485参考代码要点+@环境esp-idf-v5.1.2 +vscode 草稿

ESP32+RS485参考代码要点+@环境esp-idf-v5.1.2 +vscode 草稿

时间:2024-05-17 11:52:26浏览次数:15  
标签:set UART vscode ESP32 RS485 uart num ESP

在环境esp-idf-v5.1.2 +vscode 中,如何在一个文件内,调用另外一个文件夹内定义的函数。

 




设置帧内间隔( 在传输线上,两个发送的字节之间的时间间隔,不超过3.5发送单个字节的时间。)
通过函数 esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh)实现此功能。

在RTOS中,主机发送查询命令后,一般可以阻塞等待从机应答。这个阻塞接收应答的函数可以由uart_read_bytes()实现。此函数用法:
int len = uart_read_bytes(uart_num, data, BUF_SIZE, PACKET_READ_TICS);
其中:#define PACKET_READ_TICS        (100 / portTICK_PERIOD_MS)
/**
 * @brief UART set threshold timeout for TOUT feature
 *
 * @param uart_num     Uart number to configure, the max port number is (UART_NUM_MAX -1).
 * @param tout_thresh  This parameter defines timeout threshold in uart symbol periods. The maximum value of threshold is 126.
 *        tout_thresh = 1, defines TOUT interrupt timeout equal to transmission time of one symbol (~11 bit) on current baudrate.
 *        If the time is expired the UART_RXFIFO_TOUT_INT interrupt is triggered. If tout_thresh == 0,
 *        the TOUT feature is disabled.
 *
 * @return
 *     - ESP_OK Success
 *     - ESP_ERR_INVALID_ARG Parameter error
 *     - ESP_ERR_INVALID_STATE Driver is not installed
 */
esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh);

举例 初始化为半双工RS485工作模式

/**
 * @brief       初始化RS485
 * @param       串口编号
 * @retval      无
 */
void mcu_rs485_init(uint16_t ncom)
{
    uint16_t uart_num = 0;
    uart_num =ncom;
    uart_config_t uart_config = {
        .baud_rate = BAUD_RATE,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .rx_flow_ctrl_thresh = 122,
        .source_clk = UART_SCLK_DEFAULT,
    };

    // Set UART log level
    esp_log_level_set(TAG, ESP_LOG_INFO);

    ESP_LOGI(TAG, "Start RS485 configure UART.");

    // Install UART driver (we don't need an event queue here)
    // In this example we don't even use a buffer for sending data.
    ESP_ERROR_CHECK(uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0));

    // Configure UART parameters
    ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));

    ESP_LOGI(TAG, "UART set pins, mode and install driver.");

    // Set UART pins as per KConfig settings
    ESP_ERROR_CHECK(uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));

    // Set RS485 half duplex mode
    ESP_ERROR_CHECK(uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX));

    // Set read timeout of UART TOUT feature  3.5 bytesTime  ECHO_READ_TOUT =3
    ESP_ERROR_CHECK(uart_set_rx_timeout(uart_num, ECHO_READ_TOUT));

    // // Allocate buffers for UART
    // uint8_t* data = (uint8_t*) malloc(BUF_SIZE);

    ESP_LOGI(TAG, "UART %d ready.\r\n", uart_num);
    //echo_send(uart_num, "Start RS485 UART test.\r\n", 24);
}

 

标签:set,UART,vscode,ESP32,RS485,uart,num,ESP
From: https://www.cnblogs.com/excellentHellen/p/18189917

相关文章

  • 韭菜盒子!VSCode 也可以看股票&基金了!
    大家好,我是Java陈序员。“打工这辈子是不可能打工的”!仅仅靠打工、上班是很难实现财务自由的,当我们积累了一定的财富,就会去理财。将自己的资本投入到一些理财产品中,如基金、股票等。但常常操作不当,就会被“割韭菜”!今天,给大家介绍一款VSCode插件,支持实时查看股票、基金数据,......
  • esp32笔记[17]-显示网络延迟
    摘要使用esp32c3;使用软件i2c方式驱动ssd1306显示屏显示网络延迟和NTP时间;关键信息开发环境:ArduinoIDE主控:esp32c3显示屏:ssd1306原理简介ping测试网络延迟简介[https://github.com/dvarrel/ESPping][https://blog.csdn.net/qq_31536117/article/details/134757851......
  • vscode 避免每次ssh校验
    在本地生成public_key将文件传到虚拟机上[email protected]:.ssh将id_rsa.pub内容写到authorized_keys中catid_rsa.pub>>authorized_keys......
  • VScode 运行 jupyter 心得
    在服务器上,用vscode运行.ipynb文件是常用的手段,但是搞多了就会发现还是会有各种问题,在这里记录一下。os.environ的使用经常在这个运行一个程序前,我们需要加载一些环境变量,来设置比如代理转发(用于下载外面的东西),例:importosproxy_list=['HTTP_PROXY','HTTPS_PROXY',......
  • 使用vscode在线打开github项目
    使用vscode在线打开github项目方式1:使用github1s项目操作方式非常的简单,只需要在浏览器网址部分中的“github”后边,添加一个“1s”就可以了。缺点:代码都是以只读模式打开的,只能看,没法改。测试项目地址https://github.com/Ponderfly/GoogleTranslateIpCheck/github后面加个1s......
  • 【VSCode】VSCode常用设置
    1、中文插件安装     使用按下“Ctrl+Shift+P”组合键以显示“命令面板”,然后键入“display”以筛选并显示“ConfigureDisplayLanguage”命令。按“Enter”,然后会按区域设置显示安装的语言列表,并突出显示当前语言设置。选择另一个“语言”以切换UI语......
  • windows和vscode终端美化
    win商店下载powershell,保证版本在7以上.然后配置背景图,背景图透明度50.浏览器下载ohmypush,安装程序,重启powershelloh-my-poshfontinstall安装字体,选择Meslonotepad$PROFILE如果没有这个文件.先New-Item-Path$PROFILE-TypeFile-Force然后输入oh-my-pos......
  • 【VsCode输出中文乱码问题】用vscode写c/c++时,终端输出结果为中文乱码如何解决?
    前言因为我平时在vscode写代码,在输出语句时,不会出现中文,所以之前一直没有遇到这个问题。但是今天,在练习的时候,涉及到了中文输出,所以遇到了这个中文乱码问题。我在运行代码时,发现代码页面上的中文正常显示,而终端输出的中文却是乱码其实,出现中文乱码是非常常见的事了,比如在浏览......
  • Преимущества RS485 для сбора данных LoRaWAN
    СборщикданныхRS485toLoRaWAN—этосборщикданных,которыйиспользуетстандартныйпротоколLoRaWANдлябеспроводногоинтерфейсавосходящейлиниисв......
  • 程序员的AI编程小助手,CodeGeeX在vscode,vs2022,IDEA2023使用体验总结
    程序员的AI编程小助手,CodeGeeX使用体验总结一、1.CodeGeeX是什么?能做什么?CodeGeeX是一个智能编程软件工具,目前CodeGeeX支持多种主流IDE,如VSCode、visualstudio2022,IntelliJIDEA、PyCharm、Vim等,同时,支持Python、Java、C++/C、JavaScript、Go等多种语言。CodeGeeX如何......