Skip to content

Instantly share code, notes, and snippets.

View wsxq2's full-sized avatar
🏠
Working from home

wsxq2 wsxq2

🏠
Working from home
View GitHub Profile
@wsxq2
wsxq2 / modbus.c
Last active February 23, 2025 02:57
modbus rtu master driver for stm32. 依赖 queue.c 和 common.c,两种方式计算CRC16,一个时间复杂度低但空间复杂度高,另一个相反。使用 queue 时,利用指针强转实现泛型。大端实现目前用的是编译器宏,可能需要考虑更好的方式。响应报文的解析尚未完成,调用者如何使用也是个问题,可考虑再添加个状态机。用软件模拟slave,进行联合测试尚未完成,该测试需要使用socat工具创建两个回环的虚拟串口,一个串口和slave软件连接,另一个和该程序连接,slave软件尝试过modbusdriver的diagslave,但没有调试信息,不知道收到的报文和发出的报文,后续选择使用pymodbus中的simulator,它可以通过json文件配置其寄存…
#include "modbus.h"
#include <string.h>
#include "common.h"
#include <stdio.h>
#ifndef __weak
#define __weak __attribute__((weak))
#endif /* __weak */
#define ADU_FIXED_LEN (1+2) //slave_addr + crc
@wsxq2
wsxq2 / common.c
Created February 20, 2025 09:58
STM32环境下常用函数
#include "common.h"
#include <stdio.h>
#include <string.h>
void cm_print_buf(const char*hint, const uint8_t *buf, int buflen)
{
char tmp_buf[DP_TXBUFF_LEN], *p=tmp_buf;
int len=strnlen(hint, DP_TXBUFF_LEN);
if(len+buflen*3+1>=DP_TXBUFF_LEN) return;
@wsxq2
wsxq2 / queue.c
Last active February 20, 2025 08:36
STM32简单环形队列实现,有两种策略:未定义`Q_KEEP_NEWEST`时会在队列满时无法入队列,即丢弃最新的帧;定义`Q_KEEP_NEWEST`时在队列满时也会成功入队列,并删除最旧的元素,从而保证最新的帧总能接收。使用前根据具体使用需求调整`Q_MSG_BUF_LEN`和`Q_MSG_MAX_SIZE`
#include "queue.h"
#include <stdio.h>
#include <string.h>
bool q_create(q_queue* q, int maxsize)
{
if (maxsize > Q_MSG_MAX_SIZE)
return false;
q->front = 0;
@wsxq2
wsxq2 / CMakeLists.txt
Created February 20, 2025 03:22
最小cmake文件,可用于单元测试
cmake_minimum_required(VERSION 3.12)
SET(PROJECT_NAME test)
project(${PROJECT_NAME})
add_executable (${PROJECT_NAME} main.c lidar.c)
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -g -O0)
## 1. install pwsh in Microsoft Store
## 2. execute command on remote pc(server)
Set-Service -Name sshd -StartupType 'Automatic' # Enable the sshd service
Start-Service sshd # Start the sshd service
#Check firewall
if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
@wsxq2
wsxq2 / backup_sd.sh
Last active June 28, 2024 10:02
backup raspberry pi sd image
#!/bin/bash
set -exu
BOOTDEV=/dev/mmcblk0p1
ROOTDEV=/dev/mmcblk0p2
MOUNT_POINT=/mnt
[[ -d $MOUNT_POINT ]] || mkdir -p $MOUNT_POINT
@wsxq2
wsxq2 / auto_export_data_from_excel_to_word.vb
Last active December 29, 2023 05:24
将 Excel 中的 N 行数据导出到 Word 中的 N 个表格中
Sub auto_export_data_from_excel_to_word()
'将 Excel 中的 N 行数据导出到 Word 中的 N 个表格中
'输入:一个Excel表(提供数据),一个doc模板(提供表格模板)
'输出:一个doc文件,默认名称为 output.doc
'使用方法:
' 1. 打开Word和 Excel 的“开发工具”工具栏。可Bing搜索具体方法
' 2. 复制本函数代码到Excel文件的Visual Basic中:先打开Excel文件,点击“开发工具”,再点击“VisualBasic”,然后新建一个模块,将本函数代码复制过去
' 3. 调整本函数:一是调整doc输入输出文件名;二是Excel表格起始行和结束行,同时注意 wdapp.ActiveDocument.tables(r -1)的下标要正确调整;三是数据赋值部分需要正确对应相应的单元格;四是查找的内容要正确替换
' 4. 运行